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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "net/http"
  8. "strings"
  9. "code.gitea.io/gitea/models/auth"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/web/middleware"
  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) func(ctx *Context) {
  23. return func(ctx *Context) {
  24. // Check prohibit login users.
  25. if ctx.IsSigned {
  26. if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
  27. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  28. ctx.HTML(http.StatusOK, "user/auth/activate")
  29. return
  30. }
  31. if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin {
  32. log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr())
  33. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  34. ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
  35. return
  36. }
  37. if ctx.Doer.MustChangePassword {
  38. if ctx.Req.URL.Path != "/user/settings/change_password" {
  39. if strings.HasPrefix(ctx.Req.UserAgent(), "git") {
  40. ctx.Error(http.StatusUnauthorized, ctx.Tr("auth.must_change_password"))
  41. return
  42. }
  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. middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
  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" {
  63. ctx.csrf.Validate(ctx)
  64. if ctx.Written() {
  65. return
  66. }
  67. }
  68. if options.SignInRequired {
  69. if !ctx.IsSigned {
  70. if ctx.Req.URL.Path != "/user/events" {
  71. middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
  72. }
  73. ctx.Redirect(setting.AppSubURL + "/user/login")
  74. return
  75. } else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
  76. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  77. ctx.HTML(http.StatusOK, "user/auth/activate")
  78. return
  79. }
  80. }
  81. // Redirect to log in page if auto-signin info is provided and has not signed in.
  82. if !options.SignOutRequired && !ctx.IsSigned &&
  83. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  84. if ctx.Req.URL.Path != "/user/events" {
  85. middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
  86. }
  87. ctx.Redirect(setting.AppSubURL + "/user/login")
  88. return
  89. }
  90. if options.AdminRequired {
  91. if !ctx.Doer.IsAdmin {
  92. ctx.Error(http.StatusForbidden)
  93. return
  94. }
  95. ctx.Data["PageIsAdmin"] = true
  96. }
  97. }
  98. }
  99. // ToggleAPI returns toggle options as middleware
  100. func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
  101. return func(ctx *APIContext) {
  102. // Check prohibit login users.
  103. if ctx.IsSigned {
  104. if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
  105. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  106. ctx.JSON(http.StatusForbidden, map[string]string{
  107. "message": "This account is not activated.",
  108. })
  109. return
  110. }
  111. if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin {
  112. log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr())
  113. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  114. ctx.JSON(http.StatusForbidden, map[string]string{
  115. "message": "This account is prohibited from signing in, please contact your site administrator.",
  116. })
  117. return
  118. }
  119. if ctx.Doer.MustChangePassword {
  120. ctx.JSON(http.StatusForbidden, map[string]string{
  121. "message": "You must change your password. Change it at: " + setting.AppURL + "/user/change_password",
  122. })
  123. return
  124. }
  125. }
  126. // Redirect to dashboard if user tries to visit any non-login page.
  127. if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
  128. ctx.Redirect(setting.AppSubURL + "/")
  129. return
  130. }
  131. if options.SignInRequired {
  132. if !ctx.IsSigned {
  133. // Restrict API calls with error message.
  134. ctx.JSON(http.StatusForbidden, map[string]string{
  135. "message": "Only signed in user is allowed to call APIs.",
  136. })
  137. return
  138. } else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
  139. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  140. ctx.HTML(http.StatusOK, "user/auth/activate")
  141. return
  142. }
  143. if ctx.IsSigned && ctx.IsBasicAuth {
  144. if skip, ok := ctx.Data["SkipLocalTwoFA"]; ok && skip.(bool) {
  145. return // Skip 2FA
  146. }
  147. twofa, err := auth.GetTwoFactorByUID(ctx.Doer.ID)
  148. if err != nil {
  149. if auth.IsErrTwoFactorNotEnrolled(err) {
  150. return // No 2FA enrollment for this user
  151. }
  152. ctx.InternalServerError(err)
  153. return
  154. }
  155. otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
  156. ok, err := twofa.ValidateTOTP(otpHeader)
  157. if err != nil {
  158. ctx.InternalServerError(err)
  159. return
  160. }
  161. if !ok {
  162. ctx.JSON(http.StatusForbidden, map[string]string{
  163. "message": "Only signed in user is allowed to call APIs.",
  164. })
  165. return
  166. }
  167. }
  168. }
  169. if options.AdminRequired {
  170. if !ctx.Doer.IsAdmin {
  171. ctx.JSON(http.StatusForbidden, map[string]string{
  172. "message": "You have no permission to request for this.",
  173. })
  174. return
  175. }
  176. ctx.Data["PageIsAdmin"] = true
  177. }
  178. }
  179. }