auth.go 3.0 KB

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