@@ -3,6 +3,7 @@ package service
33import (
44 "context"
55 "crypto/tls"
6+ "errors"
67 "fmt"
78 "sync"
89 "time"
@@ -16,6 +17,17 @@ import (
1617 "go.uber.org/dig"
1718)
1819
20+ var (
21+ ErrLDAPNoUsers = errors .New ("no users found" )
22+ ErrLDAPMultipleUsers = errors .New ("multiple users found" )
23+ )
24+
25+ type UserInfoResult struct {
26+ DN string
27+ CN string
28+ Email string
29+ }
30+
1931type LdapService struct {
2032 log * logger.Logger
2133 ctx context.Context
@@ -146,7 +158,7 @@ func (ldap *LdapService) connect() (*ldapgo.Conn, error) {
146158 return ldap .conn , nil
147159}
148160
149- func (ldap * LdapService ) GetUserInfo (username string ) (dn string , email string , cn string , err error ) {
161+ func (ldap * LdapService ) GetUserInfo (username string ) (* UserInfoResult , error ) {
150162 escapedUsername := ldapgo .EscapeFilter (username )
151163 filter := fmt .Sprintf (ldap .config .LDAP .SearchFilter , escapedUsername )
152164
@@ -163,15 +175,22 @@ func (ldap *LdapService) GetUserInfo(username string) (dn string, email string,
163175
164176 searchResult , err := ldap .conn .Search (searchRequest )
165177 if err != nil {
166- return "" , "" , "" , err
178+ return nil , err
167179 }
168180
169181 if len (searchResult .Entries ) != 1 {
170- return "" , "" , "" , fmt .Errorf ("multiple or no entries found for user %s" , username )
182+ if len (searchResult .Entries ) == 0 {
183+ return nil , ErrLDAPNoUsers
184+ }
185+ return nil , ErrLDAPMultipleUsers
171186 }
172187
173188 entry := searchResult .Entries [0 ]
174- return entry .DN , entry .GetAttributeValue ("mail" ), entry .GetAttributeValue ("cn" ), nil
189+ return & UserInfoResult {
190+ DN : entry .DN ,
191+ CN : entry .GetAttributeValue ("cn" ),
192+ Email : entry .GetAttributeValue ("mail" ),
193+ }, nil
175194}
176195
177196func (ldap * LdapService ) GetUserCount () (int , error ) {
0 commit comments