Search in sources :

Example 16 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class SurrogatePrincipalResolverTests method verifyPrincipalResolutionPlan.

@Test
public void verifyPrincipalResolutionPlan() {
    val surrogatePrincipalBuilder = new SurrogatePrincipalBuilder(PrincipalFactoryUtils.newPrincipalFactory(), CoreAuthenticationTestUtils.getAttributeRepository(), new SimpleSurrogateAuthenticationService(Map.of("test", List.of("surrogate")), mock(ServicesManager.class)));
    val upc = CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword();
    val surrogateCreds = new SurrogateUsernamePasswordCredential();
    surrogateCreds.setSurrogateUsername("surrogate");
    surrogateCreds.setUsername(upc.getUsername());
    val plan = new DefaultPrincipalResolutionExecutionPlan();
    val context = getPrincipalResolutionContext(StringUtils.EMPTY, CoreAuthenticationTestUtils.getAttributeRepository());
    plan.registerPrincipalResolver(new PersonDirectoryPrincipalResolver(context));
    plan.registerPrincipalResolver(new SurrogatePrincipalResolver(context).setSurrogatePrincipalBuilder(surrogatePrincipalBuilder));
    val resolver = new ChainingPrincipalResolver(new DefaultPrincipalElectionStrategy(), casProperties);
    resolver.setChain(plan.getRegisteredPrincipalResolvers());
    val upcPrincipal = resolver.resolve(upc, Optional.of(CoreAuthenticationTestUtils.getPrincipal("test")), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertNotNull(upcPrincipal);
    assertEquals(1, upcPrincipal.getAttributes().get("givenName").size());
    assertEquals(upc.getId(), upcPrincipal.getId());
    val surrogatePrincipal = resolver.resolve(surrogateCreds, Optional.of(CoreAuthenticationTestUtils.getPrincipal("casuser")), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertNotNull(surrogatePrincipal);
    assertEquals(1, surrogatePrincipal.getAttributes().get("givenName").size());
    assertEquals(surrogateCreds.getId(), surrogatePrincipal.getId());
}
Also used : lombok.val(lombok.val) PersonDirectoryPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.PersonDirectoryPrincipalResolver) DefaultPrincipalResolutionExecutionPlan(org.apereo.cas.authentication.principal.DefaultPrincipalResolutionExecutionPlan) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) SimpleSurrogateAuthenticationService(org.apereo.cas.authentication.surrogate.SimpleSurrogateAuthenticationService) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 17 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class PersonDirectoryPrincipalResolverTests method verifyChainingResolverOverwritePrincipal.

@Test
public void verifyChainingResolverOverwritePrincipal() {
    val context1 = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(CoreAuthenticationTestUtils.getAttributeRepository()).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(formUserId -> formUserId).useCurrentPrincipalId(false).resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
    val resolver = new PersonDirectoryPrincipalResolver(context1);
    val context2 = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(new StubPersonAttributeDao(Collections.singletonMap("principal", CollectionUtils.wrap("changedPrincipal")))).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalAttributeNames("principal").principalNameTransformer(formUserId -> formUserId).useCurrentPrincipalId(false).resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
    val resolver2 = new PersonDirectoryPrincipalResolver(context2);
    val chain = new ChainingPrincipalResolver(new DefaultPrincipalElectionStrategy(), casProperties);
    chain.setChain(Arrays.asList(new EchoingPrincipalResolver(), resolver, resolver2));
    val p = chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), Optional.of(CoreAuthenticationTestUtils.getPrincipal("somethingelse", Collections.singletonMap(ATTR_1, List.of("value")))), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertNotNull(p);
    assertEquals("changedPrincipal", p.getId());
    assertEquals(7, p.getAttributes().size());
    assertTrue(p.getAttributes().containsKey(ATTR_1));
    assertTrue(p.getAttributes().containsKey("principal"));
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) StubPersonAttributeDao(org.apereo.services.persondir.support.StubPersonAttributeDao) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class PersonDirectoryPrincipalResolverTests method verifyChainingResolver.

@Test
public void verifyChainingResolver() {
    val context = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(CoreAuthenticationTestUtils.getAttributeRepository()).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(formUserId -> formUserId).useCurrentPrincipalId(false).resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
    val resolver = new PersonDirectoryPrincipalResolver(context);
    val chain = new ChainingPrincipalResolver(new DefaultPrincipalElectionStrategy(), casProperties);
    chain.setChain(Arrays.asList(new EchoingPrincipalResolver(), resolver));
    val p = chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), Optional.of(CoreAuthenticationTestUtils.getPrincipal(CoreAuthenticationTestUtils.CONST_USERNAME, Collections.singletonMap(ATTR_1, List.of("value")))), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertEquals(p.getAttributes().size(), CoreAuthenticationTestUtils.getAttributeRepository().getPossibleUserAttributeNames(IPersonAttributeDaoFilter.alwaysChoose()).size() + 1);
    assertTrue(p.getAttributes().containsKey(ATTR_1));
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class PersonDirectoryPrincipalResolverTests method verifyMultiplePrincipalAttributeNamesNotFound.

@Test
public void verifyMultiplePrincipalAttributeNamesNotFound() {
    val context1 = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(CoreAuthenticationTestUtils.getAttributeRepository()).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(formUserId -> formUserId).useCurrentPrincipalId(false).resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
    val resolver = new PersonDirectoryPrincipalResolver(context1);
    val context2 = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(new StubPersonAttributeDao(Collections.singletonMap("something", CollectionUtils.wrap("principal-id")))).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(formUserId -> formUserId).useCurrentPrincipalId(false).principalAttributeNames(" invalid, ").resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
    val resolver2 = new PersonDirectoryPrincipalResolver(context2);
    val chain = new ChainingPrincipalResolver(new DefaultPrincipalElectionStrategy(), casProperties);
    chain.setChain(Arrays.asList(new EchoingPrincipalResolver(), resolver, resolver2));
    val p = chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), Optional.of(CoreAuthenticationTestUtils.getPrincipal("somethingelse", Collections.singletonMap(ATTR_1, List.of("value")))), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertNotNull(p);
    assertEquals("test", p.getId());
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Arrays(java.util.Arrays) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) RefreshAutoConfiguration(org.springframework.cloud.autoconfigure.RefreshAutoConfiguration) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) Tag(org.junit.jupiter.api.Tag) Credential(org.apereo.cas.authentication.Credential) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) PrincipalFactoryUtils(org.apereo.cas.authentication.principal.PrincipalFactoryUtils) lombok.val(lombok.val) StubPersonAttributeDao(org.apereo.services.persondir.support.StubPersonAttributeDao) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) IPersonAttributeDaoFilter(org.apereo.services.persondir.IPersonAttributeDaoFilter) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Assertions(org.junit.jupiter.api.Assertions) PrincipalAttributesCoreProperties(org.apereo.cas.configuration.model.core.authentication.PrincipalAttributesCoreProperties) Optional(java.util.Optional) CoreAuthenticationUtils(org.apereo.cas.authentication.CoreAuthenticationUtils) Collections(java.util.Collections) CoreAuthenticationTestUtils(org.apereo.cas.authentication.CoreAuthenticationTestUtils) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) StubPersonAttributeDao(org.apereo.services.persondir.support.StubPersonAttributeDao) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 20 with DefaultPrincipalElectionStrategy

use of org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy in project cas by apereo.

the class DefaultAuthenticationResultBuilderTests method verifyAuthenticationResultMergesPrincipalAttributes.

@Test
public void verifyAuthenticationResultMergesPrincipalAttributes() {
    val builder = new DefaultAuthenticationResultBuilder();
    val p1 = CoreAuthenticationTestUtils.getPrincipal("casuser1", CollectionUtils.wrap("givenName", "CAS", "uid", "casuser1"));
    val p2 = CoreAuthenticationTestUtils.getPrincipal("casuser2", CollectionUtils.wrap("email", "cas@example.org", "givenName", "CAS SSO", "uid", "casuser2"));
    val authn1 = CoreAuthenticationTestUtils.getAuthentication(p1, CollectionUtils.wrap("authn", "test1"));
    val authn2 = CoreAuthenticationTestUtils.getAuthentication(p2, CollectionUtils.wrap("authn", "test2"));
    val principalElectionStrategy = new DefaultPrincipalElectionStrategy();
    var attributeMerger = CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.MULTIVALUED);
    principalElectionStrategy.setAttributeMerger(attributeMerger);
    val result = builder.collect(authn1).collect(authn2).build(principalElectionStrategy);
    val authentication = result.getAuthentication();
    assertNotNull(authentication);
    val authnAttributes = authentication.getAttributes();
    assertTrue(authnAttributes.containsKey("authn"));
    assertEquals(2, ((Collection) authnAttributes.get("authn")).size());
    val principal = authentication.getPrincipal();
    assertNotNull(principal);
    val attributes = principal.getAttributes();
    assertFalse(attributes.isEmpty());
    assertTrue(attributes.containsKey("uid"));
    assertTrue(attributes.containsKey("givenName"));
    assertEquals(2, ((Collection) attributes.get("uid")).size());
    assertEquals(2, ((Collection) attributes.get("givenName")).size());
    assertEquals(1, ((Collection) attributes.get("email")).size());
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)20 DefaultPrincipalElectionStrategy (org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy)20 Test (org.junit.jupiter.api.Test)13 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 HashMap (java.util.HashMap)4 DefaultAuthenticationResultBuilderFactory (org.apereo.cas.authentication.DefaultAuthenticationResultBuilderFactory)4 DefaultAuthenticationSystemSupport (org.apereo.cas.authentication.DefaultAuthenticationSystemSupport)4 DefaultAuthenticationTransactionFactory (org.apereo.cas.authentication.DefaultAuthenticationTransactionFactory)4 DefaultAuthenticationTransactionManager (org.apereo.cas.authentication.DefaultAuthenticationTransactionManager)4 Credential (org.apereo.cas.authentication.Credential)3 DefaultAuthenticationResultBuilder (org.apereo.cas.authentication.DefaultAuthenticationResultBuilder)3 WebApplicationServiceFactory (org.apereo.cas.authentication.principal.WebApplicationServiceFactory)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2