Skip to content

Commit 07fe5a8

Browse files
denyskonlunny
authored andcommitted
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 go-gitea/gitea#30790 (comment) --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> (cherry picked from commit 9c8c9ff6d10b35de8d2d7eae0fc2646ad9bbe94a)
1 parent 7d7ea45 commit 07fe5a8

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

routers/web/auth/oauth.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,15 +557,30 @@ func GrantApplicationOAuth(ctx *context.Context) {
557557
ctx.ServerError("GetOAuth2ApplicationByClientID", err)
558558
return
559559
}
560-
grant, err := app.CreateGrant(ctx, ctx.Doer.ID, form.Scope)
560+
grant, err := app.GetGrantByUserID(ctx, ctx.Doer.ID)
561561
if err != nil {
562+
handleServerError(ctx, form.State, form.RedirectURI)
563+
return
564+
}
565+
if grant == nil {
566+
grant, err = app.CreateGrant(ctx, ctx.Doer.ID, form.Scope)
567+
if err != nil {
568+
handleAuthorizeError(ctx, AuthorizeError{
569+
State: form.State,
570+
ErrorDescription: "cannot create grant for user",
571+
ErrorCode: ErrorCodeServerError,
572+
}, form.RedirectURI)
573+
return
574+
}
575+
} else if grant.Scope != form.Scope {
562576
handleAuthorizeError(ctx, AuthorizeError{
563577
State: form.State,
564-
ErrorDescription: "cannot create grant for user",
578+
ErrorDescription: "a grant exists with different scope",
565579
ErrorCode: ErrorCodeServerError,
566580
}, form.RedirectURI)
567581
return
568582
}
583+
569584
if len(form.Nonce) > 0 {
570585
err := grant.SetNonce(ctx, form.Nonce)
571586
if err != nil {

0 commit comments

Comments
 (0)