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.

reverseproxy.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package auth
  6. import (
  7. "net/http"
  8. "strings"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/web/middleware"
  13. "code.gitea.io/gitea/services/mailer"
  14. gouuid "github.com/google/uuid"
  15. )
  16. // Ensure the struct implements the interface.
  17. var (
  18. _ Method = &ReverseProxy{}
  19. _ Named = &ReverseProxy{}
  20. )
  21. // ReverseProxyMethodName is the constant name of the ReverseProxy authentication method
  22. const ReverseProxyMethodName = "reverse_proxy"
  23. // ReverseProxy implements the Auth interface, but actually relies on
  24. // a reverse proxy for authentication of users.
  25. // On successful authentication the proxy is expected to populate the username in the
  26. // "setting.ReverseProxyAuthUser" header. Optionally it can also populate the email of the
  27. // user in the "setting.ReverseProxyAuthEmail" header.
  28. type ReverseProxy struct {
  29. }
  30. // getUserName extracts the username from the "setting.ReverseProxyAuthUser" header
  31. func (r *ReverseProxy) getUserName(req *http.Request) string {
  32. webAuthUser := strings.TrimSpace(req.Header.Get(setting.ReverseProxyAuthUser))
  33. if len(webAuthUser) == 0 {
  34. return ""
  35. }
  36. return webAuthUser
  37. }
  38. // Name represents the name of auth method
  39. func (r *ReverseProxy) Name() string {
  40. return ReverseProxyMethodName
  41. }
  42. // Verify extracts the username from the "setting.ReverseProxyAuthUser" header
  43. // of the request and returns the corresponding user object for that name.
  44. // Verification of header data is not performed as it should have already been done by
  45. // the revese proxy.
  46. // If a username is available in the "setting.ReverseProxyAuthUser" header an existing
  47. // user object is returned (populated with username or email found in header).
  48. // Returns nil if header is empty.
  49. func (r *ReverseProxy) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *user_model.User {
  50. username := r.getUserName(req)
  51. if len(username) == 0 {
  52. return nil
  53. }
  54. log.Trace("ReverseProxy Authorization: Found username: %s", username)
  55. user, err := user_model.GetUserByName(username)
  56. if err != nil {
  57. if !user_model.IsErrUserNotExist(err) || !r.isAutoRegisterAllowed() {
  58. log.Error("GetUserByName: %v", err)
  59. return nil
  60. }
  61. user = r.newUser(req)
  62. }
  63. // Make sure requests to API paths, attachment downloads, git and LFS do not create a new session
  64. if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isGitRawReleaseOrLFSPath(req) {
  65. if sess != nil && (sess.Get("uid") == nil || sess.Get("uid").(int64) != user.ID) {
  66. handleSignIn(w, req, sess, user)
  67. }
  68. }
  69. store.GetData()["IsReverseProxy"] = true
  70. log.Trace("ReverseProxy Authorization: Logged in user %-v", user)
  71. return user
  72. }
  73. // isAutoRegisterAllowed checks if EnableReverseProxyAutoRegister setting is true
  74. func (r *ReverseProxy) isAutoRegisterAllowed() bool {
  75. return setting.Service.EnableReverseProxyAutoRegister
  76. }
  77. // newUser creates a new user object for the purpose of automatic registration
  78. // and populates its name and email with the information present in request headers.
  79. func (r *ReverseProxy) newUser(req *http.Request) *user_model.User {
  80. username := r.getUserName(req)
  81. if len(username) == 0 {
  82. return nil
  83. }
  84. email := gouuid.New().String() + "@localhost"
  85. if setting.Service.EnableReverseProxyEmail {
  86. webAuthEmail := req.Header.Get(setting.ReverseProxyAuthEmail)
  87. if len(webAuthEmail) > 0 {
  88. email = webAuthEmail
  89. }
  90. }
  91. user := &user_model.User{
  92. Name: username,
  93. Email: email,
  94. IsActive: true,
  95. }
  96. if err := user_model.CreateUser(user); err != nil {
  97. // FIXME: should I create a system notice?
  98. log.Error("CreateUser: %v", err)
  99. return nil
  100. }
  101. mailer.SendRegisterNotifyMail(user)
  102. return user
  103. }