Skip to content

Avoid duplicate attribute search #3917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;


/**
* Tests for {@link BindAuthenticator}.
*
Expand Down Expand Up @@ -90,7 +91,9 @@ public void testAuthenticationWithUserSearch() throws Exception {
this.authenticator.setUserSearch(new FilterBasedLdapUserSearch("ou=people",
"(uid={0})", getContextSource()));
this.authenticator.afterPropertiesSet();
this.authenticator.authenticate(this.bob);
DirContextOperations result = this.authenticator.authenticate(this.bob);
//ensure we are getting the same attributes back
assertThat(result.getStringAttribute("cn")).isEqualTo("Bob Hamilton");
// SEC-1444
this.authenticator.setUserSearch(new FilterBasedLdapUserSearch("ou=people",
"(cn={0})", getContextSource()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.springframework.security.ldap.authentication;

import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.NamingException;
Expand All @@ -35,6 +32,9 @@
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;

/**
* An authenticator which binds as a user.
*
Expand Down Expand Up @@ -93,7 +93,8 @@ public DirContextOperations authenticate(Authentication authentication) {
// with the returned DN.
if (user == null && getUserSearch() != null) {
DirContextOperations userFromSearch = getUserSearch().searchForUser(username);
user = bindWithDn(userFromSearch.getDn().toString(), username, password);
user = bindWithDn(userFromSearch.getDn().toString(), username, password,
userFromSearch.getAttributes());
}

if (user == null) {
Expand All @@ -106,6 +107,11 @@ public DirContextOperations authenticate(Authentication authentication) {

private DirContextOperations bindWithDn(String userDnStr, String username,
String password) {
return bindWithDn(userDnStr, username, password, null);
}

private DirContextOperations bindWithDn(String userDnStr, String username,
String password, Attributes attrs) {
BaseLdapPathContextSource ctxSource = (BaseLdapPathContextSource) getContextSource();
DistinguishedName userDn = new DistinguishedName(userDnStr);
DistinguishedName fullDn = new DistinguishedName(userDn);
Expand All @@ -121,8 +127,9 @@ private DirContextOperations bindWithDn(String userDnStr, String username,
.extractControl(ctx);

logger.debug("Retrieving attributes...");

Attributes attrs = ctx.getAttributes(userDn, getUserAttributes());
if (attrs == null || attrs.size()==0) {
attrs = ctx.getAttributes(userDn, getUserAttributes());
}

DirContextAdapter result = new DirContextAdapter(attrs, userDn,
ctxSource.getBaseLdapPath());
Expand Down