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"));
}
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());
}
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;
}
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());
}
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());
}
Aggregations