You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.go 1.2KB

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