diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-12-28 13:53:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-28 13:53:28 +0800 |
commit | ca67c5a8a72f3d26bb0808ba7a63cd4875cd5229 (patch) | |
tree | 8190bc13c43dce1ad3aa78d29920b8e08c0931a8 /modules/context | |
parent | 7cc7db73b922143a1288d26b39eee9e8e59f7106 (diff) | |
download | gitea-ca67c5a8a72f3d26bb0808ba7a63cd4875cd5229.tar.gz gitea-ca67c5a8a72f3d26bb0808ba7a63cd4875cd5229.zip |
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
Diffstat (limited to 'modules/context')
-rw-r--r-- | modules/context/api.go | 8 | ||||
-rw-r--r-- | modules/context/context.go | 8 |
2 files changed, 14 insertions, 2 deletions
diff --git a/modules/context/api.go b/modules/context/api.go index 1349f91ec0..3f52c54d4c 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -219,7 +219,13 @@ func (ctx *APIContext) CheckForOTP() { func APIAuth(authMethod auth_service.Method) func(*APIContext) { return func(ctx *APIContext) { // Get user from session if logged in. - ctx.Doer = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) + var err error + ctx.Doer, err = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) + if err != nil { + ctx.Error(http.StatusUnauthorized, "APIAuth", err) + return + } + if ctx.Doer != nil { if ctx.Locale.Language() != ctx.Doer.Language { ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req) diff --git a/modules/context/context.go b/modules/context/context.go index 0fe00bf787..6273093060 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -662,7 +662,13 @@ func getCsrfOpts() CsrfOptions { // Auth converts auth.Auth as a middleware func Auth(authMethod auth.Method) func(*Context) { return func(ctx *Context) { - ctx.Doer = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) + var err error + ctx.Doer, err = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) + if err != nil { + log.Error("Failed to verify user %v: %v", ctx.Req.RemoteAddr, err) + ctx.Error(http.StatusUnauthorized, "Verify") + return + } if ctx.Doer != nil { if ctx.Locale.Language() != ctx.Doer.Language { ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req) |