Skip to content

Commit f1d9f18

Browse files
authored
Return access_denied error when an OAuth2 request is denied (#30974)
According to [RFC 6749](https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1), when the resource owner or authorization server denied an request, an `access_denied` error should be returned. But currently in this case Gitea does not return any error. For example, if the user clicks "Cancel" here, an `access_denied` error should be returned. <img width="360px" src="https://github.com/go-gitea/gitea/assets/15528715/be31c09b-4c0a-4701-b7a4-f54b8fe3a6c5" />
1 parent de9bcd1 commit f1d9f18

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

routers/web/auth/oauth.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@ func GrantApplicationOAuth(ctx *context.Context) {
541541
ctx.Error(http.StatusBadRequest)
542542
return
543543
}
544+
545+
if !form.Granted {
546+
handleAuthorizeError(ctx, AuthorizeError{
547+
State: form.State,
548+
ErrorDescription: "the request is denied",
549+
ErrorCode: ErrorCodeAccessDenied,
550+
}, form.RedirectURI)
551+
return
552+
}
553+
544554
app, err := auth.GetOAuth2ApplicationByClientID(ctx, form.ClientID)
545555
if err != nil {
546556
ctx.ServerError("GetOAuth2ApplicationByClientID", err)

services/forms/user_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func (f *AuthorizationForm) Validate(req *http.Request, errs binding.Errors) bin
161161
// GrantApplicationForm form for authorizing oauth2 clients
162162
type GrantApplicationForm struct {
163163
ClientID string `binding:"Required"`
164+
Granted bool
164165
RedirectURI string
165166
State string
166167
Scope string

templates/user/auth/grant.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
<input type="hidden" name="scope" value="{{.Scope}}">
2424
<input type="hidden" name="nonce" value="{{.Nonce}}">
2525
<input type="hidden" name="redirect_uri" value="{{.RedirectURI}}">
26-
<button type="submit" id="authorize-app" value="{{ctx.Locale.Tr "auth.authorize_application"}}" class="ui red inline button">{{ctx.Locale.Tr "auth.authorize_application"}}</button>
27-
<a href="{{.RedirectURI}}" class="ui basic primary inline button">Cancel</a>
26+
<button type="submit" id="authorize-app" name="granted" value="true" class="ui red inline button">{{ctx.Locale.Tr "auth.authorize_application"}}</button>
27+
<button type="submit" name="granted" value="false" class="ui basic primary inline button">{{ctx.Locale.Tr "cancel"}}</button>
2828
</form>
2929
</div>
3030
</div>

0 commit comments

Comments
 (0)