@@ -321,7 +321,6 @@ public UserModel authenticate(String username, char[] password) {
321
321
if (result != null && result .getEntryCount () == 1 ) {
322
322
SearchResultEntry loggingInUser = result .getSearchEntries ().get (0 );
323
323
String loggingInUserDN = loggingInUser .getDN ();
324
-
325
324
if (alreadyAuthenticated || isAuthenticated (ldapConnection , loggingInUserDN , new String (password ))) {
326
325
logger .debug ("LDAP authenticated: " + username );
327
326
@@ -438,7 +437,6 @@ private void setUserAttributes(UserModel user, SearchResultEntry userEntry) {
438
437
439
438
private void getTeamsFromLdap (LDAPConnection ldapConnection , String simpleUsername , SearchResultEntry loggingInUser , UserModel user ) {
440
439
String loggingInUserDN = loggingInUser .getDN ();
441
-
442
440
// Clear the users team memberships - we're going to get them from LDAP
443
441
user .teams .clear ();
444
442
@@ -533,13 +531,22 @@ private SearchResult doSearch(LDAPConnection ldapConnection, String base, boolea
533
531
}
534
532
535
533
private boolean isAuthenticated (LDAPConnection ldapConnection , String userDn , String password ) {
534
+ LDAPConnection authldapConnection = getLdapConnection ();
536
535
try {
537
- // Binding will stop any LDAP-Injection Attacks since the searched-for user needs to bind to that DN
538
- ldapConnection .bind (userDn , password );
536
+ if (settings .getBoolean (Keys .realm .ldap .groupQueryWithUser , false )
537
+ && !StringUtils .isEmpty (settings .getString (Keys .realm .ldap .username , "" )) ) {
538
+ // bind authConnection to user
539
+ authldapConnection .bind (userDn , password );
540
+ } else {
541
+ // Binding will stop any LDAP-Injection Attacks since the searched-for user needs to bind to that DN
542
+ ldapConnection .bind (userDn , password );
543
+ }
539
544
return true ;
540
545
} catch (LDAPException e ) {
541
546
logger .error ("Error authenticating user" , e );
542
547
return false ;
548
+ } finally {
549
+ authldapConnection .close ();
543
550
}
544
551
}
545
552
0 commit comments