Search in sources :

Example 1 with CasAssertionAuthenticationToken

use of org.springframework.security.cas.authentication.CasAssertionAuthenticationToken in project spring-security by spring-projects.

the class GrantedAuthorityFromAssertionAttributesUserDetailsServiceTests method correctlyExtractsNamedAttributesFromAssertionAndConvertsThemToAuthorities.

@Test
public void correctlyExtractsNamedAttributesFromAssertionAndConvertsThemToAuthorities() {
    GrantedAuthorityFromAssertionAttributesUserDetailsService uds = new GrantedAuthorityFromAssertionAttributesUserDetailsService(new String[] { "a", "b", "c", "d" });
    uds.setConvertToUpperCase(false);
    Assertion assertion = mock(Assertion.class);
    AttributePrincipal principal = mock(AttributePrincipal.class);
    Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("a", Arrays.asList("role_a1", "role_a2"));
    attributes.put("b", "role_b");
    attributes.put("c", "role_c");
    attributes.put("d", null);
    attributes.put("someother", "unused");
    when(assertion.getPrincipal()).thenReturn(principal);
    when(principal.getAttributes()).thenReturn(attributes);
    when(principal.getName()).thenReturn("somebody");
    CasAssertionAuthenticationToken token = new CasAssertionAuthenticationToken(assertion, "ticket");
    UserDetails user = uds.loadUserDetails(token);
    Set<String> roles = AuthorityUtils.authorityListToSet(user.getAuthorities());
    assertThat(roles.size()).isEqualTo(4);
    assertThat(roles).contains("role_a1");
    assertThat(roles).contains("role_a2");
    assertThat(roles).contains("role_b");
    assertThat(roles).contains("role_c");
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) HashMap(java.util.HashMap) Assertion(org.jasig.cas.client.validation.Assertion) CasAssertionAuthenticationToken(org.springframework.security.cas.authentication.CasAssertionAuthenticationToken) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 AttributePrincipal (org.jasig.cas.client.authentication.AttributePrincipal)1 Assertion (org.jasig.cas.client.validation.Assertion)1 Test (org.junit.Test)1 CasAssertionAuthenticationToken (org.springframework.security.cas.authentication.CasAssertionAuthenticationToken)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1