Skip to content

Commit 49117e8

Browse files
denyskonlunny
authored andcommitted
use existing oauth grant for public client (go-gitea#31015)
Do not try to create a new authorization grant when one exists already, thus preventing a DB-related authorization issue. Fix go-gitea#30790 (comment) --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent 33d4d32 commit 49117e8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

routers/web/auth/oauth.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,15 +544,30 @@ func GrantApplicationOAuth(ctx *context.Context) {
544544
ctx.ServerError("GetOAuth2ApplicationByClientID", err)
545545
return
546546
}
547-
grant, err := app.CreateGrant(ctx, ctx.Doer.ID, form.Scope)
547+
grant, err := app.GetGrantByUserID(ctx, ctx.Doer.ID)
548548
if err != nil {
549+
handleServerError(ctx, form.State, form.RedirectURI)
550+
return
551+
}
552+
if grant == nil {
553+
grant, err = app.CreateGrant(ctx, ctx.Doer.ID, form.Scope)
554+
if err != nil {
555+
handleAuthorizeError(ctx, AuthorizeError{
556+
State: form.State,
557+
ErrorDescription: "cannot create grant for user",
558+
ErrorCode: ErrorCodeServerError,
559+
}, form.RedirectURI)
560+
return
561+
}
562+
} else if grant.Scope != form.Scope {
549563
handleAuthorizeError(ctx, AuthorizeError{
550564
State: form.State,
551-
ErrorDescription: "cannot create grant for user",
565+
ErrorDescription: "a grant exists with different scope",
552566
ErrorCode: ErrorCodeServerError,
553567
}, form.RedirectURI)
554568
return
555569
}
570+
556571
if len(form.Nonce) > 0 {
557572
err := grant.SetNonce(ctx, form.Nonce)
558573
if err != nil {

0 commit comments

Comments
 (0)