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