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.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "strings"
  7. "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/modules/json"
  9. "code.gitea.io/gitea/modules/secret"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // .____ ________ _____ __________
  13. // | | \______ \ / _ \\______ \
  14. // | | | | \ / /_\ \| ___/
  15. // | |___ | ` \/ | \ |
  16. // |_______ \/_______ /\____|__ /____|
  17. // \/ \/ \/
  18. // Package ldap provide functions & structure to query a LDAP ldap directory
  19. // For now, it's mainly tested again an MS Active Directory service, see README.md for more information
  20. // Source Basic LDAP authentication service
  21. type Source struct {
  22. Name string // canonical name (ie. corporate.ad)
  23. Host string // LDAP host
  24. Port int // port number
  25. SecurityProtocol SecurityProtocol
  26. SkipVerify bool
  27. BindDN string // DN to bind with
  28. BindPasswordEncrypt string // Encrypted Bind BN password
  29. BindPassword string // Bind DN password
  30. UserBase string // Base search path for users
  31. UserDN string // Template for the DN of the user for simple auth
  32. AttributeUsername string // Username attribute
  33. AttributeName string // First name attribute
  34. AttributeSurname string // Surname attribute
  35. AttributeMail string // E-mail attribute
  36. AttributesInBind bool // fetch attributes in bind context (not user)
  37. AttributeSSHPublicKey string // LDAP SSH Public Key attribute
  38. AttributeAvatar string
  39. SearchPageSize uint32 // Search with paging page size
  40. Filter string // Query filter to validate entry
  41. AdminFilter string // Query filter to check if user is admin
  42. RestrictedFilter string // Query filter to check if user is restricted
  43. Enabled bool // if this source is disabled
  44. AllowDeactivateAll bool // Allow an empty search response to deactivate all users from this source
  45. GroupsEnabled bool // if the group checking is enabled
  46. GroupDN string // Group Search Base
  47. GroupFilter string // Group Name Filter
  48. GroupMemberUID string // Group Attribute containing array of UserUID
  49. UserUID string // User Attribute listed in Group
  50. SkipLocalTwoFA bool `json:",omitempty"` // Skip Local 2fa for users authenticated with this source
  51. // reference to the authSource
  52. authSource *auth.Source
  53. }
  54. // FromDB fills up a LDAPConfig from serialized format.
  55. func (source *Source) FromDB(bs []byte) error {
  56. err := json.UnmarshalHandleDoubleEncode(bs, &source)
  57. if err != nil {
  58. return err
  59. }
  60. if source.BindPasswordEncrypt != "" {
  61. source.BindPassword, err = secret.DecryptSecret(setting.SecretKey, source.BindPasswordEncrypt)
  62. source.BindPasswordEncrypt = ""
  63. }
  64. return err
  65. }
  66. // ToDB exports a LDAPConfig to a serialized format.
  67. func (source *Source) ToDB() ([]byte, error) {
  68. var err error
  69. source.BindPasswordEncrypt, err = secret.EncryptSecret(setting.SecretKey, source.BindPassword)
  70. if err != nil {
  71. return nil, err
  72. }
  73. source.BindPassword = ""
  74. return json.Marshal(source)
  75. }
  76. // SecurityProtocolName returns the name of configured security
  77. // protocol.
  78. func (source *Source) SecurityProtocolName() string {
  79. return SecurityProtocolNames[source.SecurityProtocol]
  80. }
  81. // IsSkipVerify returns if SkipVerify is set
  82. func (source *Source) IsSkipVerify() bool {
  83. return source.SkipVerify
  84. }
  85. // HasTLS returns if HasTLS
  86. func (source *Source) HasTLS() bool {
  87. return source.SecurityProtocol > SecurityProtocolUnencrypted
  88. }
  89. // UseTLS returns if UseTLS
  90. func (source *Source) UseTLS() bool {
  91. return source.SecurityProtocol != SecurityProtocolUnencrypted
  92. }
  93. // ProvidesSSHKeys returns if this source provides SSH Keys
  94. func (source *Source) ProvidesSSHKeys() bool {
  95. return len(strings.TrimSpace(source.AttributeSSHPublicKey)) > 0
  96. }
  97. // SetAuthSource sets the related AuthSource
  98. func (source *Source) SetAuthSource(authSource *auth.Source) {
  99. source.authSource = authSource
  100. }
  101. func init() {
  102. auth.RegisterTypeConfig(auth.LDAP, &Source{})
  103. auth.RegisterTypeConfig(auth.DLDAP, &Source{})
  104. }