Refactor CasAuthoritiesPopulator to use UserDetails rather than GrantedAuthority[].

This commit is contained in:
Ben Alex
2004-07-14 09:54:09 +00:00
parent 48b1cb7c85
commit e3be8f20bb
10 changed files with 113 additions and 49 deletions

View File

@@ -22,9 +22,11 @@ import net.sf.acegisecurity.AuthenticationException;
import net.sf.acegisecurity.BadCredentialsException;
import net.sf.acegisecurity.GrantedAuthority;
import net.sf.acegisecurity.GrantedAuthorityImpl;
import net.sf.acegisecurity.UserDetails;
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import net.sf.acegisecurity.providers.cas.ticketvalidator.AbstractTicketValidator;
import net.sf.acegisecurity.providers.dao.User;
import net.sf.acegisecurity.ui.cas.CasProcessingFilter;
import java.util.HashMap;
@@ -177,7 +179,7 @@ public class CasAuthenticationProviderTests extends TestCase {
CasAuthenticationToken token = new CasAuthenticationToken("WRONG_KEY",
"test", "credentials",
new GrantedAuthority[] {new GrantedAuthorityImpl("XX")},
new Vector(), "IOU-xxx");
makeUserDetails(), new Vector(), "IOU-xxx");
try {
Authentication result = cap.authenticate(token);
@@ -324,13 +326,20 @@ public class CasAuthenticationProviderTests extends TestCase {
assertTrue(cap.supports(CasAuthenticationToken.class));
}
private UserDetails makeUserDetails() {
return new User("user", "password", true,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
}
//~ Inner Classes ==========================================================
private class MockAuthoritiesPopulator implements CasAuthoritiesPopulator {
public GrantedAuthority[] getAuthorities(String casUserId)
public UserDetails getUserDetails(String casUserId)
throws AuthenticationException {
return new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A"), new GrantedAuthorityImpl(
"ROLE_B")};
return new User("user", "password", true,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A"), new GrantedAuthorityImpl(
"ROLE_B")});
}
}

View File

@@ -19,7 +19,9 @@ import junit.framework.TestCase;
import net.sf.acegisecurity.GrantedAuthority;
import net.sf.acegisecurity.GrantedAuthorityImpl;
import net.sf.acegisecurity.UserDetails;
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import net.sf.acegisecurity.providers.dao.User;
import java.util.List;
import java.util.Vector;
@@ -56,7 +58,7 @@ public class CasAuthenticationTokenTests extends TestCase {
try {
new CasAuthenticationToken(null, "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, new Vector(),
"ROLE_TWO")}, makeUserDetails(), new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@@ -66,7 +68,7 @@ public class CasAuthenticationTokenTests extends TestCase {
try {
new CasAuthenticationToken("key", null, "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, new Vector(),
"ROLE_TWO")}, makeUserDetails(), new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@@ -76,7 +78,7 @@ public class CasAuthenticationTokenTests extends TestCase {
try {
new CasAuthenticationToken("key", "Test", null,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, new Vector(),
"ROLE_TWO")}, makeUserDetails(), new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@@ -85,7 +87,7 @@ public class CasAuthenticationTokenTests extends TestCase {
try {
new CasAuthenticationToken("key", "Test", "Password", null,
new Vector(),
makeUserDetails(), new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@@ -95,7 +97,7 @@ public class CasAuthenticationTokenTests extends TestCase {
try {
new CasAuthenticationToken("key", "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, null,
"ROLE_TWO")}, makeUserDetails(), null,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@@ -105,7 +107,17 @@ public class CasAuthenticationTokenTests extends TestCase {
try {
new CasAuthenticationToken("key", "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, new Vector(), null);
"ROLE_TWO")}, null, new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
try {
new CasAuthenticationToken("key", "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, makeUserDetails(), new Vector(), null);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
@@ -114,7 +126,7 @@ public class CasAuthenticationTokenTests extends TestCase {
try {
new CasAuthenticationToken("key", "Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), null, new GrantedAuthorityImpl(
"ROLE_TWO")}, new Vector(),
"ROLE_TWO")}, makeUserDetails(), new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@@ -129,7 +141,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token1 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList1,
"ROLE_TWO")}, makeUserDetails(), proxyList1,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
List proxyList2 = new Vector();
@@ -138,7 +150,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token2 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList2,
"ROLE_TWO")}, makeUserDetails(), proxyList2,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
assertEquals(token1, token2);
@@ -152,7 +164,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList,
"ROLE_TWO")}, makeUserDetails(), proxyList,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
assertEquals("key".hashCode(), token.getKeyHash());
assertEquals("Test", token.getPrincipal());
@@ -180,7 +192,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token1 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList1,
"ROLE_TWO")}, makeUserDetails(), proxyList1,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
List proxyList2 = new Vector();
@@ -189,7 +201,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token2 = new CasAuthenticationToken("key",
"OTHER_VALUE", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList2,
"ROLE_TWO")}, makeUserDetails(), proxyList2,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
assertTrue(!token1.equals(token2));
@@ -202,7 +214,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token1 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList1,
"ROLE_TWO")}, makeUserDetails(), proxyList1,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test",
@@ -221,7 +233,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token1 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList1,
"ROLE_TWO")}, makeUserDetails(), proxyList1,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
List proxyList2 = new Vector();
@@ -230,7 +242,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token2 = new CasAuthenticationToken("DIFFERENT_KEY",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList2,
"ROLE_TWO")}, makeUserDetails(), proxyList2,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
assertTrue(!token1.equals(token2));
@@ -243,7 +255,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token1 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList1,
"ROLE_TWO")}, makeUserDetails(), proxyList1,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
List proxyList2 = new Vector();
@@ -252,7 +264,8 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token2 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList2, "PGTIOU-SOME_OTHER_VALUE");
"ROLE_TWO")}, makeUserDetails(), proxyList2,
"PGTIOU-SOME_OTHER_VALUE");
assertTrue(!token1.equals(token2));
}
@@ -264,7 +277,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token1 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList1,
"ROLE_TWO")}, makeUserDetails(), proxyList1,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
List proxyList2 = new Vector();
@@ -274,7 +287,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token2 = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList2,
"ROLE_TWO")}, makeUserDetails(), proxyList2,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
assertTrue(!token1.equals(token2));
@@ -284,7 +297,7 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, new Vector(),
"ROLE_TWO")}, makeUserDetails(), new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
assertTrue(token.isAuthenticated());
token.setAuthenticated(false); // ignored
@@ -295,11 +308,17 @@ public class CasAuthenticationTokenTests extends TestCase {
CasAuthenticationToken token = new CasAuthenticationToken("key",
"Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, new Vector(),
"ROLE_TWO")}, makeUserDetails(), new Vector(),
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
String result = token.toString();
assertTrue(result.lastIndexOf("Proxy List:") != -1);
assertTrue(result.lastIndexOf("Proxy-Granting Ticket IOU:") != -1);
assertTrue(result.lastIndexOf("Credentials (Service/Proxy Ticket):") != -1);
}
private UserDetails makeUserDetails() {
return new User("user", "password", true,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
}
}

View File

@@ -20,6 +20,7 @@ import junit.framework.TestCase;
import net.sf.acegisecurity.GrantedAuthority;
import net.sf.acegisecurity.GrantedAuthorityImpl;
import net.sf.acegisecurity.providers.cas.CasAuthenticationToken;
import net.sf.acegisecurity.providers.dao.User;
import java.util.List;
import java.util.Vector;
@@ -82,10 +83,14 @@ public class EhCacheBasedTicketCacheTests extends TestCase {
List proxyList = new Vector();
proxyList.add("https://localhost/newPortal/j_acegi_cas_security_check");
User user = new User("marissa", "password", true,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")});
return new CasAuthenticationToken("key", "marissa",
"ST-0-ER94xMJmn6pha35CQRoZ",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO")}, proxyList,
"ROLE_TWO")}, user, proxyList,
"PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
}
}

View File

@@ -74,7 +74,7 @@ public class DaoCasAuthoritiesPopulatorTests extends TestCase {
populator.afterPropertiesSet();
try {
populator.getAuthorities("scott");
populator.getUserDetails("scott");
fail("Should have thrown UsernameNotFoundException");
} catch (UsernameNotFoundException expected) {
assertTrue(true);
@@ -87,10 +87,12 @@ public class DaoCasAuthoritiesPopulatorTests extends TestCase {
populator.setAuthenticationDao(new MockAuthenticationDaoUserMarissa());
populator.afterPropertiesSet();
GrantedAuthority[] results = populator.getAuthorities("marissa");
assertEquals(2, results.length);
assertEquals(new GrantedAuthorityImpl("ROLE_ONE"), results[0]);
assertEquals(new GrantedAuthorityImpl("ROLE_TWO"), results[1]);
UserDetails results = populator.getUserDetails("marissa");
assertEquals(2, results.getAuthorities().length);
assertEquals(new GrantedAuthorityImpl("ROLE_ONE"),
results.getAuthorities()[0]);
assertEquals(new GrantedAuthorityImpl("ROLE_TWO"),
results.getAuthorities()[1]);
}
public void testGetGrantedAuthoritiesWhenDaoThrowsException()
@@ -100,7 +102,7 @@ public class DaoCasAuthoritiesPopulatorTests extends TestCase {
populator.afterPropertiesSet();
try {
populator.getAuthorities("THE_DAO_WILL_FAIL");
populator.getUserDetails("THE_DAO_WILL_FAIL");
fail("Should have thrown DataRetrievalFailureException");
} catch (DataRetrievalFailureException expected) {
assertTrue(true);