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

11 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
10 years ago
11 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. if ctx.QueryBool("course") {
  60. ctx.Redirect(setting.AppSubURL + "/" + setting.Course.OrgName)
  61. return
  62. }
  63. }
  64. // Redirect to dashboard if user tries to visit any non-login page.
  65. if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
  66. ctx.Redirect(setting.AppSubURL + "/")
  67. return
  68. }
  69. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" && !auth.IsAPIPath(ctx.Req.URL.Path) {
  70. csrf.Validate(ctx.Context, ctx.csrf)
  71. if ctx.Written() {
  72. return
  73. }
  74. }
  75. if options.SignInRequired {
  76. if !ctx.IsSigned {
  77. // Restrict API calls with error message.
  78. if auth.IsAPIPath(ctx.Req.URL.Path) {
  79. ctx.JSON(403, map[string]string{
  80. "message": "Only signed in user is allowed to call APIs.",
  81. })
  82. return
  83. }
  84. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  85. ctx.Redirect(setting.AppSubURL + "/user/login")
  86. return
  87. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  88. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  89. ctx.HTML(200, "user/auth/activate")
  90. return
  91. }
  92. if ctx.IsSigned && auth.IsAPIPath(ctx.Req.URL.Path) && ctx.IsBasicAuth {
  93. twofa, err := models.GetTwoFactorByUID(ctx.User.ID)
  94. if err != nil {
  95. if models.IsErrTwoFactorNotEnrolled(err) {
  96. return // No 2FA enrollment for this user
  97. }
  98. ctx.Error(500)
  99. return
  100. }
  101. otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
  102. ok, err := twofa.ValidateTOTP(otpHeader)
  103. if err != nil {
  104. ctx.Error(500)
  105. return
  106. }
  107. if !ok {
  108. ctx.JSON(403, map[string]string{
  109. "message": "Only signed in user is allowed to call APIs.",
  110. })
  111. return
  112. }
  113. }
  114. }
  115. // Redirect to log in page if auto-signin info is provided and has not signed in.
  116. if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) &&
  117. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  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 options.AdminRequired {
  123. if !ctx.User.IsAdmin {
  124. ctx.Error(403)
  125. return
  126. }
  127. ctx.Data["PageIsAdmin"] = true
  128. }
  129. if options.BasicAuthRequired {
  130. if !basicAuth(ctx) {
  131. basicUnauthorized(ctx.Resp)
  132. return
  133. }
  134. }
  135. if options.OperationRequired {
  136. if !ctx.User.IsOperator {
  137. ctx.Error(403)
  138. return
  139. }
  140. ctx.Data["PageIsOperation"] = true
  141. }
  142. }
  143. }
  144. func basicAuth(ctx *Context) bool {
  145. var siteAuth = base64.StdEncoding.EncodeToString([]byte(setting.CBAuthUser + ":" + setting.CBAuthPassword))
  146. auth := ctx.Req.Header.Get("Authorization")
  147. if !marc_auth.SecureCompare(auth, "Basic "+siteAuth) {
  148. return false
  149. }
  150. return true
  151. }
  152. func basicUnauthorized(res http.ResponseWriter) {
  153. res.Header().Set("WWW-Authenticate", "Basic realm=\""+marc_auth.BasicRealm+"\"")
  154. http.Error(res, "Not Authorized", http.StatusUnauthorized)
  155. }