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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/web/middleware"
  12. )
  13. // ToggleOptions contains required or check options
  14. type ToggleOptions struct {
  15. SignInRequired bool
  16. SignOutRequired bool
  17. AdminRequired bool
  18. DisableCSRF bool
  19. }
  20. // Toggle returns toggle options as middleware
  21. func Toggle(options *ToggleOptions) func(ctx *Context) {
  22. return func(ctx *Context) {
  23. // Check prohibit login users.
  24. if ctx.IsSigned {
  25. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  26. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  27. ctx.HTML(http.StatusOK, "user/auth/activate")
  28. return
  29. }
  30. if !ctx.User.IsActive || ctx.User.ProhibitLogin {
  31. log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
  32. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  33. ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
  34. return
  35. }
  36. if ctx.User.MustChangePassword {
  37. if ctx.Req.URL.Path != "/user/settings/change_password" {
  38. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  39. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
  40. if ctx.Req.URL.Path != "/user/events" {
  41. middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
  42. }
  43. ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
  44. return
  45. }
  46. } else if ctx.Req.URL.Path == "/user/settings/change_password" {
  47. // make sure that the form cannot be accessed by users who don't need this
  48. ctx.Redirect(setting.AppSubURL + "/")
  49. return
  50. }
  51. }
  52. // Redirect to dashboard if user tries to visit any non-login page.
  53. if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
  54. ctx.Redirect(setting.AppSubURL + "/")
  55. return
  56. }
  57. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" {
  58. Validate(ctx, ctx.csrf)
  59. if ctx.Written() {
  60. return
  61. }
  62. }
  63. if options.SignInRequired {
  64. if !ctx.IsSigned {
  65. if ctx.Req.URL.Path != "/user/events" {
  66. middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
  67. }
  68. ctx.Redirect(setting.AppSubURL + "/user/login")
  69. return
  70. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  71. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  72. ctx.HTML(http.StatusOK, "user/auth/activate")
  73. return
  74. }
  75. }
  76. // Redirect to log in page if auto-signin info is provided and has not signed in.
  77. if !options.SignOutRequired && !ctx.IsSigned &&
  78. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  79. if ctx.Req.URL.Path != "/user/events" {
  80. middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
  81. }
  82. ctx.Redirect(setting.AppSubURL + "/user/login")
  83. return
  84. }
  85. if options.AdminRequired {
  86. if !ctx.User.IsAdmin {
  87. ctx.Error(http.StatusForbidden)
  88. return
  89. }
  90. ctx.Data["PageIsAdmin"] = true
  91. }
  92. }
  93. }
  94. // ToggleAPI returns toggle options as middleware
  95. func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
  96. return func(ctx *APIContext) {
  97. // Check prohibit login users.
  98. if ctx.IsSigned {
  99. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  100. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  101. ctx.JSON(http.StatusForbidden, map[string]string{
  102. "message": "This account is not activated.",
  103. })
  104. return
  105. }
  106. if !ctx.User.IsActive || ctx.User.ProhibitLogin {
  107. log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
  108. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  109. ctx.JSON(http.StatusForbidden, map[string]string{
  110. "message": "This account is prohibited from signing in, please contact your site administrator.",
  111. })
  112. return
  113. }
  114. if ctx.User.MustChangePassword {
  115. ctx.JSON(http.StatusForbidden, map[string]string{
  116. "message": "You must change your password. Change it at: " + setting.AppURL + "/user/change_password",
  117. })
  118. return
  119. }
  120. }
  121. // Redirect to dashboard if user tries to visit any non-login page.
  122. if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
  123. ctx.Redirect(setting.AppSubURL + "/")
  124. return
  125. }
  126. if options.SignInRequired {
  127. if !ctx.IsSigned {
  128. // Restrict API calls with error message.
  129. ctx.JSON(http.StatusForbidden, map[string]string{
  130. "message": "Only signed in user is allowed to call APIs.",
  131. })
  132. return
  133. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  134. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  135. ctx.HTML(http.StatusOK, "user/auth/activate")
  136. return
  137. }
  138. if ctx.IsSigned && ctx.IsBasicAuth {
  139. twofa, err := models.GetTwoFactorByUID(ctx.User.ID)
  140. if err != nil {
  141. if models.IsErrTwoFactorNotEnrolled(err) {
  142. return // No 2FA enrollment for this user
  143. }
  144. ctx.InternalServerError(err)
  145. return
  146. }
  147. otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
  148. ok, err := twofa.ValidateTOTP(otpHeader)
  149. if err != nil {
  150. ctx.InternalServerError(err)
  151. return
  152. }
  153. if !ok {
  154. ctx.JSON(http.StatusForbidden, map[string]string{
  155. "message": "Only signed in user is allowed to call APIs.",
  156. })
  157. return
  158. }
  159. }
  160. }
  161. if options.AdminRequired {
  162. if !ctx.User.IsAdmin {
  163. ctx.JSON(http.StatusForbidden, map[string]string{
  164. "message": "You have no permission to request for this.",
  165. })
  166. return
  167. }
  168. ctx.Data["PageIsAdmin"] = true
  169. }
  170. }
  171. }