Search in sources :

Example 1 with PersonDirectoryPrincipalResolverProperties

use of org.apereo.cas.configuration.model.core.authentication.PersonDirectoryPrincipalResolverProperties in project cas by apereo.

the class CoreAuthenticationUtils method buildPrincipalResolutionContext.

/**
 * New PrincipalResolutionContext.
 *
 * @param principalFactory    the principal factory
 * @param attributeRepository the attribute repository
 * @param attributeMerger     the attribute merger
 * @param personDirectory     the person directory properties
 * @return the resolver
 */
public static PrincipalResolutionContext buildPrincipalResolutionContext(final PrincipalFactory principalFactory, final IPersonAttributeDao attributeRepository, final IAttributeMerger attributeMerger, final PersonDirectoryPrincipalResolverProperties... personDirectory) {
    val transformers = Arrays.stream(personDirectory).map(p -> PrincipalNameTransformerUtils.newPrincipalNameTransformer(p.getPrincipalTransformation())).collect(Collectors.toList());
    val transformer = new ChainingPrincipalNameTransformer(transformers);
    return PrincipalResolutionContext.builder().attributeRepository(attributeRepository).attributeMerger(attributeMerger).principalFactory(principalFactory).returnNullIfNoAttributes(Arrays.stream(personDirectory).filter(p -> p.getReturnNull() != TriStateBoolean.UNDEFINED).map(p -> p.getReturnNull().toBoolean()).findFirst().orElse(Boolean.FALSE)).principalAttributeNames(Arrays.stream(personDirectory).map(PersonDirectoryPrincipalResolverProperties::getPrincipalAttribute).filter(StringUtils::isNotBlank).findFirst().orElse(StringUtils.EMPTY)).principalNameTransformer(transformer).useCurrentPrincipalId(Arrays.stream(personDirectory).filter(p -> p.getUseExistingPrincipalId() != TriStateBoolean.UNDEFINED).map(p -> p.getUseExistingPrincipalId().toBoolean()).findFirst().orElse(Boolean.FALSE)).resolveAttributes(Arrays.stream(personDirectory).filter(p -> p.getAttributeResolutionEnabled() != TriStateBoolean.UNDEFINED).map(p -> p.getAttributeResolutionEnabled().toBoolean()).findFirst().orElse(Boolean.TRUE)).activeAttributeRepositoryIdentifiers(Arrays.stream(personDirectory).filter(p -> StringUtils.isNotBlank(p.getActiveAttributeRepositoryIds())).map(p -> org.springframework.util.StringUtils.commaDelimitedListToSet(p.getActiveAttributeRepositoryIds())).filter(p -> !p.isEmpty()).findFirst().orElse(Collections.EMPTY_SET)).build();
}
Also used : lombok.val(lombok.val) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) ReplacingAttributeAdder(org.apereo.services.persondir.support.merger.ReplacingAttributeAdder) GroovyPasswordPolicyHandlingStrategy(org.apereo.cas.authentication.support.password.GroovyPasswordPolicyHandlingStrategy) Assertion(org.apereo.cas.validation.Assertion) GroovyIPAddressIntelligenceService(org.apereo.cas.authentication.adaptive.intel.GroovyIPAddressIntelligenceService) Beans(org.apereo.cas.configuration.support.Beans) StringUtils(org.apache.commons.lang3.StringUtils) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ClassUtils(org.apache.commons.lang3.ClassUtils) PrincipalFactory(org.apereo.cas.authentication.principal.PrincipalFactory) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Splitter(com.google.common.base.Splitter) DefaultIPAddressIntelligenceService(org.apereo.cas.authentication.adaptive.intel.DefaultIPAddressIntelligenceService) Unchecked(org.jooq.lambda.Unchecked) Predicate(java.util.function.Predicate) Collection(java.util.Collection) PrincipalResolver(org.apereo.cas.authentication.principal.PrincipalResolver) IAttributeMerger(org.apereo.services.persondir.support.merger.IAttributeMerger) Set(java.util.Set) MultivaluedAttributeMerger(org.apereo.services.persondir.support.merger.MultivaluedAttributeMerger) AuthenticationPolicyProperties(org.apereo.cas.configuration.model.core.authentication.AuthenticationPolicyProperties) Collectors(java.util.stream.Collectors) PasswordPolicyProperties(org.apereo.cas.configuration.model.core.authentication.PasswordPolicyProperties) IPersonAttributeDaoFilter(org.apereo.services.persondir.IPersonAttributeDaoFilter) StandardCharsets(java.nio.charset.StandardCharsets) RejectResultCodePasswordPolicyHandlingStrategy(org.apereo.cas.authentication.support.password.RejectResultCodePasswordPolicyHandlingStrategy) IOUtils(org.apache.commons.io.IOUtils) AllCredentialsValidatedAuthenticationPolicy(org.apereo.cas.authentication.policy.AllCredentialsValidatedAuthenticationPolicy) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) IPAddressIntelligenceService(org.apereo.cas.authentication.adaptive.intel.IPAddressIntelligenceService) Optional(java.util.Optional) Principal(org.apereo.cas.authentication.principal.Principal) PrincipalResolutionContext(org.apereo.cas.authentication.principal.resolvers.PrincipalResolutionContext) Pattern(java.util.regex.Pattern) GroovyClassLoader(groovy.lang.GroovyClassLoader) AtLeastOneCredentialValidatedAuthenticationPolicy(org.apereo.cas.authentication.policy.AtLeastOneCredentialValidatedAuthenticationPolicy) RequiredAuthenticationHandlerAuthenticationPolicy(org.apereo.cas.authentication.policy.RequiredAuthenticationHandlerAuthenticationPolicy) AllAuthenticationHandlersSucceededAuthenticationPolicy(org.apereo.cas.authentication.policy.AllAuthenticationHandlersSucceededAuthenticationPolicy) ArrayUtils(org.apache.commons.lang3.ArrayUtils) HashMap(java.util.HashMap) RestfulIPAddressIntelligenceService(org.apereo.cas.authentication.adaptive.intel.RestfulIPAddressIntelligenceService) Multimap(com.google.common.collect.Multimap) PersonDirectoryPrincipalResolverProperties(org.apereo.cas.configuration.model.core.authentication.PersonDirectoryPrincipalResolverProperties) NoncollidingAttributeAdder(org.apereo.services.persondir.support.merger.NoncollidingAttributeAdder) ArrayList(java.util.ArrayList) UtilityClass(lombok.experimental.UtilityClass) LinkedHashMap(java.util.LinkedHashMap) NotPreventedAuthenticationPolicy(org.apereo.cas.authentication.policy.NotPreventedAuthenticationPolicy) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) CollectionUtils(org.apereo.cas.util.CollectionUtils) ChainingPrincipalNameTransformer(org.apereo.cas.util.transforms.ChainingPrincipalNameTransformer) PersonDirectoryPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.PersonDirectoryPrincipalResolver) BaseAdditiveAttributeMerger(org.apereo.services.persondir.support.merger.BaseAdditiveAttributeMerger) lombok.val(lombok.val) GroovyScriptAuthenticationPolicy(org.apereo.cas.authentication.policy.GroovyScriptAuthenticationPolicy) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ApplicationContext(org.springframework.context.ApplicationContext) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) TriStateBoolean(org.apereo.cas.util.model.TriStateBoolean) PrincipalNameTransformerUtils(org.apereo.cas.authentication.principal.PrincipalNameTransformerUtils) DefaultPasswordPolicyHandlingStrategy(org.apereo.cas.authentication.support.password.DefaultPasswordPolicyHandlingStrategy) PrincipalAttributesCoreProperties(org.apereo.cas.configuration.model.core.authentication.PrincipalAttributesCoreProperties) RestfulAuthenticationPolicy(org.apereo.cas.authentication.policy.RestfulAuthenticationPolicy) Collections(java.util.Collections) StringUtils(org.apache.commons.lang3.StringUtils) ChainingPrincipalNameTransformer(org.apereo.cas.util.transforms.ChainingPrincipalNameTransformer)

Example 2 with PersonDirectoryPrincipalResolverProperties

use of org.apereo.cas.configuration.model.core.authentication.PersonDirectoryPrincipalResolverProperties in project cas by apereo.

the class CoreAuthenticationUtilsTests method verifyPrincipalConflictResolution.

@Test
public void verifyPrincipalConflictResolution() {
    val r1 = CoreAuthenticationUtils.newPrincipalElectionStrategyConflictResolver(new PersonDirectoryPrincipalResolverProperties().setPrincipalResolutionConflictStrategy("LAST"));
    assertNotNull(r1);
    val r2 = CoreAuthenticationUtils.newPrincipalElectionStrategyConflictResolver(new PersonDirectoryPrincipalResolverProperties().setPrincipalResolutionConflictStrategy("FIRST"));
    assertNotNull(r2);
    val r3 = CoreAuthenticationUtils.newPrincipalElectionStrategyConflictResolver(new PersonDirectoryPrincipalResolverProperties().setPrincipalResolutionConflictStrategy("INVALID"));
    assertEquals(r3, r1);
}
Also used : lombok.val(lombok.val) PersonDirectoryPrincipalResolverProperties(org.apereo.cas.configuration.model.core.authentication.PersonDirectoryPrincipalResolverProperties) Test(org.junit.jupiter.api.Test)

Example 3 with PersonDirectoryPrincipalResolverProperties

use of org.apereo.cas.configuration.model.core.authentication.PersonDirectoryPrincipalResolverProperties in project cas by apereo.

the class CoreAuthenticationUtilsTests method verifyPersonDirectoryOverrides.

@Test
public void verifyPersonDirectoryOverrides() {
    val principal = new PersonDirectoryPrincipalResolverProperties();
    val personDirectory = new PersonDirectoryPrincipalResolverProperties();
    val principalResolutionContext = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), principal, personDirectory);
    assertFalse(principalResolutionContext.isUseCurrentPrincipalId());
    assertTrue(principalResolutionContext.isResolveAttributes());
    assertFalse(principalResolutionContext.isReturnNullIfNoAttributes());
    assertTrue(principalResolutionContext.getActiveAttributeRepositoryIdentifiers().isEmpty());
    assertTrue(principalResolutionContext.getPrincipalAttributeNames().isEmpty());
    personDirectory.setUseExistingPrincipalId(TriStateBoolean.TRUE);
    personDirectory.setAttributeResolutionEnabled(TriStateBoolean.TRUE);
    personDirectory.setReturnNull(TriStateBoolean.TRUE);
    personDirectory.setAttributeResolutionEnabled(TriStateBoolean.FALSE);
    personDirectory.setActiveAttributeRepositoryIds("test1,test2");
    personDirectory.setPrincipalAttribute("principalAttribute");
    val principalResolutionContext2 = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), principal, personDirectory);
    assertTrue(principalResolutionContext2.isUseCurrentPrincipalId());
    assertFalse(principalResolutionContext2.isResolveAttributes());
    assertTrue(principalResolutionContext2.isReturnNullIfNoAttributes());
    assertEquals(2, principalResolutionContext2.getActiveAttributeRepositoryIdentifiers().size());
    assertEquals("principalAttribute", principalResolutionContext2.getPrincipalAttributeNames());
    principal.setUseExistingPrincipalId(TriStateBoolean.FALSE);
    principal.setAttributeResolutionEnabled(TriStateBoolean.FALSE);
    principal.setReturnNull(TriStateBoolean.FALSE);
    principal.setAttributeResolutionEnabled(TriStateBoolean.TRUE);
    principal.setActiveAttributeRepositoryIds("test1,test2,test3");
    principal.setPrincipalAttribute("principalAttribute2");
    val principalResolutionContext3 = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), principal, personDirectory);
    assertFalse(principalResolutionContext3.isUseCurrentPrincipalId());
    assertTrue(principalResolutionContext3.isResolveAttributes());
    assertFalse(principalResolutionContext3.isReturnNullIfNoAttributes());
    assertEquals(3, principalResolutionContext3.getActiveAttributeRepositoryIdentifiers().size());
    assertEquals("principalAttribute2", principalResolutionContext3.getPrincipalAttributeNames());
    val principalResolutionContext4 = CoreAuthenticationUtils.buildPrincipalResolutionContext(PrincipalFactoryUtils.newPrincipalFactory(), new StubPersonAttributeDao(Collections.EMPTY_MAP), CoreAuthenticationUtils.getAttributeMerger(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD), personDirectory);
    assertTrue(principalResolutionContext4.isUseCurrentPrincipalId());
    assertFalse(principalResolutionContext4.isResolveAttributes());
    assertTrue(principalResolutionContext4.isReturnNullIfNoAttributes());
    assertEquals(2, principalResolutionContext4.getActiveAttributeRepositoryIdentifiers().size());
    assertEquals("principalAttribute", principalResolutionContext4.getPrincipalAttributeNames());
}
Also used : lombok.val(lombok.val) PersonDirectoryPrincipalResolverProperties(org.apereo.cas.configuration.model.core.authentication.PersonDirectoryPrincipalResolverProperties) StubPersonAttributeDao(org.apereo.services.persondir.support.StubPersonAttributeDao) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)3 PersonDirectoryPrincipalResolverProperties (org.apereo.cas.configuration.model.core.authentication.PersonDirectoryPrincipalResolverProperties)3 Test (org.junit.jupiter.api.Test)2 Splitter (com.google.common.base.Splitter)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Multimap (com.google.common.collect.Multimap)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Pattern (java.util.regex.Pattern)1