Search in sources :

Example 1 with ChainingPrincipalResolver

use of org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver in project cas by apereo.

the class ChainingPrincipalResolverTests method examineSupports.

@Test
public void examineSupports() throws Exception {
    final Credential credential = mock(Credential.class);
    when(credential.getId()).thenReturn("a");
    final PrincipalResolver resolver1 = mock(PrincipalResolver.class);
    when(resolver1.supports(eq(credential))).thenReturn(true);
    final PrincipalResolver resolver2 = mock(PrincipalResolver.class);
    when(resolver2.supports(eq(credential))).thenReturn(false);
    final ChainingPrincipalResolver resolver = new ChainingPrincipalResolver();
    resolver.setChain(Arrays.asList(resolver1, resolver2));
    assertTrue(resolver.supports(credential));
}
Also used : Credential(org.apereo.cas.authentication.Credential) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) Test(org.junit.Test)

Example 2 with ChainingPrincipalResolver

use of org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver in project cas by apereo.

the class ChainingPrincipalResolverTests method examineResolve.

@Test
public void examineResolve() throws Exception {
    final Credential credential = mock(Credential.class);
    when(credential.getId()).thenReturn("input");
    final PrincipalResolver resolver1 = mock(PrincipalResolver.class);
    when(resolver1.supports(eq(credential))).thenReturn(true);
    when(resolver1.resolve(eq(credential), any(Principal.class), any(AuthenticationHandler.class))).thenReturn(principalFactory.createPrincipal("output"));
    final PrincipalResolver resolver2 = mock(PrincipalResolver.class);
    when(resolver2.supports(any(Credential.class))).thenReturn(true);
    when(resolver2.resolve(any(Credential.class), any(Principal.class), any(AuthenticationHandler.class))).thenReturn(principalFactory.createPrincipal("output", Collections.<String, Object>singletonMap("mail", "final@example.com")));
    final ChainingPrincipalResolver resolver = new ChainingPrincipalResolver();
    resolver.setChain(Arrays.asList(resolver1, resolver2));
    final Principal principal = resolver.resolve(credential, any(Principal.class), new SimpleTestUsernamePasswordAuthenticationHandler());
    assertEquals("output", principal.getId());
    assertEquals("final@example.com", principal.getAttributes().get("mail"));
}
Also used : Credential(org.apereo.cas.authentication.Credential) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) Test(org.junit.Test)

Example 3 with ChainingPrincipalResolver

use of org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver in project cas by apereo.

the class PersonDirectoryPrincipalResolverTests method verifyChainingResolverOverwrite.

@Test
public void verifyChainingResolverOverwrite() {
    final PersonDirectoryPrincipalResolver resolver = new PersonDirectoryPrincipalResolver();
    resolver.setAttributeRepository(CoreAuthenticationTestUtils.getAttributeRepository());
    final ChainingPrincipalResolver chain = new ChainingPrincipalResolver();
    chain.setChain(Arrays.asList(resolver, new EchoingPrincipalResolver()));
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put("cn", "changedCN");
    attributes.put(ATTR_1, "value1");
    final Principal p = chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), CoreAuthenticationTestUtils.getPrincipal(CoreAuthenticationTestUtils.CONST_USERNAME, attributes), new SimpleTestUsernamePasswordAuthenticationHandler());
    assertEquals(p.getAttributes().size(), CoreAuthenticationTestUtils.getAttributeRepository().getPossibleUserAttributeNames().size() + 1);
    assertTrue(p.getAttributes().containsKey(ATTR_1));
    assertTrue(p.getAttributes().containsKey("cn"));
    assertTrue(CollectionUtils.toCollection(p.getAttributes().get("cn")).contains("changedCN"));
}
Also used : PersonDirectoryPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.PersonDirectoryPrincipalResolver) HashMap(java.util.HashMap) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) EchoingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.EchoingPrincipalResolver) Test(org.junit.Test)

Example 4 with ChainingPrincipalResolver

use of org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver in project cas by apereo.

the class PersonDirectoryPrincipalResolverTests method verifyChainingResolver.

@Test
public void verifyChainingResolver() {
    final PersonDirectoryPrincipalResolver resolver = new PersonDirectoryPrincipalResolver();
    resolver.setAttributeRepository(CoreAuthenticationTestUtils.getAttributeRepository());
    final ChainingPrincipalResolver chain = new ChainingPrincipalResolver();
    chain.setChain(Arrays.asList(resolver, new EchoingPrincipalResolver()));
    final Principal p = chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), CoreAuthenticationTestUtils.getPrincipal(CoreAuthenticationTestUtils.CONST_USERNAME, Collections.singletonMap(ATTR_1, "value")), new SimpleTestUsernamePasswordAuthenticationHandler());
    assertEquals(p.getAttributes().size(), CoreAuthenticationTestUtils.getAttributeRepository().getPossibleUserAttributeNames().size() + 1);
    assertTrue(p.getAttributes().containsKey(ATTR_1));
}
Also used : PersonDirectoryPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.PersonDirectoryPrincipalResolver) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) EchoingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.EchoingPrincipalResolver) Test(org.junit.Test)

Example 5 with ChainingPrincipalResolver

use of org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver in project cas by apereo.

the class PersonDirectoryPrincipalResolverTests method verifyChainingResolverDistinct.

@Test
public void verifyChainingResolverDistinct() {
    final PersonDirectoryPrincipalResolver resolver = new PersonDirectoryPrincipalResolver();
    resolver.setAttributeRepository(CoreAuthenticationTestUtils.getAttributeRepository());
    final ChainingPrincipalResolver chain = new ChainingPrincipalResolver();
    chain.setChain(Arrays.asList(resolver, new EchoingPrincipalResolver()));
    this.thrown.expect(PrincipalException.class);
    chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), CoreAuthenticationTestUtils.getPrincipal("somethingelse", Collections.singletonMap(ATTR_1, "value")), new SimpleTestUsernamePasswordAuthenticationHandler());
}
Also used : PersonDirectoryPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.PersonDirectoryPrincipalResolver) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) EchoingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.EchoingPrincipalResolver) Test(org.junit.Test)

Aggregations

ChainingPrincipalResolver (org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver)6 Test (org.junit.Test)6 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)5 EchoingPrincipalResolver (org.apereo.cas.authentication.principal.resolvers.EchoingPrincipalResolver)4 PersonDirectoryPrincipalResolver (org.apereo.cas.authentication.principal.resolvers.PersonDirectoryPrincipalResolver)4 HashMap (java.util.HashMap)2 Credential (org.apereo.cas.authentication.Credential)2 AuthenticationHandler (org.apereo.cas.authentication.AuthenticationHandler)1 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1