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.

source_authenticate.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021 The Gitea 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 ldap
  5. import (
  6. "fmt"
  7. "strings"
  8. asymkey_model "code.gitea.io/gitea/models/asymkey"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/models/login"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/services/mailer"
  13. user_service "code.gitea.io/gitea/services/user"
  14. )
  15. // Authenticate queries if login/password is valid against the LDAP directory pool,
  16. // and create a local user if success when enabled.
  17. func (source *Source) Authenticate(user *user_model.User, userName, password string) (*user_model.User, error) {
  18. sr := source.SearchEntry(userName, password, source.loginSource.Type == login.DLDAP)
  19. if sr == nil {
  20. // User not in LDAP, do nothing
  21. return nil, user_model.ErrUserNotExist{Name: userName}
  22. }
  23. isAttributeSSHPublicKeySet := len(strings.TrimSpace(source.AttributeSSHPublicKey)) > 0
  24. // Update User admin flag if exist
  25. if isExist, err := user_model.IsUserExist(0, sr.Username); err != nil {
  26. return nil, err
  27. } else if isExist {
  28. if user == nil {
  29. user, err = user_model.GetUserByName(sr.Username)
  30. if err != nil {
  31. return nil, err
  32. }
  33. }
  34. if user != nil && !user.ProhibitLogin {
  35. cols := make([]string, 0)
  36. if len(source.AdminFilter) > 0 && user.IsAdmin != sr.IsAdmin {
  37. // Change existing admin flag only if AdminFilter option is set
  38. user.IsAdmin = sr.IsAdmin
  39. cols = append(cols, "is_admin")
  40. }
  41. if !user.IsAdmin && len(source.RestrictedFilter) > 0 && user.IsRestricted != sr.IsRestricted {
  42. // Change existing restricted flag only if RestrictedFilter option is set
  43. user.IsRestricted = sr.IsRestricted
  44. cols = append(cols, "is_restricted")
  45. }
  46. if len(cols) > 0 {
  47. err = user_model.UpdateUserCols(db.DefaultContext, user, cols...)
  48. if err != nil {
  49. return nil, err
  50. }
  51. }
  52. }
  53. }
  54. if user != nil {
  55. if isAttributeSSHPublicKeySet && asymkey_model.SynchronizePublicKeys(user, source.loginSource, sr.SSHPublicKey) {
  56. return user, asymkey_model.RewriteAllPublicKeys()
  57. }
  58. return user, nil
  59. }
  60. // Fallback.
  61. if len(sr.Username) == 0 {
  62. sr.Username = userName
  63. }
  64. if len(sr.Mail) == 0 {
  65. sr.Mail = fmt.Sprintf("%s@localhost", sr.Username)
  66. }
  67. user = &user_model.User{
  68. LowerName: strings.ToLower(sr.Username),
  69. Name: sr.Username,
  70. FullName: composeFullName(sr.Name, sr.Surname, sr.Username),
  71. Email: sr.Mail,
  72. LoginType: source.loginSource.Type,
  73. LoginSource: source.loginSource.ID,
  74. LoginName: userName,
  75. IsActive: true,
  76. IsAdmin: sr.IsAdmin,
  77. IsRestricted: sr.IsRestricted,
  78. }
  79. err := user_model.CreateUser(user)
  80. if err != nil {
  81. return user, err
  82. }
  83. mailer.SendRegisterNotifyMail(user)
  84. if isAttributeSSHPublicKeySet && asymkey_model.AddPublicKeysBySource(user, source.loginSource, sr.SSHPublicKey) {
  85. err = asymkey_model.RewriteAllPublicKeys()
  86. }
  87. if err == nil && len(source.AttributeAvatar) > 0 {
  88. _ = user_service.UploadAvatar(user, sr.Avatar)
  89. }
  90. return user, err
  91. }
  92. // IsSkipLocalTwoFA returns if this source should skip local 2fa for password authentication
  93. func (source *Source) IsSkipLocalTwoFA() bool {
  94. return source.SkipLocalTwoFA
  95. }