Skip to content

Commit

Permalink
Pass username as second parameter for search filter.
Browse files Browse the repository at this point in the history
Allows the username only (without domain) to be used in custom search filter like "sAMAccountName={1}",
in eg. situations where the userPrincipalName has a different suffix than domain.

Thanks to contributors in issue.

fixes spring-projectsgh-2448

(cherry picked from commit 8d717c6)
  • Loading branch information
taasjord committed Apr 29, 2018
1 parent c683bc1 commit 6937da8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Expand Up @@ -312,7 +312,7 @@ private DirContextOperations searchForUser(DirContext context, String username)
try {
return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context,
searchControls, searchRoot, searchFilter,
new Object[] { bindPrincipal });
new Object[] { bindPrincipal, username });
}
catch (IncorrectResultSizeDataAccessException incorrectResults) {
// Search should never return multiple results if properly configured - just
Expand Down Expand Up @@ -383,7 +383,8 @@ public void setConvertSubErrorCodesToExceptions(

/**
* The LDAP filter string to search for the user being authenticated. Occurrences of
* {0} are replaced with the {@code username@domain}.
* {0} are replaced with the {@code username@domain}. Occurrences of {1} are replaced
* with the {@code username} only.
* <p>
* Defaults to: {@code (&(objectClass=user)(userPrincipalName= 0}))}
* </p>
Expand Down
Expand Up @@ -140,9 +140,9 @@ public void defaultSearchFilter() throws Exception {
any(Object[].class), any(SearchControls.class));
}

// SEC-2897
// SEC-2897,SEC-2224
@Test
public void bindPrincipalUsed() throws Exception {
public void bindPrincipalAndUsernameUsed() throws Exception {
// given
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))";
ArgumentCaptor<Object[]> captor = ArgumentCaptor.forClass(Object[].class);
Expand All @@ -166,7 +166,7 @@ public void bindPrincipalUsed() throws Exception {
Authentication result = customProvider.authenticate(joe);

// then
assertThat(captor.getValue()).containsOnly("joe@mydomain.eu");
assertThat(captor.getValue()).containsExactly("joe@mydomain.eu", "joe");
assertThat(result.isAuthenticated()).isTrue();
}

Expand Down

0 comments on commit 6937da8

Please sign in to comment.