|
@@ -45,42 +45,62 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
*/
|
|
|
public class CasAuthenticationTokenMixinTests {
|
|
|
|
|
|
- private final String KEY = "casKey";
|
|
|
- private final String PASSWORD = "pass";
|
|
|
- Date startDate = new Date();
|
|
|
- Date endDate = new Date();
|
|
|
- String expectedJson = "{\"@class\": \"org.springframework.security.cas.authentication.CasAuthenticationToken\", \"keyHash\": " + KEY.hashCode() + "," +
|
|
|
- "\"principal\": {\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"username\", \"password\": %s, \"accountNonExpired\": true, \"enabled\": true," +
|
|
|
- "\"accountNonLocked\": true, \"credentialsNonExpired\": true, \"authorities\": [\"java.util.Collections$UnmodifiableSet\"," +
|
|
|
- "[{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"authority\": \"USER\"}]]}, \"credentials\": \"" + PASSWORD + "\", \"authorities\": [\"java.util.ArrayList\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"authority\": \"ROLE_USER\"}]]," +
|
|
|
- "\"userDetails\": {\"@class\": \"org.springframework.security.core.userdetails.User\",\"username\": \"user\", \"password\": \"" + PASSWORD + "\", \"enabled\": true, \"accountNonExpired\": true, \"accountNonLocked\": true, \"credentialsNonExpired\": true, \"authorities\": [\"java.util.Collections$UnmodifiableSet\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"authority\": \"ROLE_USER\"}]]}," +
|
|
|
- "\"authenticated\": true, \"details\": null," +
|
|
|
- "\"assertion\": {" +
|
|
|
- "\"@class\": \"org.jasig.cas.client.validation.AssertionImpl\", \"principal\": {\"@class\": \"org.jasig.cas.client.authentication.AttributePrincipalImpl\", \"name\": \"assertName\", \"attributes\": {\"@class\": \"java.util.Collections$EmptyMap\"}, \"proxyGrantingTicket\": null, \"proxyRetriever\": null}, " +
|
|
|
- "\"validFromDate\": [\"java.util.Date\", " + startDate.getTime() + "], \"validUntilDate\": [\"java.util.Date\", " + endDate.getTime() + "]," +
|
|
|
- "\"authenticationDate\": [\"java.util.Date\", " + startDate.getTime() + "], \"attributes\": {\"@class\": \"java.util.Collections$EmptyMap\"}" +
|
|
|
- "}}";
|
|
|
+ private static final String KEY = "casKey";
|
|
|
+ private static final String PASSWORD = "\"1234\"";
|
|
|
+ private static final Date START_DATE = new Date();
|
|
|
+ private static final Date END_DATE = new Date();
|
|
|
|
|
|
- private CasAuthenticationToken createCasAuthenticationToken() {
|
|
|
- User principal = new User("username", PASSWORD, Collections.singletonList(new SimpleGrantedAuthority("USER")));
|
|
|
- Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
- Assertion assertion = new AssertionImpl(new AttributePrincipalImpl("assertName"), startDate, endDate, startDate, Collections.<String, Object>emptyMap());
|
|
|
- return new CasAuthenticationToken(KEY, principal, principal.getPassword(), authorities,
|
|
|
- new User("user", PASSWORD, authorities), assertion);
|
|
|
- }
|
|
|
+ public static final String AUTHORITY_JSON = "{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"authority\": \"ROLE_USER\"}";
|
|
|
|
|
|
- ObjectMapper buildObjectMapper() {
|
|
|
- ClassLoader loader = getClass().getClassLoader();
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
- mapper.registerModules(SecurityJacksonModules.getModules(loader));
|
|
|
- return mapper;
|
|
|
- }
|
|
|
+ public static final String AUTHORITIES_SET_JSON = "[\"java.util.Collections$UnmodifiableSet\", [" + AUTHORITY_JSON + "]]";
|
|
|
+
|
|
|
+ public static final String AUTHORITIES_ARRAYLIST_JSON = "[\"java.util.ArrayList\", [" + AUTHORITY_JSON + "]]";
|
|
|
+
|
|
|
+ // @formatter:off
|
|
|
+ public static final String USER_JSON = "{"
|
|
|
+ + "\"@class\": \"org.springframework.security.core.userdetails.User\", "
|
|
|
+ + "\"username\": \"admin\","
|
|
|
+ + " \"password\": " + PASSWORD + ", "
|
|
|
+ + "\"accountNonExpired\": true, "
|
|
|
+ + "\"accountNonLocked\": true, "
|
|
|
+ + "\"credentialsNonExpired\": true, "
|
|
|
+ + "\"enabled\": true, "
|
|
|
+ + "\"authorities\": " + AUTHORITIES_SET_JSON
|
|
|
+ + "}";
|
|
|
+ // @formatter:on
|
|
|
+
|
|
|
+ private static final String CAS_TOKEN_JSON = "{"
|
|
|
+ + "\"@class\": \"org.springframework.security.cas.authentication.CasAuthenticationToken\", "
|
|
|
+ + "\"keyHash\": " + KEY.hashCode() + ","
|
|
|
+ + "\"principal\": " + USER_JSON + ", "
|
|
|
+ + "\"credentials\": " + PASSWORD + ", "
|
|
|
+ + "\"authorities\": " + AUTHORITIES_ARRAYLIST_JSON + ","
|
|
|
+ + "\"userDetails\": " + USER_JSON +","
|
|
|
+ + "\"authenticated\": true, "
|
|
|
+ + "\"details\": null,"
|
|
|
+ + "\"assertion\": {"
|
|
|
+ + "\"@class\": \"org.jasig.cas.client.validation.AssertionImpl\", "
|
|
|
+ + "\"principal\": {"
|
|
|
+ + "\"@class\": \"org.jasig.cas.client.authentication.AttributePrincipalImpl\", "
|
|
|
+ + "\"name\": \"assertName\", "
|
|
|
+ + "\"attributes\": {\"@class\": \"java.util.Collections$EmptyMap\"}, "
|
|
|
+ + "\"proxyGrantingTicket\": null, "
|
|
|
+ + "\"proxyRetriever\": null"
|
|
|
+ + "}, "
|
|
|
+ + "\"validFromDate\": [\"java.util.Date\", " + START_DATE.getTime() + "], "
|
|
|
+ + "\"validUntilDate\": [\"java.util.Date\", " + END_DATE.getTime() + "],"
|
|
|
+ + "\"authenticationDate\": [\"java.util.Date\", " + START_DATE.getTime() + "], "
|
|
|
+ + "\"attributes\": {\"@class\": \"java.util.Collections$EmptyMap\"}" +
|
|
|
+ "}"
|
|
|
+ + "}";
|
|
|
+
|
|
|
+ private static final String CAS_TOKEN_CLEARED_JSON = CAS_TOKEN_JSON.replaceFirst(PASSWORD, "null");
|
|
|
|
|
|
@Test
|
|
|
public void serializeCasAuthenticationTest() throws JsonProcessingException, JSONException {
|
|
|
CasAuthenticationToken token = createCasAuthenticationToken();
|
|
|
String actualJson = buildObjectMapper().writeValueAsString(token);
|
|
|
- JSONAssert.assertEquals(String.format(expectedJson, "\"" + PASSWORD + "\""), actualJson, true);
|
|
|
+ JSONAssert.assertEquals(CAS_TOKEN_JSON, actualJson, true);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -88,30 +108,45 @@ public class CasAuthenticationTokenMixinTests {
|
|
|
CasAuthenticationToken token = createCasAuthenticationToken();
|
|
|
token.eraseCredentials();
|
|
|
String actualJson = buildObjectMapper().writeValueAsString(token);
|
|
|
- JSONAssert.assertEquals(String.format(expectedJson, "null"), actualJson, true);
|
|
|
+ JSONAssert.assertEquals(CAS_TOKEN_CLEARED_JSON, actualJson, true);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void deserializeCasAuthenticationTestAfterEraseCredentialInvoked() throws Exception {
|
|
|
- CasAuthenticationToken token = buildObjectMapper().readValue(String.format(expectedJson, "null"), CasAuthenticationToken.class);
|
|
|
+ CasAuthenticationToken token = buildObjectMapper().readValue(CAS_TOKEN_CLEARED_JSON, CasAuthenticationToken.class);
|
|
|
assertThat(((UserDetails)token.getPrincipal()).getPassword()).isNull();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void deserializeCasAuthenticationTest() throws IOException, JSONException {
|
|
|
- CasAuthenticationToken token = buildObjectMapper().readValue(String.format(expectedJson, "\"" + PASSWORD + "\""), CasAuthenticationToken.class);
|
|
|
+ CasAuthenticationToken token = buildObjectMapper().readValue(CAS_TOKEN_JSON, CasAuthenticationToken.class);
|
|
|
assertThat(token).isNotNull();
|
|
|
assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
|
|
|
- assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("username");
|
|
|
- assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo(PASSWORD);
|
|
|
+ assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("admin");
|
|
|
+ assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo("1234");
|
|
|
assertThat(token.getUserDetails()).isNotNull().isInstanceOf(User.class);
|
|
|
assertThat(token.getAssertion()).isNotNull().isInstanceOf(AssertionImpl.class);
|
|
|
assertThat(token.getKeyHash()).isEqualTo(KEY.hashCode());
|
|
|
assertThat(token.getUserDetails().getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
- assertThat(token.getAssertion().getAuthenticationDate()).isEqualTo(startDate);
|
|
|
- assertThat(token.getAssertion().getValidFromDate()).isEqualTo(startDate);
|
|
|
- assertThat(token.getAssertion().getValidUntilDate()).isEqualTo(endDate);
|
|
|
+ assertThat(token.getAssertion().getAuthenticationDate()).isEqualTo(START_DATE);
|
|
|
+ assertThat(token.getAssertion().getValidFromDate()).isEqualTo(START_DATE);
|
|
|
+ assertThat(token.getAssertion().getValidUntilDate()).isEqualTo(END_DATE);
|
|
|
assertThat(token.getAssertion().getPrincipal().getName()).isEqualTo("assertName");
|
|
|
assertThat(token.getAssertion().getAttributes()).hasSize(0);
|
|
|
}
|
|
|
+
|
|
|
+ private CasAuthenticationToken createCasAuthenticationToken() {
|
|
|
+ User principal = new User("admin", "1234", Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")));
|
|
|
+ Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
+ Assertion assertion = new AssertionImpl(new AttributePrincipalImpl("assertName"), START_DATE, END_DATE, START_DATE, Collections.<String, Object>emptyMap());
|
|
|
+ return new CasAuthenticationToken(KEY, principal, principal.getPassword(), authorities,
|
|
|
+ new User("admin", "1234", authorities), assertion);
|
|
|
+ }
|
|
|
+
|
|
|
+ ObjectMapper buildObjectMapper() {
|
|
|
+ ClassLoader loader = getClass().getClassLoader();
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.registerModules(SecurityJacksonModules.getModules(loader));
|
|
|
+ return mapper;
|
|
|
+ }
|
|
|
}
|