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.

auth.go 4.7 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
10 years ago
10 years ago
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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "reflect"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth/sso"
  11. "code.gitea.io/gitea/modules/validation"
  12. "gitea.com/macaron/binding"
  13. "gitea.com/macaron/macaron"
  14. "gitea.com/macaron/session"
  15. "github.com/unknwon/com"
  16. )
  17. // IsAPIPath if URL is an api path
  18. func IsAPIPath(url string) bool {
  19. return strings.HasPrefix(url, "/api/")
  20. }
  21. // SignedInUser returns the user object of signed user.
  22. // It returns a bool value to indicate whether user uses basic auth or not.
  23. func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool) {
  24. if !models.HasEngine {
  25. return nil, false
  26. }
  27. // Try to sign in with each of the enabled plugins
  28. for _, ssoMethod := range sso.Methods() {
  29. if !ssoMethod.IsEnabled() {
  30. continue
  31. }
  32. user := ssoMethod.VerifyAuthData(ctx, sess)
  33. if user != nil {
  34. _, isBasic := ssoMethod.(*sso.Basic)
  35. return user, isBasic
  36. }
  37. }
  38. return nil, false
  39. }
  40. // Form form binding interface
  41. type Form interface {
  42. binding.Validator
  43. }
  44. func init() {
  45. binding.SetNameMapper(com.ToSnakeCase)
  46. }
  47. // AssignForm assign form values back to the template data.
  48. func AssignForm(form interface{}, data map[string]interface{}) {
  49. typ := reflect.TypeOf(form)
  50. val := reflect.ValueOf(form)
  51. if typ.Kind() == reflect.Ptr {
  52. typ = typ.Elem()
  53. val = val.Elem()
  54. }
  55. for i := 0; i < typ.NumField(); i++ {
  56. field := typ.Field(i)
  57. fieldName := field.Tag.Get("form")
  58. // Allow ignored fields in the struct
  59. if fieldName == "-" {
  60. continue
  61. } else if len(fieldName) == 0 {
  62. fieldName = com.ToSnakeCase(field.Name)
  63. }
  64. data[fieldName] = val.Field(i).Interface()
  65. }
  66. }
  67. func getRuleBody(field reflect.StructField, prefix string) string {
  68. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  69. if strings.HasPrefix(rule, prefix) {
  70. return rule[len(prefix) : len(rule)-1]
  71. }
  72. }
  73. return ""
  74. }
  75. // GetSize get size int form tag
  76. func GetSize(field reflect.StructField) string {
  77. return getRuleBody(field, "Size(")
  78. }
  79. // GetMinSize get minimal size in form tag
  80. func GetMinSize(field reflect.StructField) string {
  81. return getRuleBody(field, "MinSize(")
  82. }
  83. // GetMaxSize get max size in form tag
  84. func GetMaxSize(field reflect.StructField) string {
  85. return getRuleBody(field, "MaxSize(")
  86. }
  87. // GetInclude get include in form tag
  88. func GetInclude(field reflect.StructField) string {
  89. return getRuleBody(field, "Include(")
  90. }
  91. func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaron.Locale) binding.Errors {
  92. if errs.Len() == 0 {
  93. return errs
  94. }
  95. data["HasError"] = true
  96. // If the field with name errs[0].FieldNames[0] is not found in form
  97. // somehow, some code later on will panic on Data["ErrorMsg"].(string).
  98. // So initialize it to some default.
  99. data["ErrorMsg"] = l.Tr("form.unknown_error")
  100. AssignForm(f, data)
  101. typ := reflect.TypeOf(f)
  102. val := reflect.ValueOf(f)
  103. if typ.Kind() == reflect.Ptr {
  104. typ = typ.Elem()
  105. val = val.Elem()
  106. }
  107. if field, ok := typ.FieldByName(errs[0].FieldNames[0]); ok {
  108. fieldName := field.Tag.Get("form")
  109. if fieldName != "-" {
  110. data["Err_"+field.Name] = true
  111. trName := field.Tag.Get("locale")
  112. if len(trName) == 0 {
  113. trName = l.Tr("form." + field.Name)
  114. } else {
  115. trName = l.Tr(trName)
  116. }
  117. switch errs[0].Classification {
  118. case binding.ERR_REQUIRED:
  119. data["ErrorMsg"] = trName + l.Tr("form.require_error")
  120. case binding.ERR_ALPHA_DASH:
  121. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error")
  122. case binding.ERR_ALPHA_DASH_DOT:
  123. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
  124. case validation.ErrGitRefName:
  125. data["ErrorMsg"] = trName + l.Tr("form.git_ref_name_error")
  126. case binding.ERR_SIZE:
  127. data["ErrorMsg"] = trName + l.Tr("form.size_error", GetSize(field))
  128. case binding.ERR_MIN_SIZE:
  129. data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field))
  130. case binding.ERR_MAX_SIZE:
  131. data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field))
  132. case binding.ERR_EMAIL:
  133. data["ErrorMsg"] = trName + l.Tr("form.email_error")
  134. case binding.ERR_URL:
  135. data["ErrorMsg"] = trName + l.Tr("form.url_error")
  136. case binding.ERR_INCLUDE:
  137. data["ErrorMsg"] = trName + l.Tr("form.include_error", GetInclude(field))
  138. case validation.ErrGlobPattern:
  139. data["ErrorMsg"] = trName + l.Tr("form.glob_pattern_error", errs[0].Message)
  140. default:
  141. data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
  142. }
  143. return errs
  144. }
  145. }
  146. return errs
  147. }