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 5.4 kB

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