You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

reverseproxy.go 3.5 kB

Add single sign-on support via SSPI on Windows (#8463) * Add single sign-on support via SSPI on Windows * Ensure plugins implement interface * Ensure plugins implement interface * Move functions used only by the SSPI auth method to sspi_windows.go * Field SSPISeparatorReplacement of AuthenticationForm should not be required via binding, as binding will insist the field is non-empty even if another login type is selected * Fix breaking of oauth authentication on download links. Do not create new session with SSPI authentication on download links. * Update documentation for the new 'SPNEGO with SSPI' login source * Mention in documentation that ROOT_URL should contain the FQDN of the server * Make sure that Contexter is not checking for active login sources when the ORM engine is not initialized (eg. when installing) * Always initialize and free SSO methods, even if they are not enabled, as a method can be activated while the app is running (from Authentication sources) * Add option in SSPIConfig for removing of domains from logon names * Update helper text for StripDomainNames option * Make sure handleSignIn() is called after a new user object is created by SSPI auth method * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Only make a query to the DB to check if SSPI is enabled on handlers that need that information for templates * Remove code duplication * Log errors in ActiveLoginSources Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert suffix of randomly generated E-mails for Reverse proxy authentication Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert unneeded white-space change in template Co-Authored-By: Lauris BH <lauris@nix.lv> * Add copyright comments at the top of new files * Use loopback name for randomly generated emails * Add locale tag for the SSPISeparatorReplacement field with proper casing * Revert casing of SSPISeparatorReplacement field in locale file, moving it up, next to other form fields * Update docs/content/doc/features/authentication.en-us.md Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Remove Priority() method and define the order in which SSO auth methods should be executed in one place * Log authenticated username only if it's not empty * Rephrase helper text for automatic creation of users * Return error if more than one active SSPI auth source is found * Change newUser() function to return error, letting caller log/handle the error * Move isPublicResource, isPublicPage and handleSignIn functions outside SSPI auth method to allow other SSO methods to reuse them if needed * Refactor initialization of the list containing SSO auth methods * Validate SSPI settings on POST * Change SSPI to only perform authentication on its own login page, API paths and download links. Leave Toggle middleware to redirect non authenticated users to login page * Make 'Default language' in SSPI config empty, unless changed by admin * Show error if admin tries to add a second authentication source of type SSPI * Simplify declaration of global variable * Rebuild gitgraph.js on Linux * Make sure config values containing only whitespace are not accepted
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 sso
  6. import (
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "gitea.com/macaron/macaron"
  12. "gitea.com/macaron/session"
  13. gouuid "github.com/satori/go.uuid"
  14. )
  15. // Ensure the struct implements the interface.
  16. var (
  17. _ SingleSignOn = &ReverseProxy{}
  18. )
  19. // ReverseProxy implements the SingleSignOn interface, but actually relies on
  20. // a reverse proxy for authentication of users.
  21. // On successful authentication the proxy is expected to populate the username in the
  22. // "setting.ReverseProxyAuthUser" header. Optionally it can also populate the email of the
  23. // user in the "setting.ReverseProxyAuthEmail" header.
  24. type ReverseProxy struct {
  25. }
  26. // getUserName extracts the username from the "setting.ReverseProxyAuthUser" header
  27. func (r *ReverseProxy) getUserName(ctx *macaron.Context) string {
  28. webAuthUser := strings.TrimSpace(ctx.Req.Header.Get(setting.ReverseProxyAuthUser))
  29. if len(webAuthUser) == 0 {
  30. return ""
  31. }
  32. return webAuthUser
  33. }
  34. // Init does nothing as the ReverseProxy implementation does not need initialization
  35. func (r *ReverseProxy) Init() error {
  36. return nil
  37. }
  38. // Free does nothing as the ReverseProxy implementation does not have to release resources
  39. func (r *ReverseProxy) Free() error {
  40. return nil
  41. }
  42. // IsEnabled checks if EnableReverseProxyAuth setting is true
  43. func (r *ReverseProxy) IsEnabled() bool {
  44. return setting.Service.EnableReverseProxyAuth
  45. }
  46. // VerifyAuthData extracts the username from the "setting.ReverseProxyAuthUser" header
  47. // of the request and returns the corresponding user object for that name.
  48. // Verification of header data is not performed as it should have already been done by
  49. // the revese proxy.
  50. // If a username is available in the "setting.ReverseProxyAuthUser" header an existing
  51. // user object is returned (populated with username or email found in header).
  52. // Returns nil if header is empty.
  53. func (r *ReverseProxy) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models.User {
  54. username := r.getUserName(ctx)
  55. if len(username) == 0 {
  56. return nil
  57. }
  58. user, err := models.GetUserByName(username)
  59. if err != nil {
  60. if models.IsErrUserNotExist(err) && r.isAutoRegisterAllowed() {
  61. return r.newUser(ctx)
  62. }
  63. log.Error("GetUserByName: %v", err)
  64. return nil
  65. }
  66. return user
  67. }
  68. // isAutoRegisterAllowed checks if EnableReverseProxyAutoRegister setting is true
  69. func (r *ReverseProxy) isAutoRegisterAllowed() bool {
  70. return setting.Service.EnableReverseProxyAutoRegister
  71. }
  72. // newUser creates a new user object for the purpose of automatic registration
  73. // and populates its name and email with the information present in request headers.
  74. func (r *ReverseProxy) newUser(ctx *macaron.Context) *models.User {
  75. username := r.getUserName(ctx)
  76. if len(username) == 0 {
  77. return nil
  78. }
  79. email := gouuid.NewV4().String() + "@localhost"
  80. if setting.Service.EnableReverseProxyEmail {
  81. webAuthEmail := ctx.Req.Header.Get(setting.ReverseProxyAuthEmail)
  82. if len(webAuthEmail) > 0 {
  83. email = webAuthEmail
  84. }
  85. }
  86. user := &models.User{
  87. Name: username,
  88. Email: email,
  89. Passwd: username,
  90. IsActive: true,
  91. }
  92. if err := models.CreateUser(user); err != nil {
  93. // FIXME: should I create a system notice?
  94. log.Error("CreateUser: %v", err)
  95. return nil
  96. }
  97. return user
  98. }