Skip to content

Commit 0defcb4

Browse files
committed
Refactor LDAP SearchEntry return type
1 parent 9a0b0da commit 0defcb4

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

models/login_source.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ func composeFullName(firstname, surname, username string) string {
405405
// LoginViaLDAP queries if login/password is valid against the LDAP directory pool,
406406
// and create a local user if success when enabled.
407407
func LoginViaLDAP(user *User, login, password string, source *LoginSource, autoRegister bool) (*User, error) {
408-
username, fn, sn, mail, isAdmin, succeed := source.Cfg.(*LDAPConfig).SearchEntry(login, password, source.Type == LoginDLDAP)
409-
if !succeed {
408+
sr := source.Cfg.(*LDAPConfig).SearchEntry(login, password, source.Type == LoginDLDAP)
409+
if sr == nil {
410410
// User not in LDAP, do nothing
411411
return nil, ErrUserNotExist{0, login, 0}
412412
}
@@ -416,28 +416,28 @@ func LoginViaLDAP(user *User, login, password string, source *LoginSource, autoR
416416
}
417417

418418
// Fallback.
419-
if len(username) == 0 {
420-
username = login
419+
if len(sr.Username) == 0 {
420+
sr.Username = login
421421
}
422422
// Validate username make sure it satisfies requirement.
423-
if binding.AlphaDashDotPattern.MatchString(username) {
424-
return nil, fmt.Errorf("Invalid pattern for attribute 'username' [%s]: must be valid alpha or numeric or dash(-_) or dot characters", username)
423+
if binding.AlphaDashDotPattern.MatchString(sr.Username) {
424+
return nil, fmt.Errorf("Invalid pattern for attribute 'username' [%s]: must be valid alpha or numeric or dash(-_) or dot characters", sr.Username)
425425
}
426426

427-
if len(mail) == 0 {
428-
mail = fmt.Sprintf("%s@localhost", username)
427+
if len(sr.Mail) == 0 {
428+
sr.Mail = fmt.Sprintf("%s@localhost", sr.Username)
429429
}
430430

431431
user = &User{
432-
LowerName: strings.ToLower(username),
433-
Name: username,
434-
FullName: composeFullName(fn, sn, username),
435-
Email: mail,
432+
LowerName: strings.ToLower(sr.Username),
433+
Name: sr.Username,
434+
FullName: composeFullName(sr.Name, sr.Surname, sr.Username),
435+
Email: sr.Mail,
436436
LoginType: source.Type,
437437
LoginSource: source.ID,
438438
LoginName: login,
439439
IsActive: true,
440-
IsAdmin: isAdmin,
440+
IsAdmin: sr.IsAdmin,
441441
}
442442
return user, CreateUser(user)
443443
}

modules/auth/ldap/ldap.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ type Source struct {
4747
Enabled bool // if this source is disabled
4848
}
4949

50+
// SearchResult : user data
51+
type SearchResult struct {
52+
Username string // Username
53+
Name string // Name
54+
Surname string // Surname
55+
Mail string // E-mail address
56+
IsAdmin bool // if user is administrator
57+
}
58+
5059
func (ls *Source) sanitizedUserQuery(username string) (string, bool) {
5160
// See http://tools.ietf.org/search/rfc4515
5261
badCharacters := "\x00()*\\"
@@ -150,17 +159,17 @@ func bindUser(l *ldap.Conn, userDN, passwd string) error {
150159
}
151160

152161
// SearchEntry : search an LDAP source if an entry (name, passwd) is valid and in the specific filter
153-
func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, string, string, string, bool, bool) {
162+
func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResult {
154163
// See https://tools.ietf.org/search/rfc4513#section-5.1.2
155164
if len(passwd) == 0 {
156165
log.Debug("Auth. failed for %s, password cannot be empty")
157-
return "", "", "", "", false, false
166+
return nil
158167
}
159168
l, err := dial(ls)
160169
if err != nil {
161170
log.Error(4, "LDAP Connect error, %s:%v", ls.Host, err)
162171
ls.Enabled = false
163-
return "", "", "", "", false, false
172+
return nil
164173
}
165174
defer l.Close()
166175

@@ -171,29 +180,29 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
171180
var ok bool
172181
userDN, ok = ls.sanitizedUserDN(name)
173182
if !ok {
174-
return "", "", "", "", false, false
183+
return nil
175184
}
176185
} else {
177186
log.Trace("LDAP will use BindDN.")
178187

179188
var found bool
180189
userDN, found = ls.findUserDN(l, name)
181190
if !found {
182-
return "", "", "", "", false, false
191+
return nil
183192
}
184193
}
185194

186195
if directBind || !ls.AttributesInBind {
187196
// binds user (checking password) before looking-up attributes in user context
188197
err = bindUser(l, userDN, passwd)
189198
if err != nil {
190-
return "", "", "", "", false, false
199+
return nil
191200
}
192201
}
193202

194203
userFilter, ok := ls.sanitizedUserQuery(name)
195204
if !ok {
196-
return "", "", "", "", false, false
205+
return nil
197206
}
198207

199208
log.Trace("Fetching attributes '%v', '%v', '%v', '%v' with filter %s and base %s", ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, userFilter, userDN)
@@ -205,15 +214,15 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
205214
sr, err := l.Search(search)
206215
if err != nil {
207216
log.Error(4, "LDAP Search failed unexpectedly! (%v)", err)
208-
return "", "", "", "", false, false
217+
return nil
209218
} else if len(sr.Entries) < 1 {
210219
if directBind {
211220
log.Error(4, "User filter inhibited user login.")
212221
} else {
213222
log.Error(4, "LDAP Search failed unexpectedly! (0 entries)")
214223
}
215224

216-
return "", "", "", "", false, false
225+
return nil
217226
}
218227

219228
username := sr.Entries[0].GetAttributeValue(ls.AttributeUsername)
@@ -243,9 +252,15 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) (string, str
243252
// binds user (checking password) after looking-up attributes in BindDN context
244253
err = bindUser(l, userDN, passwd)
245254
if err != nil {
246-
return "", "", "", "", false, false
255+
return nil
247256
}
248257
}
249258

250-
return username, firstname, surname, mail, isAdmin, true
259+
return &SearchResult{
260+
Username: username,
261+
Name: firstname,
262+
Surname: surname,
263+
Mail: mail,
264+
IsAdmin: isAdmin,
265+
}
251266
}

0 commit comments

Comments
 (0)