Search in sources :

Example 1 with StubPersonAttributeDao

use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.

the class PersonDirectoryPrincipalResolverTests method verifyNullPrincipal.

@Test
public void verifyNullPrincipal() {
    val context = PrincipalResolutionContext.builder().attributeMerger(CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger())).attributeRepository(new StubPersonAttributeDao(new HashMap<>(0))).principalFactory(PrincipalFactoryUtils.newPrincipalFactory()).returnNullIfNoAttributes(false).principalNameTransformer(String::trim).useCurrentPrincipalId(false).resolveAttributes(true).activeAttributeRepositoryIdentifiers(CollectionUtils.wrapSet(IPersonAttributeDao.WILDCARD)).build();
    val resolver = new PersonDirectoryPrincipalResolver(context);
    val p = resolver.resolve(() -> null, Optional.of(CoreAuthenticationTestUtils.getPrincipal()), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertNull(p);
}
Also used : lombok.val(lombok.val) 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 2 with StubPersonAttributeDao

use of org.apereo.services.persondir.support.StubPersonAttributeDao 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 StubPersonAttributeDao

use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.

the class CoreAuthenticationTestUtils method getAttributeRepository.

public static StubPersonAttributeDao getAttributeRepository() {
    val attributes = new HashMap<String, List<Object>>();
    attributes.put("uid", CollectionUtils.wrap(CONST_USERNAME));
    attributes.put("cn", CollectionUtils.wrap(CONST_USERNAME.toUpperCase()));
    attributes.put("givenName", CollectionUtils.wrap(CONST_USERNAME));
    attributes.put("mail", CollectionUtils.wrap(CONST_USERNAME + "@example.org"));
    attributes.put("memberOf", CollectionUtils.wrapList("system", "admin", "cas", "staff"));
    return new StubPersonAttributeDao(attributes);
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) StubPersonAttributeDao(org.apereo.services.persondir.support.StubPersonAttributeDao)

Example 4 with StubPersonAttributeDao

use of org.apereo.services.persondir.support.StubPersonAttributeDao in project cas by apereo.

the class RegisteredServiceAttributeReleasePolicyTests method checkServiceAttributeFilterByAttributeRepositoryId.

@Test
public void checkServiceAttributeFilterByAttributeRepositoryId() {
    val policy = new ReturnAllAttributeReleasePolicy();
    val attributes = new HashMap<String, List<Object>>();
    attributes.put("values", Arrays.asList(new Object[] { "v1", "v2", "v3" }));
    attributes.put("cn", Arrays.asList(new Object[] { "commonName" }));
    attributes.put("username", Arrays.asList(new Object[] { "uid" }));
    val person = mock(IPersonAttributes.class);
    when(person.getName()).thenReturn("uid");
    when(person.getAttributes()).thenReturn(attributes);
    val stub = new StubPersonAttributeDao(attributes);
    stub.setId("SampleStubRepository");
    val dao = new MergingPersonAttributeDaoImpl();
    dao.setPersonAttributeDaos(List.of(stub));
    ApplicationContextProvider.registerBeanIntoApplicationContext(this.applicationContext, dao, PrincipalResolver.BEAN_NAME_ATTRIBUTE_REPOSITORY);
    val repository = new CachingPrincipalAttributesRepository(TimeUnit.MILLISECONDS.name(), 0);
    val p = PrincipalFactoryUtils.newPrincipalFactory().createPrincipal("uid", Collections.singletonMap("mail", List.of("final@example.com")));
    repository.setAttributeRepositoryIds(CollectionUtils.wrapSet("SampleStubRepository".toUpperCase()));
    policy.setPrincipalAttributesRepository(repository);
    val context = RegisteredServiceAttributeReleasePolicyContext.builder().registeredService(CoreAttributesTestUtils.getRegisteredService()).service(CoreAttributesTestUtils.getService()).principal(p).build();
    var attr = policy.getAttributes(context);
    assertEquals(attr.size(), attributes.size() + 1);
    repository.setAttributeRepositoryIds(CollectionUtils.wrapSet("DoesNotExist"));
    policy.setPrincipalAttributesRepository(repository);
    attr = policy.getAttributes(context);
    assertEquals(1, attr.size());
}
Also used : lombok.val(lombok.val) MergingPersonAttributeDaoImpl(org.apereo.services.persondir.support.MergingPersonAttributeDaoImpl) HashMap(java.util.HashMap) CachingPrincipalAttributesRepository(org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository) StubPersonAttributeDao(org.apereo.services.persondir.support.StubPersonAttributeDao) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with StubPersonAttributeDao

use of org.apereo.services.persondir.support.StubPersonAttributeDao 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

StubPersonAttributeDao (org.apereo.services.persondir.support.StubPersonAttributeDao)13 lombok.val (lombok.val)12 Test (org.junit.jupiter.api.Test)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 HashMap (java.util.HashMap)8 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DefaultPrincipalElectionStrategy (org.apereo.cas.authentication.principal.DefaultPrincipalElectionStrategy)3 IPersonAttributeDaoFilter (org.apereo.services.persondir.IPersonAttributeDaoFilter)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2 CoreAuthenticationTestUtils (org.apereo.cas.authentication.CoreAuthenticationTestUtils)2 CoreAuthenticationUtils (org.apereo.cas.authentication.CoreAuthenticationUtils)2 Credential (org.apereo.cas.authentication.Credential)2 PrincipalFactoryUtils (org.apereo.cas.authentication.principal.PrincipalFactoryUtils)2 CachingPrincipalAttributesRepository (org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository)2