MCPcopy Create free account
hub / github.com/gogs/gogs / authenticatedUser

Function authenticatedUser

internal/context/auth.go:194–259  ·  view source on GitHub ↗

authenticatedUser returns the user object of the authenticated user, along with two bool values which indicate whether the user uses HTTP Basic Authentication or token authentication respectively.

(store AuthStore, ctx *macaron.Context, sess session.Store)

Source from the content-addressed store, hash-verified

192// authenticatedUser returns the user object of the authenticated user, along with two bool values
193// which indicate whether the user uses HTTP Basic Authentication or token authentication respectively.
194func authenticatedUser(store AuthStore, ctx *macaron.Context, sess session.Store) (_ *database.User, isBasicAuth, isTokenAuth bool) {
195 if !database.HasEngine {
196 return nil, false, false
197 }
198
199 uid, isTokenAuth := authenticatedUserID(store, ctx, sess)
200
201 if uid <= 0 {
202 if conf.Auth.EnableReverseProxyAuthentication && isRequestFromTrustedProxy(ctx.Req.Request) {
203 webAuthUser := ctx.Req.Header.Get(conf.Auth.ReverseProxyAuthenticationHeader)
204 if len(webAuthUser) > 0 {
205 user, err := store.GetUserByUsername(ctx.Req.Context(), webAuthUser)
206 if err != nil {
207 if !database.IsErrUserNotExist(err) {
208 log.Error("Failed to get user by name: %v", err)
209 return nil, false, false
210 }
211
212 // Check if enabled auto-registration.
213 if conf.Auth.EnableReverseProxyAutoRegistration {
214 user, err = store.CreateUser(
215 ctx.Req.Context(),
216 webAuthUser,
217 gouuid.NewV4().String()+"@localhost",
218 database.CreateUserOptions{
219 Activated: true,
220 },
221 )
222 if err != nil {
223 log.Error("Failed to create user %q: %v", webAuthUser, err)
224 return nil, false, false
225 }
226 }
227 }
228 return user, false, false
229 }
230 }
231
232 // Check with basic auth.
233 baHead := ctx.Req.Header.Get("Authorization")
234 if len(baHead) > 0 {
235 auths := strings.Fields(baHead)
236 if len(auths) == 2 && auths[0] == "Basic" {
237 uname, passwd, _ := tool.BasicAuthDecode(auths[1])
238
239 u, err := store.AuthenticateUser(ctx.Req.Context(), uname, passwd, -1)
240 if err != nil {
241 if !auth.IsErrBadCredentials(err) {
242 log.Error("Failed to authenticate user: %v", err)
243 }
244 return nil, false, false
245 }
246
247 return u, true, false
248 }
249 }
250 return nil, false, false
251 }

Callers 1

ContexterFunction · 0.85

Calls 12

IsErrUserNotExistFunction · 0.92
BasicAuthDecodeFunction · 0.92
IsErrBadCredentialsFunction · 0.92
authenticatedUserIDFunction · 0.85
GetMethod · 0.65
GetUserByUsernameMethod · 0.65
CreateUserMethod · 0.65
AuthenticateUserMethod · 0.65
GetUserByIDMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected