Skip to content

Commit

Permalink
Merge pull request #59 from rchincha/ldap
Browse files Browse the repository at this point in the history
authN: first try local htpasswd then LDAP
  • Loading branch information
tych0 authored Jan 15, 2020
2 parents 17ac1be + d64a3e3 commit 92d8f7c
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions pkg/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,17 @@ func BasicAuthHandler(c *Controller) mux.MiddlewareFunc {
username := pair[0]
passphrase := pair[1]

// prefer LDAP if configured
// first, HTTPPassword authN (which is local)
passphraseHash, ok := credMap[username]
if ok {
if err := bcrypt.CompareHashAndPassword([]byte(passphraseHash), []byte(passphrase)); err == nil {
// Process request
next.ServeHTTP(w, r)
return
}
}

// next, LDAP if configured (network-based which can lose connectivity)
if c.Config.HTTP.Auth != nil && c.Config.HTTP.Auth.LDAP != nil {
ok, _, err := ldapClient.Authenticate(username, passphrase)
if ok && err == nil {
Expand All @@ -152,20 +162,8 @@ func BasicAuthHandler(c *Controller) mux.MiddlewareFunc {
}
}

// fallback to HTTPPassword
passphraseHash, ok := credMap[username]
if !ok {
authFail(w, realm, delay)
return
}

if err := bcrypt.CompareHashAndPassword([]byte(passphraseHash), []byte(passphrase)); err != nil {
authFail(w, realm, delay)
return
}

// Process request
next.ServeHTTP(w, r)
authFail(w, realm, delay)
return
})
}
}

0 comments on commit 92d8f7c

Please sign in to comment.