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