auth.go 2.9 KB

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