Skip to content

Commit e3390e2

Browse files
GiteaBotdenyskonlunny
authored
use existing oauth grant for public client (#31015) (#31042)
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 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 27a4c67 commit e3390e2

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
@@ -556,15 +556,30 @@ func GrantApplicationOAuth(ctx *context.Context) {
556556
ctx.ServerError("GetOAuth2ApplicationByClientID", err)
557557
return
558558
}
559-
grant, err := app.CreateGrant(ctx, ctx.Doer.ID, form.Scope)
559+
grant, err := app.GetGrantByUserID(ctx, ctx.Doer.ID)
560560
if err != nil {
561+
handleServerError(ctx, form.State, form.RedirectURI)
562+
return
563+
}
564+
if grant == nil {
565+
grant, err = app.CreateGrant(ctx, ctx.Doer.ID, form.Scope)
566+
if err != nil {
567+
handleAuthorizeError(ctx, AuthorizeError{
568+
State: form.State,
569+
ErrorDescription: "cannot create grant for user",
570+
ErrorCode: ErrorCodeServerError,
571+
}, form.RedirectURI)
572+
return
573+
}
574+
} else if grant.Scope != form.Scope {
561575
handleAuthorizeError(ctx, AuthorizeError{
562576
State: form.State,
563-
ErrorDescription: "cannot create grant for user",
577+
ErrorDescription: "a grant exists with different scope",
564578
ErrorCode: ErrorCodeServerError,
565579
}, form.RedirectURI)
566580
return
567581
}
582+
568583
if len(form.Nonce) > 0 {
569584
err := grant.SetNonce(ctx, form.Nonce)
570585
if err != nil {

0 commit comments

Comments
 (0)