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.1 kB

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