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)
| 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. |
| 194 | func 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 | } |
no test coverage detected