2023-09-12 08:15:16 +02:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
2025-03-27 19:40:14 +00:00
|
|
|
"forgejo.org/modules/web/middleware"
|
|
|
|
|
auth_service "forgejo.org/services/auth"
|
|
|
|
|
"forgejo.org/services/context"
|
2023-09-12 08:15:16 +02:00
|
|
|
)
|
|
|
|
|
|
2026-05-08 07:31:33 +02:00
|
|
|
func AuthShared(ctx *context.Base, sessionStore auth_service.SessionStore, authMethod auth_service.Method) auth_service.MethodOutput {
|
|
|
|
|
output := authMethod.Verify(ctx.Req, ctx.Resp, sessionStore)
|
|
|
|
|
|
|
|
|
|
var ar auth_service.AuthenticationResult
|
|
|
|
|
switch v := output.(type) {
|
|
|
|
|
case *auth_service.AuthenticationSuccess:
|
|
|
|
|
ar = v.Result
|
|
|
|
|
case *auth_service.AuthenticationNotAttempted:
|
|
|
|
|
ar = &auth_service.UnauthenticatedResult{}
|
2026-05-08 04:07:32 +02:00
|
|
|
}
|
2026-05-08 07:31:33 +02:00
|
|
|
if ar != nil {
|
|
|
|
|
doer := ar.User()
|
|
|
|
|
if doer != nil {
|
|
|
|
|
if ctx.Locale.Language() != doer.Language {
|
|
|
|
|
ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req)
|
|
|
|
|
}
|
|
|
|
|
ctx.Data["IsSigned"] = true
|
|
|
|
|
ctx.Data[middleware.ContextDataKeySignedUser] = doer
|
|
|
|
|
ctx.Data["SignedUserID"] = doer.ID
|
|
|
|
|
ctx.Data["IsAdmin"] = doer.IsAdmin
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Data["IsSigned"] = false
|
|
|
|
|
ctx.Data["SignedUserID"] = int64(0)
|
2023-09-12 08:15:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-08 07:31:33 +02:00
|
|
|
return output
|
2023-09-12 08:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VerifyOptions contains required or check options
|
|
|
|
|
type VerifyOptions struct {
|
|
|
|
|
SignInRequired bool
|
|
|
|
|
SignOutRequired bool
|
|
|
|
|
AdminRequired bool
|
|
|
|
|
DisableCSRF bool
|
|
|
|
|
}
|