|
@@ -21,15 +21,17 @@ import org.springframework.security.AuthenticationServiceException;
|
|
|
import org.springframework.security.GrantedAuthority;
|
|
|
import org.springframework.security.GrantedAuthorityImpl;
|
|
|
import org.springframework.security.MockApplicationEventPublisher;
|
|
|
+import org.springframework.security.AccountStatusException;
|
|
|
import org.springframework.security.concurrent.ConcurrentSessionControllerImpl;
|
|
|
import org.springframework.security.concurrent.NullConcurrentSessionController;
|
|
|
-
|
|
|
-import junit.framework.TestCase;
|
|
|
+import org.springframework.security.concurrent.ConcurrentLoginException;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Vector;
|
|
|
|
|
|
+import org.junit.Test;
|
|
|
+import static org.junit.Assert.*;
|
|
|
|
|
|
/**
|
|
|
* Tests {@link ProviderManager}.
|
|
@@ -37,60 +39,22 @@ import java.util.Vector;
|
|
|
* @author Ben Alex
|
|
|
* @version $Id$
|
|
|
*/
|
|
|
-public class ProviderManagerTests extends TestCase {
|
|
|
- //~ Constructors ===================================================================================================
|
|
|
-
|
|
|
- public ProviderManagerTests() {
|
|
|
- }
|
|
|
-
|
|
|
- public ProviderManagerTests(String arg0) {
|
|
|
- super(arg0);
|
|
|
- }
|
|
|
+public class ProviderManagerTests {
|
|
|
|
|
|
//~ Methods ========================================================================================================
|
|
|
|
|
|
- private ProviderManager makeProviderManager() throws Exception {
|
|
|
- MockProvider provider1 = new MockProvider();
|
|
|
- List providers = new Vector();
|
|
|
- providers.add(provider1);
|
|
|
-
|
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
- mgr.setProviders(providers);
|
|
|
-
|
|
|
- mgr.afterPropertiesSet();
|
|
|
-
|
|
|
- return mgr;
|
|
|
- }
|
|
|
-
|
|
|
- private ProviderManager makeProviderManagerWithMockProviderWhichReturnsNullInList() {
|
|
|
- MockProviderWhichReturnsNull provider1 = new MockProviderWhichReturnsNull();
|
|
|
- MockProvider provider2 = new MockProvider();
|
|
|
- List providers = new Vector();
|
|
|
- providers.add(provider1);
|
|
|
- providers.add(provider2);
|
|
|
-
|
|
|
- ProviderManager mgr = new ProviderManager();
|
|
|
- mgr.setProviders(providers);
|
|
|
-
|
|
|
- return mgr;
|
|
|
- }
|
|
|
-
|
|
|
- public void testAuthenticationFails() throws Exception {
|
|
|
+ @Test(expected=ProviderNotFoundException.class)
|
|
|
+ public void authenticationFailsWithUnsupportedToken() throws Exception {
|
|
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
|
|
|
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
|
|
|
|
|
|
ProviderManager mgr = makeProviderManager();
|
|
|
mgr.setApplicationEventPublisher(new MockApplicationEventPublisher(true));
|
|
|
-
|
|
|
- try {
|
|
|
- mgr.authenticate(token);
|
|
|
- fail("Should have thrown ProviderNotFoundException");
|
|
|
- } catch (ProviderNotFoundException expected) {
|
|
|
- assertTrue(true);
|
|
|
- }
|
|
|
+ mgr.authenticate(token);
|
|
|
}
|
|
|
|
|
|
- public void testAuthenticationSuccess() throws Exception {
|
|
|
+ @Test
|
|
|
+ public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() throws Exception {
|
|
|
TestingAuthenticationToken token = new TestingAuthenticationToken("Test", "Password",
|
|
|
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
|
|
|
|
|
@@ -110,7 +74,8 @@ public class ProviderManagerTests extends TestCase {
|
|
|
assertEquals("ROLE_TWO", castResult.getAuthorities()[1].getAuthority());
|
|
|
}
|
|
|
|
|
|
- public void testAuthenticationSuccessWhenFirstProviderReturnsNullButSecondAuthenticates() {
|
|
|
+ @Test
|
|
|
+ public void authenticationSuccessWhenFirstProviderReturnsNullButSecondAuthenticates() {
|
|
|
TestingAuthenticationToken token = new TestingAuthenticationToken("Test", "Password",
|
|
|
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
|
|
|
|
|
@@ -130,7 +95,8 @@ public class ProviderManagerTests extends TestCase {
|
|
|
assertEquals("ROLE_TWO", castResult.getAuthorities()[1].getAuthority());
|
|
|
}
|
|
|
|
|
|
- public void testConcurrentSessionControllerConfiguration() throws Exception {
|
|
|
+ @Test
|
|
|
+ public void concurrentSessionControllerConfiguration() throws Exception {
|
|
|
ProviderManager target = new ProviderManager();
|
|
|
|
|
|
//The NullConcurrentSessionController should be the default
|
|
@@ -142,52 +108,34 @@ public class ProviderManagerTests extends TestCase {
|
|
|
assertEquals(impl, target.getSessionController());
|
|
|
}
|
|
|
|
|
|
- public void testStartupFailsIfProviderListDoesNotContainingProviders() throws Exception {
|
|
|
+ @Test(expected=IllegalArgumentException.class)
|
|
|
+ public void startupFailsIfProviderListDoesNotContainProviders() throws Exception {
|
|
|
List providers = new Vector();
|
|
|
providers.add("THIS_IS_NOT_A_PROVIDER");
|
|
|
|
|
|
ProviderManager mgr = new ProviderManager();
|
|
|
|
|
|
- try {
|
|
|
- mgr.setProviders(providers);
|
|
|
- fail("Should have thrown IllegalArgumentException");
|
|
|
- } catch (IllegalArgumentException expected) {
|
|
|
- assertTrue(true);
|
|
|
- }
|
|
|
+ mgr.setProviders(providers);
|
|
|
}
|
|
|
|
|
|
- public void testStartupFailsIfProviderListNotSet() throws Exception {
|
|
|
+ @Test(expected=IllegalArgumentException.class)
|
|
|
+ public void startupFailsIfProviderListNotSet() throws Exception {
|
|
|
ProviderManager mgr = new ProviderManager();
|
|
|
|
|
|
- try {
|
|
|
- mgr.afterPropertiesSet();
|
|
|
- fail("Should have thrown IllegalArgumentException");
|
|
|
- } catch (IllegalArgumentException expected) {
|
|
|
- assertTrue(true);
|
|
|
- }
|
|
|
+ mgr.afterPropertiesSet();
|
|
|
}
|
|
|
|
|
|
+ @Test(expected=IllegalArgumentException.class)
|
|
|
public void testStartupFailsIfProviderListNull() throws Exception {
|
|
|
ProviderManager mgr = new ProviderManager();
|
|
|
|
|
|
- try {
|
|
|
- mgr.setProviders(null);
|
|
|
- fail("Should have thrown IllegalArgumentException");
|
|
|
- } catch (IllegalArgumentException expected) {
|
|
|
- assertTrue(true);
|
|
|
- }
|
|
|
+ mgr.setProviders(null);
|
|
|
}
|
|
|
|
|
|
- public void testSuccessfulStartup() throws Exception {
|
|
|
- ProviderManager mgr = makeProviderManager();
|
|
|
- mgr.afterPropertiesSet();
|
|
|
- assertTrue(true);
|
|
|
- assertEquals(1, mgr.getProviders().size());
|
|
|
- }
|
|
|
-
|
|
|
- public void testDetailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() throws Exception {
|
|
|
- Object requestDetails = new String("(Request Details)");
|
|
|
- final Object resultDetails = new String("(Result Details)");
|
|
|
+ @Test
|
|
|
+ public void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() throws Exception {
|
|
|
+ Object requestDetails = "(Request Details)";
|
|
|
+ final Object resultDetails = "(Result Details)";
|
|
|
ProviderManager authMgr = makeProviderManager();
|
|
|
|
|
|
AuthenticationProvider provider = new AuthenticationProvider() {
|
|
@@ -201,7 +149,7 @@ public class ProviderManagerTests extends TestCase {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- authMgr.setProviders(Arrays.asList(new AuthenticationProvider[] {provider}));
|
|
|
+ authMgr.setProviders(Arrays.asList(provider));
|
|
|
|
|
|
TestingAuthenticationToken request = createAuthenticationToken();
|
|
|
request.setDetails(requestDetails);
|
|
@@ -210,7 +158,8 @@ public class ProviderManagerTests extends TestCase {
|
|
|
assertEquals(resultDetails, result.getDetails());
|
|
|
}
|
|
|
|
|
|
- public void testDetailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() throws Exception {
|
|
|
+ @Test
|
|
|
+ public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() throws Exception {
|
|
|
Object details = new Object();
|
|
|
ProviderManager authMgr = makeProviderManager();
|
|
|
|
|
@@ -221,10 +170,57 @@ public class ProviderManagerTests extends TestCase {
|
|
|
assertEquals(details, result.getDetails());
|
|
|
}
|
|
|
|
|
|
+ // SEC-546
|
|
|
+ @Test(expected=AccountStatusException.class)
|
|
|
+ public void accountStatusExceptionPreventsCallsToSubsequentProviders() throws Exception {
|
|
|
+ ProviderManager authMgr = makeProviderManager();
|
|
|
+
|
|
|
+ authMgr.setProviders(Arrays.asList(new MockProviderWhichThrowsAccountStatusException(),
|
|
|
+ new MockProviderWhichThrowsConcurrentLoginException()) );
|
|
|
+
|
|
|
+ authMgr.authenticate(createAuthenticationToken());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test(expected=ConcurrentLoginException.class)
|
|
|
+ public void concurrentLoginExceptionPreventsCallsToSubsequentProviders() throws Exception {
|
|
|
+ ProviderManager authMgr = makeProviderManager();
|
|
|
+
|
|
|
+ authMgr.setProviders(Arrays.asList(new MockProviderWhichThrowsConcurrentLoginException(),
|
|
|
+ new MockProviderWhichThrowsAccountStatusException()) );
|
|
|
+
|
|
|
+ authMgr.authenticate(createAuthenticationToken());
|
|
|
+ }
|
|
|
+
|
|
|
private TestingAuthenticationToken createAuthenticationToken() {
|
|
|
return new TestingAuthenticationToken("name", "password", new GrantedAuthorityImpl[0]);
|
|
|
}
|
|
|
|
|
|
+ private ProviderManager makeProviderManager() throws Exception {
|
|
|
+ MockProvider provider1 = new MockProvider();
|
|
|
+ List providers = new Vector();
|
|
|
+ providers.add(provider1);
|
|
|
+
|
|
|
+ ProviderManager mgr = new ProviderManager();
|
|
|
+ mgr.setProviders(providers);
|
|
|
+
|
|
|
+ mgr.afterPropertiesSet();
|
|
|
+
|
|
|
+ return mgr;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProviderManager makeProviderManagerWithMockProviderWhichReturnsNullInList() {
|
|
|
+ MockProviderWhichReturnsNull provider1 = new MockProviderWhichReturnsNull();
|
|
|
+ MockProvider provider2 = new MockProvider();
|
|
|
+ List providers = new Vector();
|
|
|
+ providers.add(provider1);
|
|
|
+ providers.add(provider2);
|
|
|
+
|
|
|
+ ProviderManager mgr = new ProviderManager();
|
|
|
+ mgr.setProviders(providers);
|
|
|
+
|
|
|
+ return mgr;
|
|
|
+ }
|
|
|
+
|
|
|
//~ Inner Classes ==================================================================================================
|
|
|
|
|
|
private class MockProvider implements AuthenticationProvider {
|
|
@@ -262,4 +258,25 @@ public class ProviderManagerTests extends TestCase {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private class MockProviderWhichThrowsAccountStatusException implements AuthenticationProvider {
|
|
|
+ public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
|
|
+ throw new AccountStatusException("xxx") {};
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean supports(Class authentication) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class MockProviderWhichThrowsConcurrentLoginException implements AuthenticationProvider {
|
|
|
+ public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
|
|
+ throw new ConcurrentLoginException("xxx") {};
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean supports(Class authentication) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|