From c85e24e5f8ef0253261dea841dd2be31b3cc4bc0 Mon Sep 17 00:00:00 2001 From: Denys Konovalov Date: Sat, 18 May 2024 14:58:16 +0200 Subject: [PATCH 1/2] use existing oauth grant for public client --- routers/web/auth/oauth.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/routers/web/auth/oauth.go b/routers/web/auth/oauth.go index 84fa4730441f1..36e246795e176 100644 --- a/routers/web/auth/oauth.go +++ b/routers/web/auth/oauth.go @@ -556,15 +556,22 @@ func GrantApplicationOAuth(ctx *context.Context) { ctx.ServerError("GetOAuth2ApplicationByClientID", err) return } - grant, err := app.CreateGrant(ctx, ctx.Doer.ID, form.Scope) + grant, err := app.GetGrantByUserID(ctx, ctx.Doer.ID) if err != nil { - handleAuthorizeError(ctx, AuthorizeError{ - State: form.State, - ErrorDescription: "cannot create grant for user", - ErrorCode: ErrorCodeServerError, - }, form.RedirectURI) + handleServerError(ctx, form.State, form.RedirectURI) return } + if grant == nil { + grant, err = app.CreateGrant(ctx, ctx.Doer.ID, form.Scope) + if err != nil { + handleAuthorizeError(ctx, AuthorizeError{ + State: form.State, + ErrorDescription: "cannot create grant for user", + ErrorCode: ErrorCodeServerError, + }, form.RedirectURI) + return + } + } if len(form.Nonce) > 0 { err := grant.SetNonce(ctx, form.Nonce) if err != nil { From a9502eab8d8325ebc05a8c4d4047e96134f929c0 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 21 May 2024 13:35:35 +0800 Subject: [PATCH 2/2] Fix lint --- routers/web/auth/oauth.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/routers/web/auth/oauth.go b/routers/web/auth/oauth.go index 36e246795e176..b337b6b156959 100644 --- a/routers/web/auth/oauth.go +++ b/routers/web/auth/oauth.go @@ -571,7 +571,15 @@ func GrantApplicationOAuth(ctx *context.Context) { }, form.RedirectURI) return } + } else if grant.Scope != form.Scope { + handleAuthorizeError(ctx, AuthorizeError{ + State: form.State, + ErrorDescription: "a grant exists with different scope", + ErrorCode: ErrorCodeServerError, + }, form.RedirectURI) + return } + if len(form.Nonce) > 0 { err := grant.SetNonce(ctx, form.Nonce) if err != nil {