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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "net/url"
  7. "code.gitea.io/gitea/modules/auth"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/go-macaron/csrf"
  11. macaron "gopkg.in/macaron.v1"
  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) macaron.Handler {
  22. return func(ctx *Context) {
  23. // Cannot view any page before installation.
  24. if !setting.InstallLock {
  25. ctx.Redirect(setting.AppSubURL + "/install")
  26. return
  27. }
  28. // Check prohibit login users.
  29. if ctx.IsSigned {
  30. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  31. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  32. ctx.HTML(200, "user/auth/activate")
  33. return
  34. } else if !ctx.User.IsActive || ctx.User.ProhibitLogin {
  35. log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
  36. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  37. ctx.HTML(200, "user/auth/prohibit_login")
  38. return
  39. }
  40. // prevent infinite redirection
  41. // also make sure that the form cannot be accessed by
  42. // users who don't need this
  43. if ctx.Req.URL.Path == "/user/settings/change_password" {
  44. if !ctx.User.MustChangePassword {
  45. ctx.Redirect(setting.AppSubURL + "/")
  46. }
  47. return
  48. }
  49. if ctx.User.MustChangePassword {
  50. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  51. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
  52. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  53. ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
  54. return
  55. }
  56. }
  57. // Redirect to dashboard if user tries to visit any non-login page.
  58. if options.SignOutRequired && ctx.IsSigned && ctx.Req.RequestURI != "/" {
  59. ctx.Redirect(setting.AppSubURL + "/")
  60. return
  61. }
  62. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" && !auth.IsAPIPath(ctx.Req.URL.Path) {
  63. csrf.Validate(ctx.Context, ctx.csrf)
  64. if ctx.Written() {
  65. return
  66. }
  67. }
  68. if options.SignInRequired {
  69. if !ctx.IsSigned {
  70. // Restrict API calls with error message.
  71. if auth.IsAPIPath(ctx.Req.URL.Path) {
  72. ctx.JSON(403, map[string]string{
  73. "message": "Only signed in user is allowed to call APIs.",
  74. })
  75. return
  76. }
  77. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  78. ctx.Redirect(setting.AppSubURL + "/user/login")
  79. return
  80. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  81. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  82. ctx.HTML(200, "user/auth/activate")
  83. return
  84. }
  85. }
  86. // Redirect to log in page if auto-signin info is provided and has not signed in.
  87. if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) &&
  88. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  89. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  90. ctx.Redirect(setting.AppSubURL + "/user/login")
  91. return
  92. }
  93. if options.AdminRequired {
  94. if !ctx.User.IsAdmin {
  95. ctx.Error(403)
  96. return
  97. }
  98. ctx.Data["PageIsAdmin"] = true
  99. }
  100. }
  101. }