Reformat code using spring-javaformat
Run `./gradlew format` to reformat all java files. Issue gh-8945
This commit is contained in:
@@ -43,8 +43,7 @@ public class LdapUtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRelativeNameReturnsEmptyStringForDnEqualToBaseName()
|
||||
throws Exception {
|
||||
public void testGetRelativeNameReturnsEmptyStringForDnEqualToBaseName() throws Exception {
|
||||
final DirContext mockCtx = mock(DirContext.class);
|
||||
|
||||
when(mockCtx.getNameInNamespace()).thenReturn("dc=springframework,dc=org");
|
||||
@@ -57,7 +56,8 @@ public class LdapUtilsTests {
|
||||
final DirContext mockCtx = mock(DirContext.class);
|
||||
when(mockCtx.getNameInNamespace()).thenReturn("");
|
||||
|
||||
assertThat(LdapUtils.getRelativeName("cn=jane,dc=springframework,dc=org", mockCtx)).isEqualTo("cn=jane,dc=springframework,dc=org");
|
||||
assertThat(LdapUtils.getRelativeName("cn=jane,dc=springframework,dc=org", mockCtx))
|
||||
.isEqualTo("cn=jane,dc=springframework,dc=org");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -65,8 +65,8 @@ public class LdapUtilsTests {
|
||||
final DirContext mockCtx = mock(DirContext.class);
|
||||
when(mockCtx.getNameInNamespace()).thenReturn("dc=springsecurity,dc = org");
|
||||
|
||||
assertThat(LdapUtils.getRelativeName(
|
||||
"cn=jane smith, dc = springsecurity , dc=org", mockCtx)).isEqualTo("cn=jane smith");
|
||||
assertThat(LdapUtils.getRelativeName("cn=jane smith, dc = springsecurity , dc=org", mockCtx))
|
||||
.isEqualTo("cn=jane smith");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,19 +75,16 @@ public class LdapUtilsTests {
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine:11389")).isEqualTo("");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine/")).isEqualTo("");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine.co.uk/")).isEqualTo("");
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldaps://monkeymachine.co.uk/dc=springframework,dc=org")).isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(
|
||||
LdapUtils.parseRootDnFromUrl("ldap:///dc=springframework,dc=org")).isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldap://monkeymachine/dc=springframework,dc=org")).isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldap://monkeymachine.co.uk/dc=springframework,dc=org/ou=blah")).isEqualTo("dc=springframework,dc=org/ou=blah");
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldap://monkeymachine.co.uk:389/dc=springframework,dc=org/ou=blah")).isEqualTo("dc=springframework,dc=org/ou=blah");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldaps://monkeymachine.co.uk/dc=springframework,dc=org"))
|
||||
.isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap:///dc=springframework,dc=org"))
|
||||
.isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine/dc=springframework,dc=org"))
|
||||
.isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine.co.uk/dc=springframework,dc=org/ou=blah"))
|
||||
.isEqualTo("dc=springframework,dc=org/ou=blah");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine.co.uk:389/dc=springframework,dc=org/ou=blah"))
|
||||
.isEqualTo("dc=springframework,dc=org/ou=blah");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.junit.Test;
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class SpringSecurityAuthenticationSourceTests {
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void clearContext() {
|
||||
@@ -51,16 +52,14 @@ public class SpringSecurityAuthenticationSourceTests {
|
||||
AuthenticationSource source = new SpringSecurityAuthenticationSource();
|
||||
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new AnonymousAuthenticationToken("key", "anonUser", AuthorityUtils
|
||||
.createAuthorityList("ignored")));
|
||||
new AnonymousAuthenticationToken("key", "anonUser", AuthorityUtils.createAuthorityList("ignored")));
|
||||
assertThat(source.getPrincipal()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getPrincipalRejectsNonLdapUserDetailsObject() {
|
||||
AuthenticationSource source = new SpringSecurityAuthenticationSource();
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new TestingAuthenticationToken(new Object(), "password"));
|
||||
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new Object(), "password"));
|
||||
|
||||
source.getPrincipal();
|
||||
}
|
||||
@@ -68,8 +67,7 @@ public class SpringSecurityAuthenticationSourceTests {
|
||||
@Test
|
||||
public void expectedCredentialsAreReturned() {
|
||||
AuthenticationSource source = new SpringSecurityAuthenticationSource();
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new TestingAuthenticationToken(new Object(), "password"));
|
||||
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new Object(), "password"));
|
||||
|
||||
assertThat(source.getCredentials()).isEqualTo("password");
|
||||
}
|
||||
@@ -80,9 +78,10 @@ public class SpringSecurityAuthenticationSourceTests {
|
||||
user.setUsername("joe");
|
||||
user.setDn(new DistinguishedName("uid=joe,ou=users"));
|
||||
AuthenticationSource source = new SpringSecurityAuthenticationSource();
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new TestingAuthenticationToken(user.createUserDetails(), null));
|
||||
SecurityContextHolder.getContext()
|
||||
.setAuthentication(new TestingAuthenticationToken(user.createUserDetails(), null));
|
||||
|
||||
assertThat(source.getPrincipal()).isEqualTo("uid=joe,ou=users");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,10 +37,13 @@ public class SpringSecurityLdapTemplateTests {
|
||||
|
||||
@Mock
|
||||
private DirContext ctx;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<SearchControls> searchControls;
|
||||
|
||||
@Mock
|
||||
private NamingEnumeration<SearchResult> resultsEnum;
|
||||
|
||||
@Mock
|
||||
private SearchResult searchResult;
|
||||
|
||||
@@ -53,15 +56,13 @@ public class SpringSecurityLdapTemplateTests {
|
||||
Object[] params = new Object[] {};
|
||||
DirContextAdapter searchResultObject = mock(DirContextAdapter.class);
|
||||
|
||||
when(
|
||||
ctx.search(any(DistinguishedName.class), eq(filter), eq(params),
|
||||
searchControls.capture())).thenReturn(resultsEnum);
|
||||
when(ctx.search(any(DistinguishedName.class), eq(filter), eq(params), searchControls.capture()))
|
||||
.thenReturn(resultsEnum);
|
||||
when(resultsEnum.hasMore()).thenReturn(true, false);
|
||||
when(resultsEnum.next()).thenReturn(searchResult);
|
||||
when(searchResult.getObject()).thenReturn(searchResultObject);
|
||||
|
||||
SpringSecurityLdapTemplate.searchForSingleEntryInternal(ctx,
|
||||
mock(SearchControls.class), base, filter, params);
|
||||
SpringSecurityLdapTemplate.searchForSingleEntryInternal(ctx, mock(SearchControls.class), base, filter, params);
|
||||
|
||||
assertThat(searchControls.getValue().getReturningObjFlag()).isTrue();
|
||||
}
|
||||
|
||||
@@ -51,36 +51,34 @@ public class LdapAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testSupportsUsernamePasswordAuthenticationToken() {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
|
||||
new MockAuthenticator(), new MockAuthoritiesPopulator());
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
|
||||
new MockAuthoritiesPopulator());
|
||||
|
||||
assertThat(ldapProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultMapperIsSet() {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
|
||||
new MockAuthenticator(), new MockAuthoritiesPopulator());
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
|
||||
new MockAuthoritiesPopulator());
|
||||
|
||||
assertThat(ldapProvider.getUserDetailsContextMapper() instanceof LdapUserDetailsMapper).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyOrNullUserNameThrowsException() {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
|
||||
new MockAuthenticator(), new MockAuthoritiesPopulator());
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
|
||||
new MockAuthoritiesPopulator());
|
||||
|
||||
try {
|
||||
ldapProvider.authenticate(new UsernamePasswordAuthenticationToken(null,
|
||||
"password"));
|
||||
ldapProvider.authenticate(new UsernamePasswordAuthenticationToken(null, "password"));
|
||||
fail("Expected BadCredentialsException for empty username");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
|
||||
try {
|
||||
ldapProvider.authenticate(new UsernamePasswordAuthenticationToken("",
|
||||
"bobspassword"));
|
||||
ldapProvider.authenticate(new UsernamePasswordAuthenticationToken("", "bobspassword"));
|
||||
fail("Expected BadCredentialsException for null username");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
@@ -90,26 +88,20 @@ public class LdapAuthenticationProviderTests {
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void usernameNotFoundExceptionIsHiddenByDefault() {
|
||||
final LdapAuthenticator authenticator = mock(LdapAuthenticator.class);
|
||||
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken(
|
||||
"joe", "password");
|
||||
when(authenticator.authenticate(joe)).thenThrow(
|
||||
new UsernameNotFoundException("nobody"));
|
||||
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
|
||||
when(authenticator.authenticate(joe)).thenThrow(new UsernameNotFoundException("nobody"));
|
||||
|
||||
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(
|
||||
authenticator);
|
||||
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(authenticator);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
|
||||
@Test(expected = UsernameNotFoundException.class)
|
||||
public void usernameNotFoundExceptionIsNotHiddenIfConfigured() {
|
||||
final LdapAuthenticator authenticator = mock(LdapAuthenticator.class);
|
||||
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken(
|
||||
"joe", "password");
|
||||
when(authenticator.authenticate(joe)).thenThrow(
|
||||
new UsernameNotFoundException("nobody"));
|
||||
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
|
||||
when(authenticator.authenticate(joe)).thenThrow(new UsernameNotFoundException("nobody"));
|
||||
|
||||
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(
|
||||
authenticator);
|
||||
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(authenticator);
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
@@ -117,16 +109,15 @@ public class LdapAuthenticationProviderTests {
|
||||
@Test
|
||||
public void normalUsage() {
|
||||
MockAuthoritiesPopulator populator = new MockAuthoritiesPopulator();
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
|
||||
new MockAuthenticator(), populator);
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(), populator);
|
||||
LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
|
||||
userMapper.setRoleAttributes(new String[] { "ou" });
|
||||
ldapProvider.setUserDetailsContextMapper(userMapper);
|
||||
|
||||
assertThat(ldapProvider.getAuthoritiesPopulator()).isNotNull();
|
||||
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
|
||||
"ben", "benspassword");
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
"benspassword");
|
||||
Object authDetails = new Object();
|
||||
authRequest.setDetails(authDetails);
|
||||
Authentication authResult = ldapProvider.authenticate(authRequest);
|
||||
@@ -138,20 +129,18 @@ public class LdapAuthenticationProviderTests {
|
||||
assertThat(user.getUsername()).isEqualTo("ben");
|
||||
assertThat(populator.getRequestedUsername()).isEqualTo("ben");
|
||||
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()))
|
||||
.contains("ROLE_FROM_ENTRY");
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()))
|
||||
.contains("ROLE_FROM_POPULATOR");
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_ENTRY");
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_POPULATOR");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void passwordIsSetFromUserDataIfUseAuthenticationRequestCredentialsIsFalse() {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
|
||||
new MockAuthenticator(), new MockAuthoritiesPopulator());
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
|
||||
new MockAuthoritiesPopulator());
|
||||
ldapProvider.setUseAuthenticationRequestCredentials(false);
|
||||
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
|
||||
"ben", "benspassword");
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
"benspassword");
|
||||
Authentication authResult = ldapProvider.authenticate(authRequest);
|
||||
assertThat(authResult.getCredentials()).isEqualTo("{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
|
||||
|
||||
@@ -159,31 +148,26 @@ public class LdapAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void useWithNullAuthoritiesPopulatorReturnsCorrectRole() {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
|
||||
new MockAuthenticator());
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator());
|
||||
LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
|
||||
userMapper.setRoleAttributes(new String[] { "ou" });
|
||||
ldapProvider.setUserDetailsContextMapper(userMapper);
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
|
||||
"ben", "benspassword");
|
||||
UserDetails user = (UserDetails) ldapProvider.authenticate(authRequest)
|
||||
.getPrincipal();
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
"benspassword");
|
||||
UserDetails user = (UserDetails) ldapProvider.authenticate(authRequest).getPrincipal();
|
||||
assertThat(user.getAuthorities()).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()))
|
||||
.contains("ROLE_FROM_ENTRY");
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_ENTRY");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticateWithNamingException() {
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
|
||||
"ben", "benspassword");
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
"benspassword");
|
||||
LdapAuthenticator mockAuthenticator = mock(LdapAuthenticator.class);
|
||||
CommunicationException expectedCause = new CommunicationException(
|
||||
new javax.naming.CommunicationException());
|
||||
CommunicationException expectedCause = new CommunicationException(new javax.naming.CommunicationException());
|
||||
when(mockAuthenticator.authenticate(authRequest)).thenThrow(expectedCause);
|
||||
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
|
||||
mockAuthenticator);
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(mockAuthenticator);
|
||||
try {
|
||||
ldapProvider.authenticate(authRequest);
|
||||
fail("Expected Exception");
|
||||
@@ -205,28 +189,27 @@ public class LdapAuthenticationProviderTests {
|
||||
String password = (String) authentication.getCredentials();
|
||||
|
||||
if (username.equals("ben") && password.equals("benspassword")) {
|
||||
ctx.setDn(new DistinguishedName(
|
||||
"cn=ben,ou=people,dc=springframework,dc=org"));
|
||||
ctx.setDn(new DistinguishedName("cn=ben,ou=people,dc=springframework,dc=org"));
|
||||
ctx.setAttributeValue("userPassword", "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
|
||||
|
||||
return ctx;
|
||||
}
|
||||
else if (username.equals("jen") && password.equals("")) {
|
||||
ctx.setDn(new DistinguishedName(
|
||||
"cn=jen,ou=people,dc=springframework,dc=org"));
|
||||
ctx.setDn(new DistinguishedName("cn=jen,ou=people,dc=springframework,dc=org"));
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
throw new BadCredentialsException("Authentication failed.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MockAuthoritiesPopulator implements LdapAuthoritiesPopulator {
|
||||
|
||||
String username;
|
||||
|
||||
public Collection<GrantedAuthority> getGrantedAuthorities(
|
||||
DirContextOperations userCtx, String username) {
|
||||
public Collection<GrantedAuthority> getGrantedAuthorities(DirContextOperations userCtx, String username) {
|
||||
this.username = username;
|
||||
return AuthorityUtils.createAuthorityList("ROLE_FROM_POPULATOR");
|
||||
}
|
||||
@@ -234,5 +217,7 @@ public class LdapAuthenticationProviderTests {
|
||||
String getRequestedUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,11 +21,10 @@ import org.springframework.security.ldap.search.LdapUserSearch;
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class MockUserSearch implements LdapUserSearch {
|
||||
|
||||
// ~ Instance fields
|
||||
// ================================================================================================
|
||||
|
||||
@@ -47,4 +46,5 @@ public class MockUserSearch implements LdapUserSearch {
|
||||
public DirContextOperations searchForUser(String username) {
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ import org.springframework.ldap.core.support.BaseLdapPathContextSource;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class PasswordComparisonAuthenticatorMockTests {
|
||||
@@ -44,26 +43,22 @@ public class PasswordComparisonAuthenticatorMockTests {
|
||||
final BasicAttributes attrs = new BasicAttributes();
|
||||
attrs.put(new BasicAttribute("uid", "bob"));
|
||||
|
||||
PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator(
|
||||
source);
|
||||
PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator(source);
|
||||
|
||||
authenticator.setUserDnPatterns(new String[] { "cn={0},ou=people" });
|
||||
|
||||
// Get the mock to return an empty attribute set
|
||||
when(source.getReadOnlyContext()).thenReturn(dirCtx);
|
||||
when(dirCtx.getAttributes(eq("cn=Bob,ou=people"), any(String[].class)))
|
||||
.thenReturn(attrs);
|
||||
when(dirCtx.getAttributes(eq("cn=Bob,ou=people"), any(String[].class))).thenReturn(attrs);
|
||||
when(dirCtx.getNameInNamespace()).thenReturn("dc=springframework,dc=org");
|
||||
|
||||
// Setup a single return value (i.e. success)
|
||||
final NamingEnumeration searchResults = new BasicAttributes("", null).getAll();
|
||||
|
||||
when(
|
||||
dirCtx.search(eq("cn=Bob,ou=people"), eq("(userPassword={0})"),
|
||||
any(Object[].class), any(SearchControls.class))).thenReturn(
|
||||
searchResults);
|
||||
when(dirCtx.search(eq("cn=Bob,ou=people"), eq("(userPassword={0})"), any(Object[].class),
|
||||
any(SearchControls.class))).thenReturn(searchResults);
|
||||
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("Bob",
|
||||
"bobspassword"));
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("Bob", "bobspassword"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -64,20 +64,21 @@ import static org.springframework.security.ldap.authentication.ad.ActiveDirector
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
|
||||
public static final String EXISTING_LDAP_PROVIDER = "ldap://192.168.1.200/";
|
||||
|
||||
public static final String NON_EXISTING_LDAP_PROVIDER = "ldap://192.168.1.201/";
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
ActiveDirectoryLdapAuthenticationProvider provider;
|
||||
UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken(
|
||||
"joe", "password");
|
||||
|
||||
UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
provider = new ActiveDirectoryLdapAuthenticationProvider("mydomain.eu",
|
||||
"ldap://192.168.1.200/");
|
||||
provider = new ActiveDirectoryLdapAuthenticationProvider("mydomain.eu", "ldap://192.168.1.200/");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -101,12 +102,9 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
when(ctx.getNameInNamespace()).thenReturn("");
|
||||
|
||||
DirContextAdapter dca = new DirContextAdapter();
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca,
|
||||
dca.getAttributes());
|
||||
when(
|
||||
ctx.search(any(Name.class), eq(customSearchFilter), any(Object[].class),
|
||||
any(SearchControls.class))).thenReturn(
|
||||
new MockNamingEnumeration(sr));
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
|
||||
when(ctx.search(any(Name.class), eq(customSearchFilter), any(Object[].class), any(SearchControls.class)))
|
||||
.thenReturn(new MockNamingEnumeration(sr));
|
||||
|
||||
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider(
|
||||
"mydomain.eu", "ldap://192.168.1.200/");
|
||||
@@ -129,12 +127,9 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
when(ctx.getNameInNamespace()).thenReturn("");
|
||||
|
||||
DirContextAdapter dca = new DirContextAdapter();
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca,
|
||||
dca.getAttributes());
|
||||
when(
|
||||
ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class),
|
||||
any(SearchControls.class))).thenReturn(
|
||||
new MockNamingEnumeration(sr));
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
|
||||
when(ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class)))
|
||||
.thenReturn(new MockNamingEnumeration(sr));
|
||||
|
||||
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider(
|
||||
"mydomain.eu", "ldap://192.168.1.200/");
|
||||
@@ -145,8 +140,8 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
|
||||
// then
|
||||
assertThat(result.isAuthenticated()).isTrue();
|
||||
verify(ctx).search(any(DistinguishedName.class), eq(defaultSearchFilter),
|
||||
any(Object[].class), any(SearchControls.class));
|
||||
verify(ctx).search(any(DistinguishedName.class), eq(defaultSearchFilter), any(Object[].class),
|
||||
any(SearchControls.class));
|
||||
}
|
||||
|
||||
// SEC-2897,SEC-2224
|
||||
@@ -160,12 +155,9 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
when(ctx.getNameInNamespace()).thenReturn("");
|
||||
|
||||
DirContextAdapter dca = new DirContextAdapter();
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca,
|
||||
dca.getAttributes());
|
||||
when(
|
||||
ctx.search(any(Name.class), eq(defaultSearchFilter), captor.capture(),
|
||||
any(SearchControls.class))).thenReturn(
|
||||
new MockNamingEnumeration(sr));
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
|
||||
when(ctx.search(any(Name.class), eq(defaultSearchFilter), captor.capture(), any(SearchControls.class)))
|
||||
.thenReturn(new MockNamingEnumeration(sr));
|
||||
|
||||
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider(
|
||||
"mydomain.eu", "ldap://192.168.1.200/");
|
||||
@@ -190,20 +182,15 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal()
|
||||
throws Exception {
|
||||
provider = new ActiveDirectoryLdapAuthenticationProvider(null,
|
||||
"ldap://192.168.1.200/");
|
||||
public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws Exception {
|
||||
provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://192.168.1.200/");
|
||||
DirContext ctx = mock(DirContext.class);
|
||||
when(ctx.getNameInNamespace()).thenReturn("");
|
||||
|
||||
DirContextAdapter dca = new DirContextAdapter();
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca,
|
||||
dca.getAttributes());
|
||||
when(
|
||||
ctx.search(eq(new DistinguishedName("DC=mydomain,DC=eu")),
|
||||
any(String.class), any(Object[].class), any(SearchControls.class)))
|
||||
.thenReturn(new MockNamingEnumeration(sr));
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
|
||||
when(ctx.search(eq(new DistinguishedName("DC=mydomain,DC=eu")), any(String.class), any(Object[].class),
|
||||
any(SearchControls.class))).thenReturn(new MockNamingEnumeration(sr));
|
||||
provider.contextFactory = createContextFactoryReturning(ctx);
|
||||
|
||||
try {
|
||||
@@ -213,17 +200,14 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
|
||||
provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu",
|
||||
"password"));
|
||||
provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu", "password"));
|
||||
}
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void failedUserSearchCausesBadCredentials() throws Exception {
|
||||
DirContext ctx = mock(DirContext.class);
|
||||
when(ctx.getNameInNamespace()).thenReturn("");
|
||||
when(
|
||||
ctx.search(any(Name.class), any(String.class), any(Object[].class),
|
||||
any(SearchControls.class)))
|
||||
when(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class)))
|
||||
.thenThrow(new NameNotFoundException());
|
||||
|
||||
provider.contextFactory = createContextFactoryReturning(ctx);
|
||||
@@ -236,10 +220,8 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
public void noUserSearchCausesUsernameNotFound() throws Exception {
|
||||
DirContext ctx = mock(DirContext.class);
|
||||
when(ctx.getNameInNamespace()).thenReturn("");
|
||||
when(
|
||||
ctx.search(any(Name.class), any(String.class), any(Object[].class),
|
||||
any(SearchControls.class))).thenReturn(
|
||||
new EmptyEnumeration<>());
|
||||
when(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class)))
|
||||
.thenReturn(new EmptyEnumeration<>());
|
||||
|
||||
provider.contextFactory = createContextFactoryReturning(ctx);
|
||||
|
||||
@@ -260,12 +242,10 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
NamingEnumeration<SearchResult> searchResults = mock(NamingEnumeration.class);
|
||||
when(searchResults.hasMore()).thenReturn(true, true, false);
|
||||
SearchResult searchResult = mock(SearchResult.class);
|
||||
when(searchResult.getObject()).thenReturn(new DirContextAdapter("ou=1"),
|
||||
new DirContextAdapter("ou=2"));
|
||||
when(searchResult.getObject()).thenReturn(new DirContextAdapter("ou=1"), new DirContextAdapter("ou=2"));
|
||||
when(searchResults.next()).thenReturn(searchResult);
|
||||
when(
|
||||
ctx.search(any(Name.class), any(String.class), any(Object[].class),
|
||||
any(SearchControls.class))).thenReturn(searchResults);
|
||||
when(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class)))
|
||||
.thenReturn(searchResults);
|
||||
|
||||
provider.contextFactory = createContextFactoryReturning(ctx);
|
||||
|
||||
@@ -276,24 +256,21 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void userNotFoundIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "525, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "525, xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void incorrectPasswordIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "52e, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "52e, xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void notPermittedIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "530, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "530, xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
@@ -301,22 +278,20 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
@Test
|
||||
public void passwordNeedsResetIsCorrectlyMapped() {
|
||||
final String dataCode = "773";
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + dataCode + ", xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + dataCode + ", xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
|
||||
thrown.expect(BadCredentialsException.class);
|
||||
thrown.expect(new BaseMatcher<BadCredentialsException>() {
|
||||
private Matcher<Object> causeInstance = CoreMatchers
|
||||
.instanceOf(ActiveDirectoryAuthenticationException.class);
|
||||
|
||||
private Matcher<String> causeDataCode = CoreMatchers.equalTo(dataCode);
|
||||
|
||||
public boolean matches(Object that) {
|
||||
Throwable t = (Throwable) that;
|
||||
ActiveDirectoryAuthenticationException cause = (ActiveDirectoryAuthenticationException) t
|
||||
.getCause();
|
||||
return causeInstance.matches(cause)
|
||||
&& causeDataCode.matches(cause.getDataCode());
|
||||
ActiveDirectoryAuthenticationException cause = (ActiveDirectoryAuthenticationException) t.getCause();
|
||||
return causeInstance.matches(cause) && causeDataCode.matches(cause.getDataCode());
|
||||
}
|
||||
|
||||
public void describeTo(Description desc) {
|
||||
@@ -332,8 +307,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
|
||||
@Test(expected = CredentialsExpiredException.class)
|
||||
public void expiredPasswordIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "532, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "532, xxxx]"));
|
||||
|
||||
try {
|
||||
provider.authenticate(joe);
|
||||
@@ -348,40 +322,35 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
|
||||
@Test(expected = DisabledException.class)
|
||||
public void accountDisabledIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "533, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "533, xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
|
||||
@Test(expected = AccountExpiredException.class)
|
||||
public void accountExpiredIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "701, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "701, xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
|
||||
@Test(expected = LockedException.class)
|
||||
public void accountLockedIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "775, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "775, xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void unknownErrorCodeIsCorrectlyMapped() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg + "999, xxxx]"));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "999, xxxx]"));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void errorWithNoSubcodeIsHandledCleanly() {
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(
|
||||
msg));
|
||||
provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg));
|
||||
provider.setConvertSubErrorCodesToExceptions(true);
|
||||
provider.authenticate(joe);
|
||||
}
|
||||
@@ -389,22 +358,23 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
@Test(expected = org.springframework.ldap.CommunicationException.class)
|
||||
public void nonAuthenticationExceptionIsConvertedToSpringLdapException() throws Throwable {
|
||||
try {
|
||||
provider.contextFactory = createContextFactoryThrowing(new CommunicationException(
|
||||
msg));
|
||||
provider.contextFactory = createContextFactoryThrowing(new CommunicationException(msg));
|
||||
provider.authenticate(joe);
|
||||
} catch (InternalAuthenticationServiceException e) {
|
||||
// Since GH-8418 ldap communication exception is wrapped into InternalAuthenticationServiceException.
|
||||
}
|
||||
catch (InternalAuthenticationServiceException e) {
|
||||
// Since GH-8418 ldap communication exception is wrapped into
|
||||
// InternalAuthenticationServiceException.
|
||||
// This test is about the wrapped exception, so we throw it.
|
||||
throw e.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = org.springframework.security.authentication.InternalAuthenticationServiceException.class )
|
||||
@Test(expected = org.springframework.security.authentication.InternalAuthenticationServiceException.class)
|
||||
public void connectionExceptionIsWrappedInInternalException() throws Exception {
|
||||
ActiveDirectoryLdapAuthenticationProvider noneReachableProvider = new ActiveDirectoryLdapAuthenticationProvider(
|
||||
"mydomain.eu", NON_EXISTING_LDAP_PROVIDER, "dc=ad,dc=eu,dc=mydomain");
|
||||
noneReachableProvider.setContextEnvironmentProperties(
|
||||
Collections.singletonMap("com.sun.jndi.ldap.connect.timeout", "5"));
|
||||
noneReachableProvider
|
||||
.setContextEnvironmentProperties(Collections.singletonMap("com.sun.jndi.ldap.connect.timeout", "5"));
|
||||
noneReachableProvider.doAuthentication(joe);
|
||||
}
|
||||
|
||||
@@ -439,8 +409,8 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
}
|
||||
catch (InternalAuthenticationServiceException expected) {
|
||||
assertThat(expected.getCause()).isInstanceOf(org.springframework.ldap.CommunicationException.class);
|
||||
org.springframework.ldap.CommunicationException cause =
|
||||
(org.springframework.ldap.CommunicationException) expected.getCause();
|
||||
org.springframework.ldap.CommunicationException cause = (org.springframework.ldap.CommunicationException) expected
|
||||
.getCause();
|
||||
assertThat(cause.getRootCause()).isInstanceOf(ClassNotFoundException.class);
|
||||
}
|
||||
}
|
||||
@@ -463,20 +433,17 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
};
|
||||
}
|
||||
|
||||
private void checkAuthentication(String rootDn,
|
||||
ActiveDirectoryLdapAuthenticationProvider provider) throws NamingException {
|
||||
private void checkAuthentication(String rootDn, ActiveDirectoryLdapAuthenticationProvider provider)
|
||||
throws NamingException {
|
||||
DirContext ctx = mock(DirContext.class);
|
||||
when(ctx.getNameInNamespace()).thenReturn("");
|
||||
|
||||
DirContextAdapter dca = new DirContextAdapter();
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca,
|
||||
dca.getAttributes());
|
||||
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
|
||||
@SuppressWarnings("deprecation")
|
||||
DistinguishedName searchBaseDn = new DistinguishedName(rootDn);
|
||||
when(
|
||||
ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class),
|
||||
any(SearchControls.class))).thenReturn(
|
||||
new MockNamingEnumeration(sr)).thenReturn(new MockNamingEnumeration(sr));
|
||||
when(ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class), any(SearchControls.class)))
|
||||
.thenReturn(new MockNamingEnumeration(sr)).thenReturn(new MockNamingEnumeration(sr));
|
||||
|
||||
provider.contextFactory = createContextFactoryReturning(ctx);
|
||||
|
||||
@@ -492,6 +459,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
}
|
||||
|
||||
static class MockNamingEnumeration implements NamingEnumeration<SearchResult> {
|
||||
|
||||
private SearchResult sr;
|
||||
|
||||
MockNamingEnumeration(SearchResult sr) {
|
||||
@@ -518,5 +486,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
public SearchResult nextElement() {
|
||||
return next();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ package org.springframework.security.ldap.ppolicy;
|
||||
public class OpenLDAPIntegrationTestSuite {
|
||||
|
||||
PasswordPolicyAwareContextSource cs;
|
||||
|
||||
/*
|
||||
* @Before public void createContextSource() throws Exception { cs = new
|
||||
* PasswordPolicyAwareContextSource("ldap://localhost:22389/dc=springsource,dc=com");
|
||||
@@ -60,4 +61,5 @@ public class OpenLDAPIntegrationTestSuite {
|
||||
* = (LdapUserDetailsImpl) a.getPrincipal(); assertTrue(ud.getTimeBeforeExpiration() <
|
||||
* Integer.MAX_VALUE && ud.getTimeBeforeExpiration() > 0); }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -32,14 +32,15 @@ import java.util.*;
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class PasswordPolicyAwareContextSourceTests {
|
||||
|
||||
private PasswordPolicyAwareContextSource ctxSource;
|
||||
|
||||
private final LdapContext ctx = mock(LdapContext.class);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
reset(ctx);
|
||||
ctxSource = new PasswordPolicyAwareContextSource(
|
||||
"ldap://blah:789/dc=springframework,dc=org") {
|
||||
ctxSource = new PasswordPolicyAwareContextSource("ldap://blah:789/dc=springframework,dc=org") {
|
||||
@Override
|
||||
protected DirContext createContext(Hashtable env) {
|
||||
if ("manager".equals(env.get(Context.SECURITY_PRINCIPAL))) {
|
||||
@@ -60,24 +61,20 @@ public class PasswordPolicyAwareContextSourceTests {
|
||||
}
|
||||
|
||||
@Test(expected = UncategorizedLdapException.class)
|
||||
public void standardExceptionIsPropagatedWhenExceptionRaisedAndNoControlsAreSet()
|
||||
throws Exception {
|
||||
doThrow(new NamingException("some LDAP exception")).when(ctx).reconnect(
|
||||
any(Control[].class));
|
||||
public void standardExceptionIsPropagatedWhenExceptionRaisedAndNoControlsAreSet() throws Exception {
|
||||
doThrow(new NamingException("some LDAP exception")).when(ctx).reconnect(any(Control[].class));
|
||||
|
||||
ctxSource.getContext("user", "ignored");
|
||||
}
|
||||
|
||||
@Test(expected = PasswordPolicyException.class)
|
||||
public void lockedPasswordPolicyControlRaisesPasswordPolicyException()
|
||||
throws Exception {
|
||||
when(ctx.getResponseControls()).thenReturn(
|
||||
new Control[] { new PasswordPolicyResponseControl(
|
||||
PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL) });
|
||||
public void lockedPasswordPolicyControlRaisesPasswordPolicyException() throws Exception {
|
||||
when(ctx.getResponseControls()).thenReturn(new Control[] {
|
||||
new PasswordPolicyResponseControl(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL) });
|
||||
|
||||
doThrow(new NamingException("locked message")).when(ctx).reconnect(
|
||||
any(Control[].class));
|
||||
doThrow(new NamingException("locked message")).when(ctx).reconnect(any(Control[].class));
|
||||
|
||||
ctxSource.getContext("user", "ignored");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,10 +42,10 @@ public class PasswordPolicyControlFactoryTests {
|
||||
Control control = mock(Control.class);
|
||||
|
||||
when(control.getID()).thenReturn(PasswordPolicyControl.OID);
|
||||
when(control.getEncodedValue()).thenReturn(
|
||||
PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL);
|
||||
when(control.getEncodedValue()).thenReturn(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL);
|
||||
Control result = ctrlFactory.getControlInstance(control);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL).isEqualTo(result.getEncodedValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.Test;
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class PasswordPolicyResponseControlTests {
|
||||
|
||||
// ~ Methods
|
||||
// ========================================================================================================
|
||||
|
||||
@@ -94,8 +95,7 @@ public class PasswordPolicyResponseControlTests {
|
||||
|
||||
@Test
|
||||
public void openLDAP496GraceLoginsRemainingCtrlIsParsedCorrectly() {
|
||||
byte[] ctrlBytes = { 0x30, 0x06, (byte) 0xA0, 0x04, (byte) 0xA1, 0x02, 0x01,
|
||||
(byte) 0xF0 };
|
||||
byte[] ctrlBytes = { 0x30, 0x06, (byte) 0xA0, 0x04, (byte) 0xA1, 0x02, 0x01, (byte) 0xF0 };
|
||||
|
||||
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(ctrlBytes);
|
||||
|
||||
@@ -103,13 +103,11 @@ public class PasswordPolicyResponseControlTests {
|
||||
assertThat(ctrl.getGraceLoginsRemaining()).isEqualTo(496);
|
||||
}
|
||||
|
||||
static final byte[] OPENLDAP_5_LOGINS_REMAINING_CTRL = { 0x30, 0x05, (byte) 0xA0,
|
||||
0x03, (byte) 0xA1, 0x01, 0x05 };
|
||||
static final byte[] OPENLDAP_5_LOGINS_REMAINING_CTRL = { 0x30, 0x05, (byte) 0xA0, 0x03, (byte) 0xA1, 0x01, 0x05 };
|
||||
|
||||
@Test
|
||||
public void openLDAP5GraceLoginsRemainingCtrlIsParsedCorrectly() {
|
||||
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(
|
||||
OPENLDAP_5_LOGINS_REMAINING_CTRL);
|
||||
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(OPENLDAP_5_LOGINS_REMAINING_CTRL);
|
||||
|
||||
assertThat(ctrl.hasWarning()).isTrue();
|
||||
assertThat(ctrl.getGraceLoginsRemaining()).isEqualTo(5);
|
||||
@@ -119,8 +117,7 @@ public class PasswordPolicyResponseControlTests {
|
||||
|
||||
@Test
|
||||
public void openLDAPAccountLockedCtrlIsParsedCorrectly() {
|
||||
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(
|
||||
OPENLDAP_LOCKED_CTRL);
|
||||
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(OPENLDAP_LOCKED_CTRL);
|
||||
|
||||
assertThat(ctrl.hasError() && ctrl.isLocked()).isTrue();
|
||||
assertThat(ctrl.hasWarning()).isFalse();
|
||||
@@ -135,4 +132,5 @@ public class PasswordPolicyResponseControlTests {
|
||||
assertThat(ctrl.hasError() && ctrl.isExpired()).isTrue();
|
||||
assertThat(ctrl.hasWarning()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -95,11 +95,10 @@ public class InetOrgPersonTests {
|
||||
public void mappingBackToContextMatchesOriginalData() {
|
||||
DirContextAdapter ctx1 = createUserContext();
|
||||
DirContextAdapter ctx2 = new DirContextAdapter();
|
||||
ctx1.setAttributeValues("objectclass", new String[] { "top", "person",
|
||||
"organizationalPerson", "inetOrgPerson" });
|
||||
ctx1.setAttributeValues("objectclass",
|
||||
new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
|
||||
ctx2.setDn(new DistinguishedName("ignored=ignored"));
|
||||
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1))
|
||||
.createUserDetails();
|
||||
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
|
||||
p.populateContext(ctx2);
|
||||
|
||||
assertThat(ctx2).isEqualTo(ctx1);
|
||||
@@ -110,12 +109,10 @@ public class InetOrgPersonTests {
|
||||
DirContextAdapter ctx1 = createUserContext();
|
||||
DirContextAdapter ctx2 = new DirContextAdapter();
|
||||
ctx2.setDn(new DistinguishedName("ignored=ignored"));
|
||||
ctx1.setAttributeValues("objectclass", new String[] { "top", "person",
|
||||
"organizationalPerson", "inetOrgPerson" });
|
||||
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1))
|
||||
.createUserDetails();
|
||||
InetOrgPerson p2 = (InetOrgPerson) new InetOrgPerson.Essence(p)
|
||||
.createUserDetails();
|
||||
ctx1.setAttributeValues("objectclass",
|
||||
new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
|
||||
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
|
||||
InetOrgPerson p2 = (InetOrgPerson) new InetOrgPerson.Essence(p).createUserDetails();
|
||||
p2.populateContext(ctx2);
|
||||
|
||||
assertThat(ctx2).isEqualTo(ctx1);
|
||||
|
||||
@@ -32,14 +32,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class LdapAuthorityTests {
|
||||
|
||||
public static final String DN = "cn=filip,ou=Users,dc=test,dc=com";
|
||||
|
||||
LdapAuthority authority;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Map<String, List<String>> attributes = new HashMap<>();
|
||||
attributes.put(SpringSecurityLdapTemplate.DN_KEY, Arrays.asList(DN));
|
||||
attributes.put("mail",
|
||||
Arrays.asList("filip@ldap.test.org", "filip@ldap.test2.org"));
|
||||
attributes.put("mail", Arrays.asList("filip@ldap.test.org", "filip@ldap.test2.org"));
|
||||
authority = new LdapAuthority("testRole", DN, attributes);
|
||||
}
|
||||
|
||||
@@ -66,4 +66,5 @@ public class LdapAuthorityTests {
|
||||
assertThat(authority.getAuthority()).isNotNull();
|
||||
assertThat(authority.getAuthority()).isEqualTo("testRole");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,8 +48,8 @@ public class LdapUserDetailsMapperTests {
|
||||
ctx.setAttributeValues("userRole", new String[] { "X", "Y", "Z" });
|
||||
ctx.setAttributeValue("uid", "ani");
|
||||
|
||||
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx,
|
||||
"ani", AuthorityUtils.NO_AUTHORITIES);
|
||||
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
|
||||
AuthorityUtils.NO_AUTHORITIES);
|
||||
|
||||
assertThat(user.getAuthorities()).hasSize(3);
|
||||
}
|
||||
@@ -66,12 +66,11 @@ public class LdapUserDetailsMapperTests {
|
||||
BasicAttributes attrs = new BasicAttributes();
|
||||
attrs.put(new BasicAttribute("userRole", "x"));
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs,
|
||||
new DistinguishedName("cn=someName"));
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
|
||||
ctx.setAttributeValue("uid", "ani");
|
||||
|
||||
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx,
|
||||
"ani", AuthorityUtils.NO_AUTHORITIES);
|
||||
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
|
||||
AuthorityUtils.NO_AUTHORITIES);
|
||||
|
||||
assertThat(user.getAuthorities()).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_X");
|
||||
@@ -85,8 +84,7 @@ public class LdapUserDetailsMapperTests {
|
||||
BasicAttributes attrs = new BasicAttributes();
|
||||
attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs,
|
||||
new DistinguishedName("cn=someName"));
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
|
||||
ctx.setAttributeValue("uid", "ani");
|
||||
|
||||
LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
|
||||
|
||||
@@ -49,36 +49,34 @@ public class LdapUserDetailsServiceTests {
|
||||
|
||||
@Test
|
||||
public void correctAuthoritiesAreReturned() {
|
||||
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName(
|
||||
"uid=joe"));
|
||||
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
|
||||
|
||||
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(
|
||||
userData), new MockAuthoritiesPopulator());
|
||||
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData),
|
||||
new MockAuthoritiesPopulator());
|
||||
service.setUserDetailsMapper(new LdapUserDetailsMapper());
|
||||
|
||||
UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
|
||||
|
||||
Set<String> authorities = AuthorityUtils
|
||||
.authorityListToSet(user.getAuthorities());
|
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(user.getAuthorities());
|
||||
assertThat(authorities).hasSize(1);
|
||||
assertThat(authorities.contains("ROLE_FROM_POPULATOR")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullPopulatorConstructorReturnsEmptyAuthoritiesList() {
|
||||
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName(
|
||||
"uid=joe"));
|
||||
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
|
||||
|
||||
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(
|
||||
userData));
|
||||
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData));
|
||||
UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
|
||||
assertThat(user.getAuthorities()).isEmpty();
|
||||
}
|
||||
|
||||
class MockAuthoritiesPopulator implements LdapAuthoritiesPopulator {
|
||||
public Collection<GrantedAuthority> getGrantedAuthorities(
|
||||
DirContextOperations userCtx, String username) {
|
||||
|
||||
public Collection<GrantedAuthority> getGrantedAuthorities(DirContextOperations userCtx, String username) {
|
||||
return AuthorityUtils.createAuthorityList("ROLE_FROM_POPULATOR");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,12 +42,11 @@ public class UserDetailsServiceLdapAuthoritiesPopulatorTests {
|
||||
List authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
||||
when(user.getAuthorities()).thenReturn(authorities);
|
||||
|
||||
UserDetailsServiceLdapAuthoritiesPopulator populator = new UserDetailsServiceLdapAuthoritiesPopulator(
|
||||
uds);
|
||||
Collection<? extends GrantedAuthority> auths = populator.getGrantedAuthorities(
|
||||
new DirContextAdapter(), "joe");
|
||||
UserDetailsServiceLdapAuthoritiesPopulator populator = new UserDetailsServiceLdapAuthoritiesPopulator(uds);
|
||||
Collection<? extends GrantedAuthority> auths = populator.getGrantedAuthorities(new DirContextAdapter(), "joe");
|
||||
|
||||
assertThat(auths).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(auths).contains("ROLE_USER")).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user