|
45 | 45 | */
|
46 | 46 | public class CasAuthenticationTokenMixinTests {
|
47 | 47 |
|
48 |
| - private final String KEY = "casKey"; |
49 |
| - private final String PASSWORD = "pass"; |
50 |
| - Date startDate = new Date(); |
51 |
| - Date endDate = new Date(); |
52 |
| - String expectedJson = "{\"@class\": \"org.springframework.security.cas.authentication.CasAuthenticationToken\", \"keyHash\": " + KEY.hashCode() + "," + |
53 |
| - "\"principal\": {\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"username\", \"password\": %s, \"accountNonExpired\": true, \"enabled\": true," + |
54 |
| - "\"accountNonLocked\": true, \"credentialsNonExpired\": true, \"authorities\": [\"java.util.Collections$UnmodifiableSet\"," + |
55 |
| - "[{\"@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\"}]]," + |
56 |
| - "\"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\"}]]}," + |
57 |
| - "\"authenticated\": true, \"details\": null," + |
58 |
| - "\"assertion\": {" + |
59 |
| - "\"@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}, " + |
60 |
| - "\"validFromDate\": [\"java.util.Date\", " + startDate.getTime() + "], \"validUntilDate\": [\"java.util.Date\", " + endDate.getTime() + "]," + |
61 |
| - "\"authenticationDate\": [\"java.util.Date\", " + startDate.getTime() + "], \"attributes\": {\"@class\": \"java.util.Collections$EmptyMap\"}" + |
62 |
| - "}}"; |
| 48 | + private static final String KEY = "casKey"; |
| 49 | + private static final String PASSWORD = "\"1234\""; |
| 50 | + private static final Date START_DATE = new Date(); |
| 51 | + private static final Date END_DATE = new Date(); |
63 | 52 |
|
64 |
| - private CasAuthenticationToken createCasAuthenticationToken() { |
65 |
| - User principal = new User("username", PASSWORD, Collections.singletonList(new SimpleGrantedAuthority("USER"))); |
66 |
| - Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); |
67 |
| - Assertion assertion = new AssertionImpl(new AttributePrincipalImpl("assertName"), startDate, endDate, startDate, Collections.<String, Object>emptyMap()); |
68 |
| - return new CasAuthenticationToken(KEY, principal, principal.getPassword(), authorities, |
69 |
| - new User("user", PASSWORD, authorities), assertion); |
70 |
| - } |
| 53 | + public static final String AUTHORITY_JSON = "{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"authority\": \"ROLE_USER\"}"; |
71 | 54 |
|
72 |
| - ObjectMapper buildObjectMapper() { |
73 |
| - ClassLoader loader = getClass().getClassLoader(); |
74 |
| - ObjectMapper mapper = new ObjectMapper(); |
75 |
| - mapper.registerModules(SecurityJacksonModules.getModules(loader)); |
76 |
| - return mapper; |
77 |
| - } |
| 55 | + public static final String AUTHORITIES_SET_JSON = "[\"java.util.Collections$UnmodifiableSet\", [" + AUTHORITY_JSON + "]]"; |
| 56 | + |
| 57 | + public static final String AUTHORITIES_ARRAYLIST_JSON = "[\"java.util.ArrayList\", [" + AUTHORITY_JSON + "]]"; |
| 58 | + |
| 59 | + // @formatter:off |
| 60 | + public static final String USER_JSON = "{" |
| 61 | + + "\"@class\": \"org.springframework.security.core.userdetails.User\", " |
| 62 | + + "\"username\": \"admin\"," |
| 63 | + + " \"password\": " + PASSWORD + ", " |
| 64 | + + "\"accountNonExpired\": true, " |
| 65 | + + "\"accountNonLocked\": true, " |
| 66 | + + "\"credentialsNonExpired\": true, " |
| 67 | + + "\"enabled\": true, " |
| 68 | + + "\"authorities\": " + AUTHORITIES_SET_JSON |
| 69 | + + "}"; |
| 70 | + // @formatter:on |
| 71 | + |
| 72 | + private static final String CAS_TOKEN_JSON = "{" |
| 73 | + + "\"@class\": \"org.springframework.security.cas.authentication.CasAuthenticationToken\", " |
| 74 | + + "\"keyHash\": " + KEY.hashCode() + "," |
| 75 | + + "\"principal\": " + USER_JSON + ", " |
| 76 | + + "\"credentials\": " + PASSWORD + ", " |
| 77 | + + "\"authorities\": " + AUTHORITIES_ARRAYLIST_JSON + "," |
| 78 | + + "\"userDetails\": " + USER_JSON +"," |
| 79 | + + "\"authenticated\": true, " |
| 80 | + + "\"details\": null," |
| 81 | + + "\"assertion\": {" |
| 82 | + + "\"@class\": \"org.jasig.cas.client.validation.AssertionImpl\", " |
| 83 | + + "\"principal\": {" |
| 84 | + + "\"@class\": \"org.jasig.cas.client.authentication.AttributePrincipalImpl\", " |
| 85 | + + "\"name\": \"assertName\", " |
| 86 | + + "\"attributes\": {\"@class\": \"java.util.Collections$EmptyMap\"}, " |
| 87 | + + "\"proxyGrantingTicket\": null, " |
| 88 | + + "\"proxyRetriever\": null" |
| 89 | + + "}, " |
| 90 | + + "\"validFromDate\": [\"java.util.Date\", " + START_DATE.getTime() + "], " |
| 91 | + + "\"validUntilDate\": [\"java.util.Date\", " + END_DATE.getTime() + "]," |
| 92 | + + "\"authenticationDate\": [\"java.util.Date\", " + START_DATE.getTime() + "], " |
| 93 | + + "\"attributes\": {\"@class\": \"java.util.Collections$EmptyMap\"}" + |
| 94 | + "}" |
| 95 | + + "}"; |
| 96 | + |
| 97 | + private static final String CAS_TOKEN_CLEARED_JSON = CAS_TOKEN_JSON.replaceFirst(PASSWORD, "null"); |
78 | 98 |
|
79 | 99 | @Test
|
80 | 100 | public void serializeCasAuthenticationTest() throws JsonProcessingException, JSONException {
|
81 | 101 | CasAuthenticationToken token = createCasAuthenticationToken();
|
82 | 102 | String actualJson = buildObjectMapper().writeValueAsString(token);
|
83 |
| - JSONAssert.assertEquals(String.format(expectedJson, "\"" + PASSWORD + "\""), actualJson, true); |
| 103 | + JSONAssert.assertEquals(CAS_TOKEN_JSON, actualJson, true); |
84 | 104 | }
|
85 | 105 |
|
86 | 106 | @Test
|
87 | 107 | public void serializeCasAuthenticationTestAfterEraseCredentialInvoked() throws JsonProcessingException, JSONException {
|
88 | 108 | CasAuthenticationToken token = createCasAuthenticationToken();
|
89 | 109 | token.eraseCredentials();
|
90 | 110 | String actualJson = buildObjectMapper().writeValueAsString(token);
|
91 |
| - JSONAssert.assertEquals(String.format(expectedJson, "null"), actualJson, true); |
| 111 | + JSONAssert.assertEquals(CAS_TOKEN_CLEARED_JSON, actualJson, true); |
92 | 112 | }
|
93 | 113 |
|
94 | 114 | @Test
|
95 | 115 | public void deserializeCasAuthenticationTestAfterEraseCredentialInvoked() throws Exception {
|
96 |
| - CasAuthenticationToken token = buildObjectMapper().readValue(String.format(expectedJson, "null"), CasAuthenticationToken.class); |
| 116 | + CasAuthenticationToken token = buildObjectMapper().readValue(CAS_TOKEN_CLEARED_JSON, CasAuthenticationToken.class); |
97 | 117 | assertThat(((UserDetails)token.getPrincipal()).getPassword()).isNull();
|
98 | 118 | }
|
99 | 119 |
|
100 | 120 | @Test
|
101 | 121 | public void deserializeCasAuthenticationTest() throws IOException, JSONException {
|
102 |
| - CasAuthenticationToken token = buildObjectMapper().readValue(String.format(expectedJson, "\"" + PASSWORD + "\""), CasAuthenticationToken.class); |
| 122 | + CasAuthenticationToken token = buildObjectMapper().readValue(CAS_TOKEN_JSON, CasAuthenticationToken.class); |
103 | 123 | assertThat(token).isNotNull();
|
104 | 124 | assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
|
105 |
| - assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("username"); |
106 |
| - assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo(PASSWORD); |
| 125 | + assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("admin"); |
| 126 | + assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo("1234"); |
107 | 127 | assertThat(token.getUserDetails()).isNotNull().isInstanceOf(User.class);
|
108 | 128 | assertThat(token.getAssertion()).isNotNull().isInstanceOf(AssertionImpl.class);
|
109 | 129 | assertThat(token.getKeyHash()).isEqualTo(KEY.hashCode());
|
110 | 130 | assertThat(token.getUserDetails().getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
|
111 |
| - assertThat(token.getAssertion().getAuthenticationDate()).isEqualTo(startDate); |
112 |
| - assertThat(token.getAssertion().getValidFromDate()).isEqualTo(startDate); |
113 |
| - assertThat(token.getAssertion().getValidUntilDate()).isEqualTo(endDate); |
| 131 | + assertThat(token.getAssertion().getAuthenticationDate()).isEqualTo(START_DATE); |
| 132 | + assertThat(token.getAssertion().getValidFromDate()).isEqualTo(START_DATE); |
| 133 | + assertThat(token.getAssertion().getValidUntilDate()).isEqualTo(END_DATE); |
114 | 134 | assertThat(token.getAssertion().getPrincipal().getName()).isEqualTo("assertName");
|
115 | 135 | assertThat(token.getAssertion().getAttributes()).hasSize(0);
|
116 | 136 | }
|
| 137 | + |
| 138 | + private CasAuthenticationToken createCasAuthenticationToken() { |
| 139 | + User principal = new User("admin", "1234", Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))); |
| 140 | + Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); |
| 141 | + Assertion assertion = new AssertionImpl(new AttributePrincipalImpl("assertName"), START_DATE, END_DATE, START_DATE, Collections.<String, Object>emptyMap()); |
| 142 | + return new CasAuthenticationToken(KEY, principal, principal.getPassword(), authorities, |
| 143 | + new User("admin", "1234", authorities), assertion); |
| 144 | + } |
| 145 | + |
| 146 | + ObjectMapper buildObjectMapper() { |
| 147 | + ClassLoader loader = getClass().getClassLoader(); |
| 148 | + ObjectMapper mapper = new ObjectMapper(); |
| 149 | + mapper.registerModules(SecurityJacksonModules.getModules(loader)); |
| 150 | + return mapper; |
| 151 | + } |
117 | 152 | }
|
0 commit comments