Add DaoAuthenticationProvider caching support.

This commit is contained in:
Ben Alex
2004-04-23 05:01:57 +00:00
parent babb908fea
commit e0d57de330
12 changed files with 577 additions and 17 deletions

View File

@@ -36,6 +36,7 @@
<!-- Authentication provider that queries our data access object -->
<bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="authenticationDao"><ref bean="inMemoryDaoImpl"/></property>
<property name="key"><value>my_password</value></property>
</bean>
<!-- The authentication manager that iterates through our only authentication provider -->

View File

@@ -31,6 +31,8 @@ import net.sf.acegisecurity.providers.encoding.ShaPasswordEncoder;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import java.util.Date;
/**
* Tests {@link DaoAuthenticationProvider}.
@@ -54,6 +56,7 @@ public class DaoAuthenticationProviderTests extends TestCase {
"KOala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
try {
@@ -69,6 +72,7 @@ public class DaoAuthenticationProviderTests extends TestCase {
"opal");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserPeter());
try {
@@ -84,6 +88,7 @@ public class DaoAuthenticationProviderTests extends TestCase {
"koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoSimulateBackendError());
try {
@@ -99,6 +104,7 @@ public class DaoAuthenticationProviderTests extends TestCase {
"INVALID_PASSWORD");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
try {
@@ -114,6 +120,7 @@ public class DaoAuthenticationProviderTests extends TestCase {
"koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
try {
@@ -129,6 +136,7 @@ public class DaoAuthenticationProviderTests extends TestCase {
"koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
try {
@@ -144,20 +152,55 @@ public class DaoAuthenticationProviderTests extends TestCase {
"koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
Authentication result = provider.authenticate(token);
if (!(result instanceof UsernamePasswordAuthenticationToken)) {
fail(
"Should have returned instance of UsernamePasswordAuthenticationToken");
if (!(result instanceof DaoAuthenticationToken)) {
fail("Should have returned instance of DaoAuthenticationToken");
}
UsernamePasswordAuthenticationToken castResult = (UsernamePasswordAuthenticationToken) result;
DaoAuthenticationToken castResult = (DaoAuthenticationToken) result;
assertEquals("marissa", castResult.getPrincipal());
assertEquals("koala", castResult.getCredentials());
assertEquals("ROLE_ONE", castResult.getAuthorities()[0].getAuthority());
assertEquals("ROLE_TWO", castResult.getAuthorities()[1].getAuthority());
assertEquals(provider.getKey().hashCode(), castResult.getKeyHash());
}
public void testAuthenticatesThenAcceptsCreatedTokenAutomatically() {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("marissa",
"koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
Authentication result = provider.authenticate(token);
if (!(result instanceof DaoAuthenticationToken)) {
fail("Should have returned instance of DaoAuthenticationToken");
}
DaoAuthenticationToken castResult = (DaoAuthenticationToken) result;
assertEquals("marissa", castResult.getPrincipal());
assertEquals(provider.getKey().hashCode(), castResult.getKeyHash());
assertTrue(castResult.getExpires().after(new Date()));
// Now try to re-authenticate
// Set provider to null, so we get a NullPointerException if it tries to re-authenticate
provider.setAuthenticationDao(null);
Authentication secondResult = provider.authenticate(result);
if (!(secondResult instanceof DaoAuthenticationToken)) {
fail("Should have returned instance of DaoAuthenticationToken");
}
// Should still have the same expiry time as original
assertEquals(castResult.getExpires(),
((DaoAuthenticationToken) secondResult).getExpires());
}
public void testAuthenticatesWhenASaltIsUsed() {
@@ -168,21 +211,77 @@ public class DaoAuthenticationProviderTests extends TestCase {
salt.setSystemWideSalt("SYSTEM_SALT_VALUE");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissaWithSalt());
provider.setSaltSource(salt);
Authentication result = provider.authenticate(token);
if (!(result instanceof UsernamePasswordAuthenticationToken)) {
if (!(result instanceof DaoAuthenticationToken)) {
fail(
"Should have returned instance of UsernamePasswordAuthenticationToken");
"Should have returned instance of DaoPasswordAuthenticationToken");
}
UsernamePasswordAuthenticationToken castResult = (UsernamePasswordAuthenticationToken) result;
DaoAuthenticationToken castResult = (DaoAuthenticationToken) result;
assertEquals("marissa", castResult.getPrincipal());
assertEquals("koala", castResult.getCredentials());
assertEquals("koala{SYSTEM_SALT_VALUE}", castResult.getCredentials());
assertEquals("ROLE_ONE", castResult.getAuthorities()[0].getAuthority());
assertEquals("ROLE_TWO", castResult.getAuthorities()[1].getAuthority());
assertEquals(provider.getKey().hashCode(), castResult.getKeyHash());
}
public void testDaoAuthenticationTokensThatHaveExpiredAreRefreshed()
throws Exception {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("marissa",
"koala");
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setRefreshTokenInterval(0); // never cache
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
Authentication result = provider.authenticate(token);
if (!(result instanceof DaoAuthenticationToken)) {
fail("Should have returned instance of DaoAuthenticationToken");
}
DaoAuthenticationToken castResult = (DaoAuthenticationToken) result;
assertEquals("marissa", castResult.getPrincipal());
assertEquals(provider.getKey().hashCode(), castResult.getKeyHash());
Thread.sleep(1000);
assertTrue(castResult.getExpires().before(new Date())); // already expired
// Now try to re-authenticate
Authentication secondResult = provider.authenticate(result);
if (!(secondResult instanceof DaoAuthenticationToken)) {
fail("Should have returned instance of DaoAuthenticationToken");
}
// Should still have a later expiry time than original
assertTrue(castResult.getExpires().before(((DaoAuthenticationToken) secondResult)
.getExpires()));
}
public void testDaoAuthenticationTokensWithWrongKeyAreRejected()
throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("x");
provider.setRefreshTokenInterval(0); // never cache
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
DaoAuthenticationToken token = new DaoAuthenticationToken("key",
new Date(), "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
try {
provider.authenticate(token);
fail("Should have thrown BadCredentialsException");
} catch (BadCredentialsException expected) {
assertTrue(true);
}
}
public void testGettersSetters() {
@@ -199,6 +298,19 @@ public class DaoAuthenticationProviderTests extends TestCase {
public void testStartupFailsIfNoAuthenticationDao()
throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setKey("xxx");
try {
provider.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testStartupFailsIfNoKeySet() throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
try {
provider.afterPropertiesSet();
@@ -211,6 +323,7 @@ public class DaoAuthenticationProviderTests extends TestCase {
public void testStartupSuccess() throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
AuthenticationDao dao = new MockAuthenticationDaoUserMarissa();
provider.setKey("x");
provider.setAuthenticationDao(dao);
assertEquals(dao, provider.getAuthenticationDao());
provider.afterPropertiesSet();

View File

@@ -0,0 +1,217 @@
/* Copyright 2004 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 net.sf.acegisecurity.providers.dao;
import junit.framework.TestCase;
import net.sf.acegisecurity.GrantedAuthority;
import net.sf.acegisecurity.GrantedAuthorityImpl;
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import java.util.Date;
/**
* Tests {@link DaoAuthenticationToken}.
*
* @author Ben Alex
* @version $Id$
*/
public class DaoAuthenticationTokenTests extends TestCase {
//~ Constructors ===========================================================
public DaoAuthenticationTokenTests() {
super();
}
public DaoAuthenticationTokenTests(String arg0) {
super(arg0);
}
//~ Methods ================================================================
public final void setUp() throws Exception {
super.setUp();
}
public static void main(String[] args) {
junit.textui.TestRunner.run(DaoAuthenticationTokenTests.class);
}
public void testConstructorRejectsNulls() {
try {
new DaoAuthenticationToken(null, new Date(), "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
try {
new DaoAuthenticationToken("key", null, "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
try {
new DaoAuthenticationToken("key", new Date(), null, "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
try {
new DaoAuthenticationToken("key", new Date(), "Test", null,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
try {
new DaoAuthenticationToken("key", new Date(), "Test", "Password",
null);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
try {
new DaoAuthenticationToken("key", new Date(), "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), null});
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testEqualsWhenEqual() {
Date date = new Date();
DaoAuthenticationToken token1 = new DaoAuthenticationToken("key", date,
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
DaoAuthenticationToken token2 = new DaoAuthenticationToken("key", date,
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
assertEquals(token1, token2);
}
public void testGetters() {
Date date = new Date();
DaoAuthenticationToken token = new DaoAuthenticationToken("key", date,
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
assertEquals("key".hashCode(), token.getKeyHash());
assertEquals("Test", token.getPrincipal());
assertEquals("Password", token.getCredentials());
assertEquals("ROLE_ONE", token.getAuthorities()[0].getAuthority());
assertEquals("ROLE_TWO", token.getAuthorities()[1].getAuthority());
assertEquals(date, token.getExpires());
}
public void testNoArgConstructor() {
try {
new DaoAuthenticationToken();
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testNotEqualsDueToAbstractParentEqualsCheck() {
Date date = new Date();
DaoAuthenticationToken token1 = new DaoAuthenticationToken("key", date,
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
DaoAuthenticationToken token2 = new DaoAuthenticationToken("key", date,
"DIFFERENT_PRINCIPAL", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
assertTrue(!token1.equals(token2));
}
public void testNotEqualsDueToDifferentAuthenticationClass() {
DaoAuthenticationToken token1 = new DaoAuthenticationToken("key",
new Date(), "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test",
"Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
token2.setAuthenticated(true);
assertTrue(!token1.equals(token2));
}
public void testNotEqualsDueToDifferentExpiresDate() {
DaoAuthenticationToken token1 = new DaoAuthenticationToken("key",
new Date(50000), "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
DaoAuthenticationToken token2 = new DaoAuthenticationToken("key",
new Date(60000), "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
assertTrue(!token1.equals(token2));
}
public void testNotEqualsDueToKey() {
Date date = new Date();
DaoAuthenticationToken token1 = new DaoAuthenticationToken("key", date,
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
DaoAuthenticationToken token2 = new DaoAuthenticationToken("DIFFERENT_KEY",
date, "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
assertTrue(!token1.equals(token2));
}
public void testSetAuthenticatedIgnored() {
DaoAuthenticationToken token = new DaoAuthenticationToken("key",
new Date(), "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
assertTrue(token.isAuthenticated());
token.setAuthenticated(false); // ignored
assertTrue(token.isAuthenticated());
}
}

View File

@@ -36,6 +36,7 @@
<!-- Authentication provider that queries our data access object -->
<bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="authenticationDao"><ref bean="inMemoryDaoImpl"/></property>
<property name="key"><value>my_password</value></property>
</bean>
<!-- The authentication manager that iterates through our only authentication provider -->