SEC-1399: Removed AbstractAuthenticationManager.

MockAuthenticationManager was the only other subclass (apart from the main ProviderManager) and has been removed also.
This commit is contained in:
Luke Taylor
2010-02-20 21:35:39 +00:00
parent dacb8dd25a
commit ea7ccc718d
6 changed files with 60 additions and 165 deletions

View File

@@ -1,52 +0,0 @@
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security;
import org.springframework.security.authentication.AbstractAuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
/**
* Simply accepts as valid whatever is passed to it, if <code>grantAccess</code> is set to <code>true</code>.
*
* @author Ben Alex
* @author Wesley Hall
*/
public class MockAuthenticationManager extends AbstractAuthenticationManager {
//~ Instance fields ================================================================================================
private boolean grantAccess = true;
//~ Constructors ===================================================================================================
public MockAuthenticationManager(boolean grantAccess) {
this.grantAccess = grantAccess;
}
public MockAuthenticationManager() {
}
//~ Methods ========================================================================================================
public Authentication doAuthentication(Authentication authentication) throws AuthenticationException {
if (grantAccess) {
return authentication;
} else {
throw new BadCredentialsException("MockAuthenticationManager instructed to deny access");
}
}
}

View File

@@ -15,11 +15,15 @@
package org.springframework.security.authentication.rcp;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import org.springframework.security.MockAuthenticationManager;
import org.springframework.security.authentication.rcp.RemoteAuthenticationException;
import org.springframework.security.authentication.rcp.RemoteAuthenticationManagerImpl;
import org.junit.Test;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
/**
@@ -27,27 +31,20 @@ import org.springframework.security.authentication.rcp.RemoteAuthenticationManag
*
* @author Ben Alex
*/
public class RemoteAuthenticationManagerImplTests extends TestCase {
public class RemoteAuthenticationManagerImplTests {
//~ Methods ========================================================================================================
@Test(expected=RemoteAuthenticationException.class)
public void testFailedAuthenticationReturnsRemoteAuthenticationException() {
RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl();
manager.setAuthenticationManager(new MockAuthenticationManager(false));
AuthenticationManager am = mock(AuthenticationManager.class);
when(am.authenticate(any(Authentication.class))).thenThrow(new BadCredentialsException(""));
manager.setAuthenticationManager(am);
try {
manager.attemptAuthentication("rod", "password");
fail("Should have thrown RemoteAuthenticationException");
} catch (RemoteAuthenticationException expected) {
assertTrue(true);
}
}
public void testGettersSetters() {
RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl();
manager.setAuthenticationManager(new MockAuthenticationManager(true));
assertNotNull(manager.getAuthenticationManager());
manager.attemptAuthentication("rod", "password");
}
@Test
public void testStartupChecksAuthenticationManagerSet() throws Exception {
RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl();
@@ -55,17 +52,19 @@ public class RemoteAuthenticationManagerImplTests extends TestCase {
manager.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
manager.setAuthenticationManager(new MockAuthenticationManager(true));
manager.setAuthenticationManager(mock(AuthenticationManager.class));
manager.afterPropertiesSet();
assertTrue(true);
}
@Test
public void testSuccessfulAuthentication() {
RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl();
manager.setAuthenticationManager(new MockAuthenticationManager(true));
AuthenticationManager am = mock(AuthenticationManager.class);
when(am.authenticate(any(Authentication.class))).thenReturn(new TestingAuthenticationToken("u","p","A"));
manager.setAuthenticationManager(am);
manager.attemptAuthentication("rod", "password");
}

View File

@@ -1,6 +1,7 @@
package org.springframework.security.provisioning;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.HashMap;
@@ -13,10 +14,10 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.MockAuthenticationManager;
import org.springframework.security.PopulatedDatabase;
import org.springframework.security.TestDataSource;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -155,7 +156,10 @@ public class JdbcUserDetailsManagerTests {
public void changePasswordSucceedsWithIfReAuthenticationSucceeds() {
insertJoe();
Authentication currentAuth = authenticateJoe();
manager.setAuthenticationManager(new MockAuthenticationManager(true));
AuthenticationManager am = mock(AuthenticationManager.class);
when(am.authenticate(currentAuth)).thenReturn(currentAuth);
manager.setAuthenticationManager(am);
manager.changePassword("password", "newPassword");
UserDetails newJoe = manager.loadUserByUsername("joe");
@@ -172,7 +176,10 @@ public class JdbcUserDetailsManagerTests {
public void changePasswordFailsIfReAuthenticationFails() {
insertJoe();
authenticateJoe();
manager.setAuthenticationManager(new MockAuthenticationManager(false));
AuthenticationManager am = mock(AuthenticationManager.class);
when(am.authenticate(any(Authentication.class))).thenThrow(new BadCredentialsException(""));
manager.setAuthenticationManager(am);
try {
manager.changePassword("password", "newPassword");