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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package context
  6. import (
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "gitea.com/macaron/csrf"
  12. "gitea.com/macaron/macaron"
  13. )
  14. // ToggleOptions contains required or check options
  15. type ToggleOptions struct {
  16. SignInRequired bool
  17. SignOutRequired bool
  18. AdminRequired bool
  19. DisableCSRF bool
  20. }
  21. // Toggle returns toggle options as middleware
  22. func Toggle(options *ToggleOptions) macaron.Handler {
  23. return func(ctx *Context) {
  24. // Cannot view any page before installation.
  25. if !setting.InstallLock {
  26. ctx.Redirect(setting.AppSubURL + "/install")
  27. return
  28. }
  29. // Check prohibit login users.
  30. if ctx.IsSigned {
  31. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  32. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  33. ctx.HTML(200, "user/auth/activate")
  34. return
  35. } else if !ctx.User.IsActive || ctx.User.ProhibitLogin {
  36. log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
  37. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  38. ctx.HTML(200, "user/auth/prohibit_login")
  39. return
  40. }
  41. if ctx.User.MustChangePassword {
  42. if ctx.Req.URL.Path != "/user/settings/change_password" {
  43. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  44. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
  45. if ctx.Req.URL.Path != "/user/events" {
  46. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  47. }
  48. ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
  49. return
  50. }
  51. } else if ctx.Req.URL.Path == "/user/settings/change_password" {
  52. // make sure that the form cannot be accessed by users who don't need this
  53. ctx.Redirect(setting.AppSubURL + "/")
  54. return
  55. }
  56. }
  57. // Redirect to dashboard if user tries to visit any non-login page.
  58. if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
  59. ctx.Redirect(setting.AppSubURL + "/")
  60. return
  61. }
  62. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" && !auth.IsAPIPath(ctx.Req.URL.Path) {
  63. csrf.Validate(ctx.Context, ctx.csrf)
  64. if ctx.Written() {
  65. return
  66. }
  67. }
  68. if options.SignInRequired {
  69. if !ctx.IsSigned {
  70. // Restrict API calls with error message.
  71. if auth.IsAPIPath(ctx.Req.URL.Path) {
  72. ctx.JSON(403, map[string]string{
  73. "message": "Only signed in user is allowed to call APIs.",
  74. })
  75. return
  76. }
  77. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  78. ctx.Redirect(setting.AppSubURL + "/user/login")
  79. return
  80. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  81. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  82. ctx.HTML(200, "user/auth/activate")
  83. return
  84. }
  85. if ctx.IsSigned && auth.IsAPIPath(ctx.Req.URL.Path) && ctx.IsBasicAuth {
  86. twofa, err := models.GetTwoFactorByUID(ctx.User.ID)
  87. if err != nil {
  88. if models.IsErrTwoFactorNotEnrolled(err) {
  89. return // No 2FA enrollment for this user
  90. }
  91. ctx.Error(500)
  92. return
  93. }
  94. otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
  95. ok, err := twofa.ValidateTOTP(otpHeader)
  96. if err != nil {
  97. ctx.Error(500)
  98. return
  99. }
  100. if !ok {
  101. ctx.JSON(403, map[string]string{
  102. "message": "Only signed in user is allowed to call APIs.",
  103. })
  104. return
  105. }
  106. }
  107. }
  108. // Redirect to log in page if auto-signin info is provided and has not signed in.
  109. if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) &&
  110. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  111. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  112. ctx.Redirect(setting.AppSubURL + "/user/login")
  113. return
  114. }
  115. if options.AdminRequired {
  116. if !ctx.User.IsAdmin {
  117. ctx.Error(403)
  118. return
  119. }
  120. ctx.Data["PageIsAdmin"] = true
  121. }
  122. }
  123. }