Search in sources :

Example 1 with DefaultPrincipalElectionStrategy

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

the class PersonDirectoryPrincipalResolverTests method verifyChainingResolverOverwrite.

@Test
public void verifyChainingResolverOverwrite() {
    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 attributes = new HashMap<String, List<Object>>();
    attributes.put("cn", List.of("originalCN"));
    attributes.put(ATTR_1, List.of("value1"));
    val p = chain.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), Optional.of(CoreAuthenticationTestUtils.getPrincipal(CoreAuthenticationTestUtils.CONST_USERNAME, attributes)), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertEquals(p.getAttributes().size(), CoreAuthenticationTestUtils.getAttributeRepository().getPossibleUserAttributeNames(IPersonAttributeDaoFilter.alwaysChoose()).size() + 1);
    assertTrue(p.getAttributes().containsKey(ATTR_1));
    assertTrue(p.getAttributes().containsKey("cn"));
    assertNotEquals("originalCN", p.getAttributes().get("cn"));
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) HashMap(java.util.HashMap) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with DefaultPrincipalElectionStrategy

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

the class PersonDirectoryPrincipalResolverTests method verifyMultiplePrincipalAttributeNames.

@Test
public void verifyMultiplePrincipalAttributeNames() {
    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, something").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("principal-id", 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 3 with DefaultPrincipalElectionStrategy

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

the class CoreAuthenticationTestUtils method getAuthenticationSystemSupport.

public static AuthenticationSystemSupport getAuthenticationSystemSupport() {
    val authSupport = mock(AuthenticationSystemSupport.class);
    when(authSupport.getPrincipalElectionStrategy()).thenReturn(new DefaultPrincipalElectionStrategy());
    return authSupport;
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy)

Example 4 with DefaultPrincipalElectionStrategy

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

the class DefaultAuthenticationBuilderTests method verifyMergeOperation.

@Test
public void verifyMergeOperation() {
    val builder1 = new DefaultAuthenticationBuilder(CoreAuthenticationTestUtils.getPrincipal());
    builder1.mergeAttribute("key", 12345);
    builder1.mergeAttribute("key", CollectionUtils.wrapList(54321, 998877));
    val authn1 = builder1.build();
    val builder2 = new DefaultAuthenticationBuilder(CoreAuthenticationTestUtils.getPrincipal());
    builder2.mergeAttribute("key", 112233);
    builder2.mergeAttribute("key", CollectionUtils.wrapList(443322));
    val authn2 = builder2.build();
    val resultBuilder = new DefaultAuthenticationResultBuilderFactory().newBuilder();
    val strategy = new DefaultPrincipalElectionStrategy();
    strategy.setAttributeMerger(CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.MULTIVALUED));
    val resultAuthn = resultBuilder.collect(authn1).collect(authn2).build(strategy);
    assertNotNull(resultAuthn);
    assertEquals(5, resultAuthn.getAuthentication().getAttributes().get("key").size());
}
Also used : lombok.val(lombok.val) DefaultPrincipalElectionStrategy(org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultPrincipalElectionStrategy

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

the class DefaultAuthenticationResultBuilderTests method verifyAuthenticationResultBuildsPrincipals.

@Test
public void verifyAuthenticationResultBuildsPrincipals() {
    val builder = new DefaultAuthenticationResultBuilder();
    assertFalse(builder.getInitialAuthentication().isPresent());
    assertFalse(builder.getInitialCredential().isPresent());
    val p1 = CoreAuthenticationTestUtils.getPrincipal("casuser1", CollectionUtils.wrap("uid", "casuser1"));
    val p2 = CoreAuthenticationTestUtils.getPrincipal("casuser2", CollectionUtils.wrap("givenName", "CAS"));
    val authn1 = CoreAuthenticationTestUtils.getAuthentication(p1, CollectionUtils.wrap("authn1", "first"));
    val authn2 = CoreAuthenticationTestUtils.getAuthentication(p2, CollectionUtils.wrap("authn2", "second"));
    val result = builder.collect(authn1).collect(authn2).build(new DefaultPrincipalElectionStrategy());
    val authentication = result.getAuthentication();
    assertNotNull(authentication);
    val authnAttributes = authentication.getAttributes();
    assertTrue(authnAttributes.containsKey("authn1"));
    assertTrue(authnAttributes.containsKey("authn2"));
    val principal = authentication.getPrincipal();
    assertNotNull(principal);
    val attributes = principal.getAttributes();
    assertFalse(attributes.isEmpty());
    assertTrue(attributes.containsKey("uid"));
    assertTrue(attributes.containsKey("givenName"));
    assertEquals(1, ((Collection) attributes.get("uid")).size());
    assertEquals(1, ((Collection) attributes.get("givenName")).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