Skip to content

Commit 64fac47

Browse files
authored
Merge pull request #62 from wiltonsr/fix-issue-61
Use same operation mode to bind validations
2 parents 5de938d + 059dce2 commit 64fac47

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

ldapauth.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,6 @@ func (la *LdapAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
175175

176176
LoggerDEBUG.Println("No session found! Trying to authenticate in LDAP")
177177

178-
var certPool *x509.CertPool
179-
180-
if la.config.CertificateAuthority != "" {
181-
certPool = x509.NewCertPool()
182-
certPool.AppendCertsFromPEM([]byte(la.config.CertificateAuthority))
183-
}
184-
185178
conn, err := Connect(la.config)
186179
if err != nil {
187180
LoggerERROR.Printf("%s", err)
@@ -270,8 +263,13 @@ func LdapCheckUser(conn *ldap.Conn, config *Config, username, password string) (
270263
userDN := result.Entries[0].DN
271264
LoggerINFO.Printf("Authenticating User: %s", userDN)
272265

266+
// Create a new conn to validate user password. This prevents changing the bind made
267+
// previously, then LdapCheckUserAuthorized will use same operation mode
268+
_nconn, _ := Connect(config)
269+
defer _nconn.Close()
270+
273271
// Bind User and password.
274-
err = conn.Bind(userDN, password)
272+
err = _nconn.Bind(userDN, password)
275273
return err == nil, result.Entries[0], err
276274
}
277275

@@ -354,6 +352,13 @@ func LdapCheckUserGroups(conn *ldap.Conn, config *Config, entry *ldap.Entry, use
354352

355353
LoggerDEBUG.Printf("Group Filter: '%s'", group_filter.String())
356354

355+
res, err := conn.WhoAmI(nil)
356+
if err != nil {
357+
LoggerERROR.Printf("Failed to call WhoAmI(): %s", err)
358+
} else {
359+
LoggerDEBUG.Printf("Using credential: '%s' for Search Groups", res.AuthzID)
360+
}
361+
357362
for _, g := range config.AllowedGroups {
358363

359364
LoggerDEBUG.Printf("Searching Group: '%s' with User: '%s'", g, entry.DN)

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ labels:
101101
102102
## Operations Mode
103103
104+
The `Operation Mode` detected will be used to perform all subsequent requests.
105+
104106
### Bind Mode
105107

106108
If no `searchFilter` is specified in its configuration, the middleware runs in the default bind mode, meaning it tries to make a simple bind request to the LDAP server with the credentials provided in the request headers. If the bind succeeds, the middleware forwards the request, otherwise, it returns a 401 Unauthorized status code.

0 commit comments

Comments
 (0)