|
@@ -7,7 +7,7 @@ import (
|
|
|
"eta/eta_api/models/system"
|
|
|
"eta/eta_api/utils"
|
|
|
"fmt"
|
|
|
- "github.com/jtblin/go-ldap-client"
|
|
|
+ "github.com/go-ldap/ldap"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -198,24 +198,44 @@ func LdapUserCheck(userName, password string) (pass bool, err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- client := &ldap.LDAPClient{
|
|
|
- Base: confMap[models.BusinessConfLdapBase],
|
|
|
- Host: confMap[models.BusinessConfLdapHost],
|
|
|
- Port: ldapPort,
|
|
|
- //UseSSL: false,
|
|
|
- //BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com",
|
|
|
- //BindPassword: "readonlypassword",
|
|
|
- UserFilter: "(uid=%s)",
|
|
|
- //GroupFilter: "(memberUid=%s)",
|
|
|
- //Attributes: []string{"givenName", "sn", "mail", "uid"},
|
|
|
+ // 连接ldap
|
|
|
+ addr := fmt.Sprintf("%s:%d", confMap[models.BusinessConfLdapHost], ldapPort)
|
|
|
+ conn, e := ldap.Dial("tcp", addr)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ldap Dial err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer conn.Close()
|
|
|
+
|
|
|
+ // 绑定用户
|
|
|
+ bindUserName := fmt.Sprintf("%s%s", userName, confMap[models.BusinessConfLdapBindUserSuffix])
|
|
|
+ if e = conn.Bind(bindUserName, password); e != nil {
|
|
|
+ err = fmt.Errorf("ldap Bind err: %s", e.Error())
|
|
|
+ return
|
|
|
}
|
|
|
- defer client.Close()
|
|
|
|
|
|
- ok, _, e := client.Authenticate(userName, password)
|
|
|
+ // 鉴权操作
|
|
|
+ searchRequest := ldap.NewSearchRequest(
|
|
|
+ confMap[models.BusinessConfLdapBase],
|
|
|
+ ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
|
+ fmt.Sprintf(confMap[models.BusinessConfLdapUserFilter], userName),
|
|
|
+ []string{"dn"},
|
|
|
+ nil,
|
|
|
+ )
|
|
|
+ //b, _ := json.Marshal(searchRequest)
|
|
|
+ //fmt.Println("searchRequest: ", string(b))
|
|
|
+
|
|
|
+ sr, e := conn.Search(searchRequest)
|
|
|
if e != nil {
|
|
|
- err = fmt.Errorf("AD域校验账号密码失败, Err: %s", e.Error())
|
|
|
+ err = fmt.Errorf("ldap Search err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证结果
|
|
|
+ if len(sr.Entries) != 1 {
|
|
|
+ utils.FileLog.Info("ldap check fail: user does not exist or too many entries returned")
|
|
|
return
|
|
|
}
|
|
|
- pass = ok
|
|
|
+ pass = true
|
|
|
return
|
|
|
}
|