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 3.5 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
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if ctx.User.MustChangePassword {
  41. if ctx.Req.URL.Path != "/user/settings/change_password" {
  42. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  43. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
  44. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  45. ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
  46. return
  47. }
  48. } else if ctx.Req.URL.Path == "/user/settings/change_password" {
  49. // make sure that the form cannot be accessed by users who don't need this
  50. ctx.Redirect(setting.AppSubURL + "/")
  51. return
  52. }
  53. }
  54. // Redirect to dashboard if user tries to visit any non-login page.
  55. if options.SignOutRequired && ctx.IsSigned && ctx.Req.RequestURI != "/" {
  56. ctx.Redirect(setting.AppSubURL + "/")
  57. return
  58. }
  59. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" && !auth.IsAPIPath(ctx.Req.URL.Path) {
  60. csrf.Validate(ctx.Context, ctx.csrf)
  61. if ctx.Written() {
  62. return
  63. }
  64. }
  65. if options.SignInRequired {
  66. if !ctx.IsSigned {
  67. // Restrict API calls with error message.
  68. if auth.IsAPIPath(ctx.Req.URL.Path) {
  69. ctx.JSON(403, map[string]string{
  70. "message": "Only signed in user is allowed to call APIs.",
  71. })
  72. return
  73. }
  74. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  75. ctx.Redirect(setting.AppSubURL + "/user/login")
  76. return
  77. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  78. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  79. ctx.HTML(200, "user/auth/activate")
  80. return
  81. }
  82. }
  83. // Redirect to log in page if auto-signin info is provided and has not signed in.
  84. if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) &&
  85. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  86. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  87. ctx.Redirect(setting.AppSubURL + "/user/login")
  88. return
  89. }
  90. if options.AdminRequired {
  91. if !ctx.User.IsAdmin {
  92. ctx.Error(403)
  93. return
  94. }
  95. ctx.Data["PageIsAdmin"] = true
  96. }
  97. }
  98. }