Browse Source

完善ldap登录

zhangjq 5 years ago
parent
commit
d6239076dd
1 changed files with 26 additions and 31 deletions
  1. 26 31
      engine/auth/ldap_auth.go

+ 26 - 31
engine/auth/ldap_auth.go

@@ -81,48 +81,43 @@ func (h ldapHandler) Bind(bindDN, bindSimplePw string, conn net.Conn) (ldap.LDAP
 
 ///////////// Return some hardcoded search results - we'll respond to any baseDN for testing
 func (h ldapHandler) Search(boundDN string, searchReq ldap.SearchRequest, conn net.Conn) (ldap.ServerSearchResult, error) {
-	fmt.Printf("%s,search......%s\n", boundDN, searchReq)
+	fmt.Printf("search......basedn:%s, searchreq:%s\n", boundDN, searchReq)
 	userName := ""
 	if boundDN == "cn=qianqiuiot" {
-		if searchReq.Filter == "(objectClass=*)" {
-			uid := searchReq.BaseDN
-			dn := uid
-			if dn == "" {
-				dn = boundDN
-			}
-			entries := []*ldap.Entry{
-				&ldap.Entry{dn, []*ldap.EntryAttribute{
-					//&ldap.EntryAttribute{"uid", []string{}},
-				}},
-			}
-			return ldap.ServerSearchResult{entries, []string{}, []ldap.Control{}, ldap.LDAPResultSuccess}, nil
-		}else {
-			start := strings.Index(searchReq.Filter, "uid=")
+		fmt.Println("filter:", searchReq.Filter)
+		fmt.Println("BaseDN:", searchReq.BaseDN)
+		start := strings.Index(searchReq.Filter, "uid=")
+		if start > 0 {
 			end := strings.Index(searchReq.Filter[start:], ")")
 			fmt.Println("%d,%d", start, end)
 			userName = searchReq.Filter[start+4 : start+end]
 			fmt.Println(userName)
+		}else {
+			if len(searchReq.BaseDN)>3 {
+				userName = searchReq.BaseDN[3:]
+			}
 		}
 	}else {
 		userName = boundDN[3:]
 	}
+	if userName != "" {
+		var user models.SysUser
+		ret, err := h.App.GetBusinessDb("qianqiuiot.com").SQL(models.SqlUserLogin, userName).Get(&user)
 
-	var user models.SysUser
-	ret, err := h.App.GetBusinessDb("qianqiuiot.com").SQL(models.SqlUserLogin, userName).Get(&user)
-
-	if ret && err == nil {
-		entries := []*ldap.Entry{
-			&ldap.Entry{"cn=" + user.LoginId, []*ldap.EntryAttribute{
-				&ldap.EntryAttribute{"cn", []string{user.Name}},
-				&ldap.EntryAttribute{"uidNumber", []string{user.Id}},
-				&ldap.EntryAttribute{"accountStatus", []string{"active"}},
-				&ldap.EntryAttribute{"uid", []string{user.LoginId}},
-				&ldap.EntryAttribute{"description", []string{user.Name}},
-				&ldap.EntryAttribute{"objectClass", []string{"posixAccount"}},
-			}},
+		if ret && err == nil {
+			entries := []*ldap.Entry{
+				&ldap.Entry{"cn=" + user.LoginId, []*ldap.EntryAttribute{
+					&ldap.EntryAttribute{"cn", []string{user.Name}},
+					&ldap.EntryAttribute{"uidNumber", []string{user.Id}},
+					&ldap.EntryAttribute{"accountStatus", []string{"active"}},
+					&ldap.EntryAttribute{"uid", []string{user.LoginId}},
+					&ldap.EntryAttribute{"description", []string{user.Name}},
+					&ldap.EntryAttribute{"email", []string{user.Email}},
+					&ldap.EntryAttribute{"objectClass", []string{"posixAccount"}},
+				}},
+			}
+			return ldap.ServerSearchResult{entries, []string{}, []ldap.Control{}, ldap.LDAPResultSuccess}, nil
 		}
-		return ldap.ServerSearchResult{entries, []string{}, []ldap.Control{}, ldap.LDAPResultSuccess}, err
-	}else {
-		return ldap.ServerSearchResult{nil, []string{}, []ldap.Control{}, ldap.LDAPResultCompareFalse}, err
 	}
+	return ldap.ServerSearchResult{nil, []string{}, []ldap.Control{}, ldap.LDAPResultSuccess}, nil
 }