Skip to content

Commit ec4fa23

Browse files
GiteaBotdenyskonlunny
authored
use existing oauth grant for public client (#31015) (#31041)
Backport #31015 by @denyskon Do not try to create a new authorization grant when one exists already, thus preventing a DB-related authorization issue. Fix #30790 (comment) Co-authored-by: Denys Konovalov <kontakt@denyskon.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent 33d4d32 commit ec4fa23

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

routers/web/auth/oauth.go

+17-2
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)