Skip to content

Commit 9c8c9ff

Browse files
denyskonlunny
andauthored
use existing oauth grant for public client (#31015)
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: Lunny Xiao <xiaolunwen@gmail.com>
1 parent c6cf96d commit 9c8c9ff

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
@@ -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)