@@ -8,23 +8,28 @@ import (
88 "github.com/go-ldap/ldap/v3"
99)
1010
11+ const (
12+ LdapDefaultAccountPattern = "(&(objectClass=User)(userPrincipalName=%s))"
13+ LdapDefaultAttributeGivenName = "displayName"
14+ )
15+
1116var (
1217 LdapUserSearchNotFoundErr = errors .New ("用户不存在或密码错误" )
1318)
1419
1520type AuthLoginConfig struct {
16- BaseDn string `json:"basedn"`
17- BindUsername string `json:"bind_username"`
18- BindPassword string `json:"bind_password"`
19- AccountPattern string `json:"account_pattern"`
20- GivenNameKey string `json:"given_name_key"`
21- EmailKey string `json:"email_key"`
22- MobileKey string `json:"mobile_key"`
23- PhoneKey string `json:"phone_key"`
24- DepartmentKey string `json:"department_key"`
25- PositionKey string `json:"position_key"`
26- LocationKey string `json:"location_key"`
27- ImKey string `json:"im_key"`
21+ BaseDn string `json:"basedn"`
22+ BindUsername string `json:"bind_username"`
23+ BindPassword string `json:"bind_password"`
24+ AccountPattern string `json:"account_pattern"`
25+ GivenNameKey string `json:"given_name_key"`
26+ EmailKey string `json:"email_key"`
27+ MobileKey string `json:"mobile_key"`
28+ PhoneKey string `json:"phone_key"`
29+ DepartmentKey string `json:"department_key"`
30+ PositionKey string `json:"position_key"`
31+ LocationKey string `json:"location_key"`
32+ ImKey string `json:"im_key"`
2833}
2934
3035// AuthLoginLdapService ldap auth login
@@ -49,6 +54,12 @@ func (al *AuthLoginLdapService) InitConf(url string, conf string) error {
4954 return err
5055 }
5156 al .config = authLoginConfig
57+ if al .config .AccountPattern == "" {
58+ al .config .AccountPattern = LdapDefaultAccountPattern
59+ }
60+ if al .config .GivenNameKey == "" {
61+ al .config .GivenNameKey = LdapDefaultAttributeGivenName
62+ }
5263 return nil
5364}
5465
@@ -82,18 +93,14 @@ func (al *AuthLoginLdapService) AuthLogin(username string, password string) (*Au
8293 }
8394
8495 // 搜索下用户信息
85- accountPattern := "(&(objectClass=User)(userPrincipalName=%s))"
86- if al .config .AccountPattern != "" {
87- accountPattern = al .config .AccountPattern
88- }
8996 searchRequest := ldap .NewSearchRequest (
9097 al .config .BaseDn ,
9198 ldap .ScopeWholeSubtree ,
9299 ldap .NeverDerefAliases ,
93100 0 ,
94101 0 ,
95102 false ,
96- fmt .Sprintf (accountPattern , username ),
103+ fmt .Sprintf (al . config . AccountPattern , username ),
97104 al .GetAttributes (),
98105 nil ,
99106 )
0 commit comments