gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package ldap
  4. import (
  5. "strings"
  6. "code.gitea.io/gitea/models/auth"
  7. "code.gitea.io/gitea/modules/json"
  8. "code.gitea.io/gitea/modules/secret"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // .____ ________ _____ __________
  12. // | | \______ \ / _ \\______ \
  13. // | | | | \ / /_\ \| ___/
  14. // | |___ | ` \/ | \ |
  15. // |_______ \/_______ /\____|__ /____|
  16. // \/ \/ \/
  17. // Package ldap provide functions & structure to query a LDAP ldap directory
  18. // For now, it's mainly tested again an MS Active Directory service, see README.md for more information
  19. // Source Basic LDAP authentication service
  20. type Source struct {
  21. auth.ConfigBase `json:"-"`
  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. GroupTeamMap string // Map LDAP groups to teams
  50. GroupTeamMapRemoval bool // Remove user from teams which are synchronized and user is not a member of the corresponding LDAP group
  51. UserUID string // User Attribute listed in Group
  52. }
  53. // FromDB fills up a LDAPConfig from serialized format.
  54. func (source *Source) FromDB(bs []byte) error {
  55. err := json.UnmarshalHandleDoubleEncode(bs, &source)
  56. if err != nil {
  57. return err
  58. }
  59. if source.BindPasswordEncrypt != "" {
  60. source.BindPassword, err = secret.DecryptSecret(setting.SecretKey, source.BindPasswordEncrypt)
  61. source.BindPasswordEncrypt = ""
  62. }
  63. return err
  64. }
  65. // ToDB exports a LDAPConfig to a serialized format.
  66. func (source *Source) ToDB() ([]byte, error) {
  67. var err error
  68. source.BindPasswordEncrypt, err = secret.EncryptSecret(setting.SecretKey, source.BindPassword)
  69. if err != nil {
  70. return nil, err
  71. }
  72. source.BindPassword = ""
  73. return json.Marshal(source)
  74. }
  75. // SecurityProtocolName returns the name of configured security
  76. // protocol.
  77. func (source *Source) SecurityProtocolName() string {
  78. return SecurityProtocolNames[source.SecurityProtocol]
  79. }
  80. // IsSkipVerify returns if SkipVerify is set
  81. func (source *Source) IsSkipVerify() bool {
  82. return source.SkipVerify
  83. }
  84. // HasTLS returns if HasTLS
  85. func (source *Source) HasTLS() bool {
  86. return source.SecurityProtocol > SecurityProtocolUnencrypted
  87. }
  88. // UseTLS returns if UseTLS
  89. func (source *Source) UseTLS() bool {
  90. return source.SecurityProtocol != SecurityProtocolUnencrypted
  91. }
  92. // ProvidesSSHKeys returns if this source provides SSH Keys
  93. func (source *Source) ProvidesSSHKeys() bool {
  94. return strings.TrimSpace(source.AttributeSSHPublicKey) != ""
  95. }
  96. func init() {
  97. auth.RegisterTypeConfig(auth.LDAP, &Source{})
  98. auth.RegisterTypeConfig(auth.DLDAP, &Source{})
  99. }