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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/setting"
  9. "github.com/go-macaron/csrf"
  10. macaron "gopkg.in/macaron.v1"
  11. )
  12. // ToggleOptions contains required or check options
  13. type ToggleOptions struct {
  14. SignInRequired bool
  15. SignOutRequired bool
  16. AdminRequired bool
  17. DisableCSRF bool
  18. }
  19. // Toggle returns toggle options as middleware
  20. func Toggle(options *ToggleOptions) macaron.Handler {
  21. return func(ctx *Context) {
  22. // Cannot view any page before installation.
  23. if !setting.InstallLock {
  24. ctx.Redirect(setting.AppSubURL + "/install")
  25. return
  26. }
  27. // Check prohibit login users.
  28. if ctx.IsSigned && ctx.User.ProhibitLogin {
  29. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  30. ctx.HTML(200, "user/auth/prohibit_login")
  31. return
  32. }
  33. // Redirect to dashboard if user tries to visit any non-login page.
  34. if options.SignOutRequired && ctx.IsSigned && ctx.Req.RequestURI != "/" {
  35. ctx.Redirect(setting.AppSubURL + "/")
  36. return
  37. }
  38. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" && !auth.IsAPIPath(ctx.Req.URL.Path) {
  39. csrf.Validate(ctx.Context, ctx.csrf)
  40. if ctx.Written() {
  41. return
  42. }
  43. }
  44. if options.SignInRequired {
  45. if !ctx.IsSigned {
  46. // Restrict API calls with error message.
  47. if auth.IsAPIPath(ctx.Req.URL.Path) {
  48. ctx.JSON(403, map[string]string{
  49. "message": "Only signed in user is allowed to call APIs.",
  50. })
  51. return
  52. }
  53. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  54. ctx.Redirect(setting.AppSubURL + "/user/login")
  55. return
  56. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  57. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  58. ctx.HTML(200, "user/auth/activate")
  59. return
  60. }
  61. }
  62. // Redirect to log in page if auto-signin info is provided and has not signed in.
  63. if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) &&
  64. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  65. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  66. ctx.Redirect(setting.AppSubURL + "/user/login")
  67. return
  68. }
  69. if options.AdminRequired {
  70. if !ctx.User.IsAdmin {
  71. ctx.Error(403)
  72. return
  73. }
  74. ctx.Data["PageIsAdmin"] = true
  75. }
  76. }
  77. }