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

auth.go 5.6 kB

11 years ago
3 years ago
11 years ago
11 years ago
11 years ago
3 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
3 years ago
10 years ago
11 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. "encoding/base64"
  12. "net/http"
  13. "gitea.com/macaron/csrf"
  14. "gitea.com/macaron/macaron"
  15. marc_auth "github.com/go-macaron/auth"
  16. )
  17. // ToggleOptions contains required or check options
  18. type ToggleOptions struct {
  19. SignInRequired bool
  20. SignOutRequired bool
  21. AdminRequired bool
  22. DisableCSRF bool
  23. BasicAuthRequired bool
  24. OperationRequired bool
  25. WechatAuthRequired bool
  26. }
  27. // Toggle returns toggle options as middleware
  28. func Toggle(options *ToggleOptions) macaron.Handler {
  29. return func(ctx *Context) {
  30. // Cannot view any page before installation.
  31. if !setting.InstallLock {
  32. ctx.Redirect(setting.AppSubURL + "/install")
  33. return
  34. }
  35. // Check prohibit login users.
  36. if ctx.IsSigned {
  37. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  38. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  39. ctx.HTML(200, "user/auth/activate")
  40. return
  41. } else if !ctx.User.IsActive || ctx.User.ProhibitLogin {
  42. log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
  43. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  44. ctx.HTML(200, "user/auth/prohibit_login")
  45. return
  46. }
  47. if ctx.User.MustChangePassword {
  48. if ctx.Req.URL.Path != "/user/settings/change_password" {
  49. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  50. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
  51. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  52. ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
  53. return
  54. }
  55. } else if ctx.Req.URL.Path == "/user/settings/change_password" {
  56. // make sure that the form cannot be accessed by users who don't need this
  57. ctx.Redirect(setting.AppSubURL + "/")
  58. return
  59. }
  60. if ctx.QueryBool("course") {
  61. ctx.Redirect(setting.AppSubURL + "/" + setting.Course.OrgName)
  62. return
  63. }
  64. }
  65. // Redirect to dashboard if user tries to visit any non-login page.
  66. if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
  67. ctx.Redirect(setting.AppSubURL + "/")
  68. return
  69. }
  70. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" && !auth.IsAPIPath(ctx.Req.URL.Path) {
  71. csrf.Validate(ctx.Context, ctx.csrf)
  72. if ctx.Written() {
  73. return
  74. }
  75. }
  76. if options.SignInRequired {
  77. if !ctx.IsSigned {
  78. // Restrict API calls with error message.
  79. if auth.IsAPIPath(ctx.Req.URL.Path) {
  80. ctx.JSON(403, map[string]string{
  81. "message": "Only signed in user is allowed to call APIs.",
  82. })
  83. return
  84. }
  85. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  86. ctx.Redirect(setting.AppSubURL + "/user/login")
  87. return
  88. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  89. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  90. ctx.HTML(200, "user/auth/activate")
  91. return
  92. }
  93. if ctx.IsSigned && auth.IsAPIPath(ctx.Req.URL.Path) && ctx.IsBasicAuth {
  94. twofa, err := models.GetTwoFactorByUID(ctx.User.ID)
  95. if err != nil {
  96. if models.IsErrTwoFactorNotEnrolled(err) {
  97. return // No 2FA enrollment for this user
  98. }
  99. ctx.Error(500)
  100. return
  101. }
  102. otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
  103. ok, err := twofa.ValidateTOTP(otpHeader)
  104. if err != nil {
  105. ctx.Error(500)
  106. return
  107. }
  108. if !ok {
  109. ctx.JSON(403, map[string]string{
  110. "message": "Only signed in user is allowed to call APIs.",
  111. })
  112. return
  113. }
  114. }
  115. }
  116. if options.WechatAuthRequired {
  117. if !ctx.IsSigned {
  118. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  119. ctx.Redirect(setting.AppSubURL + "/user/login")
  120. return
  121. }
  122. if ctx.User.WechatOpenId == "" {
  123. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  124. ctx.Redirect(setting.AppSubURL + "/explore/users")
  125. }
  126. }
  127. // Redirect to log in page if auto-signin info is provided and has not signed in.
  128. if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) &&
  129. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  130. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  131. ctx.Redirect(setting.AppSubURL + "/user/login")
  132. return
  133. }
  134. if options.AdminRequired {
  135. if !ctx.User.IsAdmin {
  136. ctx.Error(403)
  137. return
  138. }
  139. ctx.Data["PageIsAdmin"] = true
  140. }
  141. if options.BasicAuthRequired {
  142. if !basicAuth(ctx) {
  143. basicUnauthorized(ctx.Resp)
  144. return
  145. }
  146. }
  147. if options.OperationRequired {
  148. if !ctx.User.IsOperator {
  149. ctx.Error(403)
  150. return
  151. }
  152. ctx.Data["PageIsOperation"] = true
  153. }
  154. }
  155. }
  156. func basicAuth(ctx *Context) bool {
  157. var siteAuth = base64.StdEncoding.EncodeToString([]byte(setting.CBAuthUser + ":" + setting.CBAuthPassword))
  158. auth := ctx.Req.Header.Get("Authorization")
  159. if !marc_auth.SecureCompare(auth, "Basic "+siteAuth) {
  160. return false
  161. }
  162. return true
  163. }
  164. func basicUnauthorized(res http.ResponseWriter) {
  165. res.Header().Set("WWW-Authenticate", "Basic realm=\""+marc_auth.BasicRealm+"\"")
  166. http.Error(res, "Not Authorized", http.StatusUnauthorized)
  167. }