Skip to content

Commit dda39fe

Browse files
committed
fix: don't fail with 500 when ldap user is not found
1 parent 3ce458e commit dda39fe

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

‎internal/service/auth_service.go‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,19 @@ func (auth *AuthService) SearchUser(username string) (*model.UserSearch, error)
173173
}
174174

175175
if auth.ldap != nil {
176-
userDN, email, cn, err := auth.ldap.GetUserInfo(username)
176+
res, err := auth.ldap.GetUserInfo(username)
177177

178178
if err != nil {
179+
if errors.Is(err, ErrLDAPMultipleUsers) || errors.Is(err, ErrLDAPNoUsers) {
180+
return nil, ErrUserNotFound
181+
}
179182
return nil, fmt.Errorf("failed to get ldap user: %w", err)
180183
}
181184

182185
return &model.UserSearch{
183-
Username: userDN,
184-
Email: email,
185-
Name: cn,
186+
Username: res.DN,
187+
Email: res.Email,
188+
Name: res.CN,
186189
Type: model.UserLDAP,
187190
}, nil
188191
}

‎internal/service/ldap_service.go‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
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+
1931
type 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

177196
func (ldap *LdapService) GetUserCount() (int, error) {

0 commit comments

Comments
 (0)