Search in sources :

Example 11 with StubPersonAttributeDao

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

the class PersonDirectoryPrincipalResolverTests method verifyNoPrincipalAttrWithoutNull.

@Test
public void verifyNoPrincipalAttrWithoutNull() {
    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)).principalAttributeNames("cn").build();
    val resolver = new PersonDirectoryPrincipalResolver(context);
    val p = resolver.resolve(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword(), Optional.of(CoreAuthenticationTestUtils.getPrincipal()), Optional.of(new SimpleTestUsernamePasswordAuthenticationHandler()));
    assertNotNull(p);
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) 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 12 with StubPersonAttributeDao

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

the class RegisteredServiceAttributeReleasePolicyTests method checkServiceAttributeFilterAllAttributesWithCachingTurnedOn.

@Test
public void checkServiceAttributeFilterAllAttributesWithCachingTurnedOn() {
    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(), 100);
    repository.setAttributeRepositoryIds(Set.of(stub.getId()));
    val p = PrincipalFactoryUtils.newPrincipalFactory().createPrincipal("uid", Collections.singletonMap("mail", List.of("final@example.com")));
    policy.setPrincipalAttributesRepository(repository);
    val context = RegisteredServiceAttributeReleasePolicyContext.builder().registeredService(CoreAttributesTestUtils.getRegisteredService()).service(CoreAttributesTestUtils.getService()).principal(p).build();
    val attr = policy.getAttributes(context);
    assertEquals(attributes.size() + 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 13 with StubPersonAttributeDao

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

the class CoreAuthenticationUtilsTests method verifyAttributeRepositoriesByFilter.

@Test
public void verifyAttributeRepositoriesByFilter() {
    val repository = new StubPersonAttributeDao(CoreAuthenticationTestUtils.getAttributes()) {

        @Override
        public IPersonAttributes getPerson(final String uid, final IPersonAttributeDaoFilter filter) {
            if (filter.choosePersonAttributeDao(this)) {
                return super.getPerson(uid, filter);
            }
            return null;
        }
    };
    var attrs = CoreAuthenticationUtils.retrieveAttributesFromAttributeRepository(repository, "casuser", Set.of("*"), Optional.of(CoreAuthenticationTestUtils.getPrincipal("casuser")));
    assertTrue(attrs.containsKey("uid"));
    assertTrue(attrs.containsKey("mail"));
    assertTrue(attrs.containsKey("memberOf"));
    attrs = CoreAuthenticationUtils.retrieveAttributesFromAttributeRepository(repository, "casuser", Set.of("Invalid"), Optional.of(CoreAuthenticationTestUtils.getPrincipal("casuser")));
    assertTrue(attrs.isEmpty());
}
Also used : lombok.val(lombok.val) IPersonAttributeDaoFilter(org.apereo.services.persondir.IPersonAttributeDaoFilter) 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