|
@@ -69,10 +69,9 @@ public class ProviderManagerTests {
|
|
@Test
|
|
@Test
|
|
public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() throws Exception {
|
|
public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() throws Exception {
|
|
final Authentication a = mock(Authentication.class);
|
|
final Authentication a = mock(Authentication.class);
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
|
|
|
+ ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichReturns(a)));
|
|
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
|
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
|
mgr.setAuthenticationEventPublisher(publisher);
|
|
mgr.setAuthenticationEventPublisher(publisher);
|
|
- mgr.setProviders(Arrays.asList(createProviderWhichReturns(a)));
|
|
|
|
|
|
|
|
Authentication result = mgr.authenticate(a);
|
|
Authentication result = mgr.authenticate(a);
|
|
assertEquals(a, result);
|
|
assertEquals(a, result);
|
|
@@ -82,37 +81,24 @@ public class ProviderManagerTests {
|
|
@Test
|
|
@Test
|
|
public void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates() {
|
|
public void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates() {
|
|
final Authentication a = mock(Authentication.class);
|
|
final Authentication a = mock(Authentication.class);
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
|
|
|
+ ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichReturns(null), createProviderWhichReturns(a)));
|
|
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
|
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
|
mgr.setAuthenticationEventPublisher(publisher);
|
|
mgr.setAuthenticationEventPublisher(publisher);
|
|
- mgr.setProviders(Arrays.asList(createProviderWhichReturns(null), createProviderWhichReturns(a)));
|
|
|
|
|
|
|
|
Authentication result = mgr.authenticate(a);
|
|
Authentication result = mgr.authenticate(a);
|
|
assertSame(a, result);
|
|
assertSame(a, result);
|
|
verify(publisher).publishAuthenticationSuccess(result);
|
|
verify(publisher).publishAuthenticationSuccess(result);
|
|
}
|
|
}
|
|
|
|
|
|
- @Test(expected=IllegalArgumentException.class)
|
|
|
|
- public void startupFailsIfProviderListDoesNotContainProviders() throws Exception {
|
|
|
|
- List<Object> providers = new ArrayList<Object>();
|
|
|
|
- providers.add("THIS_IS_NOT_A_PROVIDER");
|
|
|
|
-
|
|
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
|
-
|
|
|
|
- mgr.setProviders(providers);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
@Test(expected=IllegalArgumentException.class)
|
|
@Test(expected=IllegalArgumentException.class)
|
|
public void testStartupFailsIfProvidersNotSet() throws Exception {
|
|
public void testStartupFailsIfProvidersNotSet() throws Exception {
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
|
- mgr.afterPropertiesSet();
|
|
|
|
|
|
+ new ProviderManager(null);
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() throws Exception {
|
|
public void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() throws Exception {
|
|
Object requestDetails = "(Request Details)";
|
|
Object requestDetails = "(Request Details)";
|
|
final Object resultDetails = "(Result Details)";
|
|
final Object resultDetails = "(Result Details)";
|
|
- ProviderManager authMgr = makeProviderManager();
|
|
|
|
|
|
|
|
// A provider which sets the details object
|
|
// A provider which sets the details object
|
|
AuthenticationProvider provider = new AuthenticationProvider() {
|
|
AuthenticationProvider provider = new AuthenticationProvider() {
|
|
@@ -126,7 +112,7 @@ public class ProviderManagerTests {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- authMgr.setProviders(Arrays.asList(provider));
|
|
|
|
|
|
+ ProviderManager authMgr = new ProviderManager(Arrays.asList(provider));
|
|
|
|
|
|
TestingAuthenticationToken request = createAuthenticationToken();
|
|
TestingAuthenticationToken request = createAuthenticationToken();
|
|
request.setDetails(requestDetails);
|
|
request.setDetails(requestDetails);
|
|
@@ -150,35 +136,32 @@ public class ProviderManagerTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() throws Exception {
|
|
public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() throws Exception {
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
|
final Authentication authReq = mock(Authentication.class);
|
|
final Authentication authReq = mock(Authentication.class);
|
|
- mgr.setProviders(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", new Throwable())),
|
|
|
|
|
|
+ ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", new Throwable())),
|
|
createProviderWhichReturns(authReq)));
|
|
createProviderWhichReturns(authReq)));
|
|
assertSame(authReq, mgr.authenticate(mock(Authentication.class)));
|
|
assertSame(authReq, mgr.authenticate(mock(Authentication.class)));
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() throws Exception {
|
|
public void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() throws Exception {
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
|
|
|
|
|
- mgr.setProviders(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", "extra")),
|
|
|
|
|
|
+ ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("")),
|
|
createProviderWhichReturns(null)));
|
|
createProviderWhichReturns(null)));
|
|
try {
|
|
try {
|
|
mgr.authenticate(mock(Authentication.class));
|
|
mgr.authenticate(mock(Authentication.class));
|
|
fail("Expected BadCredentialsException");
|
|
fail("Expected BadCredentialsException");
|
|
} catch (BadCredentialsException expected) {
|
|
} catch (BadCredentialsException expected) {
|
|
- assertEquals("extra", expected.getExtraInformation());
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// SEC-546
|
|
// SEC-546
|
|
@Test
|
|
@Test
|
|
public void accountStatusExceptionPreventsCallsToSubsequentProviders() throws Exception {
|
|
public void accountStatusExceptionPreventsCallsToSubsequentProviders() throws Exception {
|
|
- ProviderManager authMgr = makeProviderManager();
|
|
|
|
- AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(new AccountStatusException(""){});
|
|
|
|
|
|
+ AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(new AccountStatusException("") {
|
|
|
|
+ });
|
|
AuthenticationProvider otherProvider = mock(AuthenticationProvider.class);
|
|
AuthenticationProvider otherProvider = mock(AuthenticationProvider.class);
|
|
|
|
|
|
- authMgr.setProviders(Arrays.asList(iThrowAccountStatusException, otherProvider));
|
|
|
|
|
|
+ ProviderManager authMgr = new ProviderManager(Arrays.asList(iThrowAccountStatusException, otherProvider));
|
|
|
|
|
|
try {
|
|
try {
|
|
authMgr.authenticate(mock(Authentication.class));
|
|
authMgr.authenticate(mock(Authentication.class));
|
|
@@ -188,22 +171,6 @@ public class ProviderManagerTests {
|
|
verifyZeroInteractions(otherProvider);
|
|
verifyZeroInteractions(otherProvider);
|
|
}
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
|
- public void extraInformationIsClearedIfFlagIsSet() throws Exception {
|
|
|
|
- ProviderManager authMgr = makeProviderManager();
|
|
|
|
- AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(new AccountStatusException("", "extra"){});
|
|
|
|
-
|
|
|
|
- authMgr.setProviders(Arrays.asList(iThrowAccountStatusException));
|
|
|
|
- authMgr.setClearExtraInformation(true);
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- authMgr.authenticate(mock(Authentication.class));
|
|
|
|
- fail("Expected AccountStatusException");
|
|
|
|
- } catch (AccountStatusException expected) {
|
|
|
|
- assertNull(expected.getExtraInformation());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
@Test
|
|
@Test
|
|
public void parentAuthenticationIsUsedIfProvidersDontAuthenticate() throws Exception {
|
|
public void parentAuthenticationIsUsedIfProvidersDontAuthenticate() throws Exception {
|
|
AuthenticationManager parent = mock(AuthenticationManager.class);
|
|
AuthenticationManager parent = mock(AuthenticationManager.class);
|
|
@@ -229,15 +196,15 @@ public class ProviderManagerTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void providerNotFoundFromParentIsIgnored() throws Exception {
|
|
public void providerNotFoundFromParentIsIgnored() throws Exception {
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
|
final Authentication authReq = mock(Authentication.class);
|
|
final Authentication authReq = mock(Authentication.class);
|
|
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
|
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
|
- mgr.setAuthenticationEventPublisher(publisher);
|
|
|
|
- // Set a provider that throws an exception - this is the exception we expect to be propagated
|
|
|
|
- mgr.setProviders(Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))));
|
|
|
|
AuthenticationManager parent = mock(AuthenticationManager.class);
|
|
AuthenticationManager parent = mock(AuthenticationManager.class);
|
|
when(parent.authenticate(authReq)).thenThrow(new ProviderNotFoundException(""));
|
|
when(parent.authenticate(authReq)).thenThrow(new ProviderNotFoundException(""));
|
|
- mgr.setParent(parent);
|
|
|
|
|
|
+
|
|
|
|
+ // Set a provider that throws an exception - this is the exception we expect to be propagated
|
|
|
|
+ ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))), parent);
|
|
|
|
+ mgr.setAuthenticationEventPublisher(publisher);
|
|
|
|
+
|
|
try {
|
|
try {
|
|
mgr.authenticate(authReq);
|
|
mgr.authenticate(authReq);
|
|
fail("Expected exception");
|
|
fail("Expected exception");
|
|
@@ -262,7 +229,6 @@ public class ProviderManagerTests {
|
|
fail("Expected exception");
|
|
fail("Expected exception");
|
|
} catch (BadCredentialsException e) {
|
|
} catch (BadCredentialsException e) {
|
|
assertSame(expected, e);
|
|
assertSame(expected, e);
|
|
- assertSame(authReq, e.getAuthentication());
|
|
|
|
}
|
|
}
|
|
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
|
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
|
}
|
|
}
|
|
@@ -282,7 +248,6 @@ public class ProviderManagerTests {
|
|
fail("Expected exception");
|
|
fail("Expected exception");
|
|
} catch (LockedException e) {
|
|
} catch (LockedException e) {
|
|
assertSame(expected, e);
|
|
assertSame(expected, e);
|
|
- assertSame(authReq, e.getAuthentication());
|
|
|
|
}
|
|
}
|
|
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
|
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
|
}
|
|
}
|