code.gitea.io/gitea@v1.21.7/routers/common/auth.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package common 5 6 import ( 7 user_model "code.gitea.io/gitea/models/user" 8 "code.gitea.io/gitea/modules/context" 9 "code.gitea.io/gitea/modules/web/middleware" 10 auth_service "code.gitea.io/gitea/services/auth" 11 ) 12 13 type AuthResult struct { 14 Doer *user_model.User 15 IsBasicAuth bool 16 } 17 18 func AuthShared(ctx *context.Base, sessionStore auth_service.SessionStore, authMethod auth_service.Method) (ar AuthResult, err error) { 19 ar.Doer, err = authMethod.Verify(ctx.Req, ctx.Resp, ctx, sessionStore) 20 if err != nil { 21 return ar, err 22 } 23 if ar.Doer != nil { 24 if ctx.Locale.Language() != ar.Doer.Language { 25 ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req) 26 } 27 ar.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == auth_service.BasicMethodName 28 29 ctx.Data["IsSigned"] = true 30 ctx.Data[middleware.ContextDataKeySignedUser] = ar.Doer 31 ctx.Data["SignedUserID"] = ar.Doer.ID 32 ctx.Data["IsAdmin"] = ar.Doer.IsAdmin 33 } else { 34 ctx.Data["SignedUserID"] = int64(0) 35 } 36 return ar, nil 37 } 38 39 // VerifyOptions contains required or check options 40 type VerifyOptions struct { 41 SignInRequired bool 42 SignOutRequired bool 43 AdminRequired bool 44 DisableCSRF bool 45 }