auth.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package context
  7. import (
  8. "gitote/gitote/pkg/auth"
  9. "gitote/gitote/pkg/setting"
  10. "gitote/gitote/pkg/tool"
  11. "net/http"
  12. "net/url"
  13. "strings"
  14. "github.com/go-macaron/csrf"
  15. "gopkg.in/macaron.v1"
  16. )
  17. type ToggleOptions struct {
  18. SignInRequired bool
  19. SignOutRequired bool
  20. AdminRequired bool
  21. DisableCSRF bool
  22. }
  23. func Toggle(options *ToggleOptions) macaron.Handler {
  24. return func(c *Context) {
  25. // Cannot view any page before installation.
  26. if !setting.InstallLock {
  27. c.Redirect(setting.AppSubURL + "/install")
  28. return
  29. }
  30. // Check prohibit login users.
  31. if c.IsLogged && c.User.Suspended {
  32. c.Data["Title"] = c.Tr("auth.prohibit_login")
  33. c.Data["PageIsSuspended"] = true
  34. c.HTML(200, "user/auth/prohibit_login")
  35. return
  36. }
  37. // Check non-logged users landing page.
  38. if !c.IsLogged && c.Req.RequestURI == "/" && setting.LandingPageURL != setting.LANDING_PAGE_HOME {
  39. c.Redirect(setting.AppSubURL + string(setting.LandingPageURL))
  40. return
  41. }
  42. // Redirect to dashboard if user tries to visit any non-login page.
  43. if options.SignOutRequired && c.IsLogged && c.Req.RequestURI != "/" {
  44. c.Redirect(setting.AppSubURL + "/")
  45. return
  46. }
  47. if !options.SignOutRequired && !options.DisableCSRF && c.Req.Method == "POST" && !auth.IsAPIPath(c.Req.URL.Path) {
  48. csrf.Validate(c.Context, c.csrf)
  49. if c.Written() {
  50. return
  51. }
  52. }
  53. if options.SignInRequired {
  54. if !c.IsLogged {
  55. // Restrict API calls with error message.
  56. if auth.IsAPIPath(c.Req.URL.Path) {
  57. c.JSON(403, map[string]string{
  58. "message": "Only signed in user is allowed to call APIs.",
  59. })
  60. return
  61. }
  62. c.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+c.Req.RequestURI), 0, setting.AppSubURL)
  63. c.Redirect(setting.AppSubURL + "/login")
  64. return
  65. } else if !c.User.IsActive && setting.Service.RegisterEmailConfirm {
  66. c.Data["Title"] = c.Tr("auth.active_your_account")
  67. c.HTML(200, "user/auth/activate")
  68. return
  69. }
  70. }
  71. // Redirect to log in page if auto-signin info is provided and has not signed in.
  72. if !options.SignOutRequired && !c.IsLogged && !auth.IsAPIPath(c.Req.URL.Path) &&
  73. len(c.GetCookie(setting.CookieUserName)) > 0 {
  74. c.SetCookie("redirect_to", url.QueryEscape(setting.AppSubURL+c.Req.RequestURI), 0, setting.AppSubURL)
  75. c.Redirect(setting.AppSubURL + "/login")
  76. return
  77. }
  78. if options.AdminRequired {
  79. if !c.User.IsAdmin {
  80. c.Redirect(setting.AppSubURL + "/")
  81. return
  82. }
  83. c.Data["PageIsAdmin"] = true
  84. }
  85. }
  86. }
  87. // RequireBasicAuth verifies HTTP Basic Authentication header with given credentials
  88. func (c *Context) RequireBasicAuth(username, password string) {
  89. fields := strings.Fields(c.Req.Header.Get("Authorization"))
  90. if len(fields) != 2 || fields[0] != "Basic" {
  91. c.Status(http.StatusUnauthorized)
  92. return
  93. }
  94. uname, passwd, _ := tool.BasicAuthDecode(fields[1])
  95. if uname != username || passwd != password {
  96. c.Status(http.StatusForbidden)
  97. return
  98. }
  99. }