Search in sources :

Example 6 with CachingPrincipalAttributesRepository

use of org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository in project cas by apereo.

the class RegisteredServiceTestUtils method getRegisteredService.

@SneakyThrows
public static AbstractRegisteredService getRegisteredService(final String id) {
    final RegexRegisteredService s = new RegexRegisteredService();
    s.setServiceId(id);
    s.setEvaluationOrder(1);
    s.setName("Test registered service " + id);
    s.setDescription("Registered service description");
    s.setProxyPolicy(new RegexMatchingRegisteredServiceProxyPolicy("^https?://.+"));
    s.setId(RandomUtils.getNativeInstance().nextInt(Math.abs(s.hashCode())));
    s.setTheme("exampleTheme");
    s.setUsernameAttributeProvider(new PrincipalAttributeRegisteredServiceUsernameProvider("uid"));
    final DefaultRegisteredServiceAccessStrategy accessStrategy = new DefaultRegisteredServiceAccessStrategy(true, true);
    accessStrategy.setRequireAllAttributes(true);
    accessStrategy.setRequiredAttributes(getTestAttributes());
    s.setAccessStrategy(accessStrategy);
    s.setLogo("https://logo.example.org/logo.png");
    s.setLogoutType(LogoutType.BACK_CHANNEL);
    s.setLogoutUrl(new URL("https://sys.example.org/logout.png"));
    s.setProxyPolicy(new RegexMatchingRegisteredServiceProxyPolicy("^http.+"));
    s.setPublicKey(new RegisteredServicePublicKeyImpl("classpath:RSA1024Public.key", "RSA"));
    final ReturnAllowedAttributeReleasePolicy policy = new ReturnAllowedAttributeReleasePolicy();
    policy.setAuthorizedToReleaseCredentialPassword(true);
    policy.setAuthorizedToReleaseProxyGrantingTicket(true);
    final CachingPrincipalAttributesRepository repo = new CachingPrincipalAttributesRepository(TimeUnit.SECONDS.name(), 10);
    repo.setMergingStrategy(AbstractPrincipalAttributesRepository.MergingStrategy.ADD);
    policy.setPrincipalAttributesRepository(repo);
    policy.setAttributeFilter(new RegisteredServiceRegexAttributeFilter("https://.+"));
    policy.setAllowedAttributes(new ArrayList<>(getTestAttributes().keySet()));
    s.setAttributeReleasePolicy(policy);
    return s;
}
Also used : RegisteredServiceRegexAttributeFilter(org.apereo.cas.services.support.RegisteredServiceRegexAttributeFilter) CachingPrincipalAttributesRepository(org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository) URL(java.net.URL) SneakyThrows(lombok.SneakyThrows)

Example 7 with CachingPrincipalAttributesRepository

use of org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository in project cas by apereo.

the class RegisteredServiceTestUtils method getRegisteredService.

@SneakyThrows
public static AbstractRegisteredService getRegisteredService(final String id, final Class<? extends RegisteredService> clazz, final boolean uniq, final Map requiredAttributes) {
    val s = (AbstractRegisteredService) clazz.getDeclaredConstructor().newInstance();
    s.setServiceId(id);
    s.setEvaluationOrder(1);
    if (uniq) {
        val uuid = Iterables.get(Splitter.on('-').split(UUID.randomUUID().toString()), 0);
        s.setName("TestService" + uuid);
    } else {
        s.setName(id);
    }
    s.setDescription("Registered service description");
    s.setProxyPolicy(new RegexMatchingRegisteredServiceProxyPolicy("^https?://.+"));
    s.setId(RandomUtils.nextInt());
    s.setTheme("exampleTheme");
    s.setUsernameAttributeProvider(new PrincipalAttributeRegisteredServiceUsernameProvider("uid"));
    val accessStrategy = new DefaultRegisteredServiceAccessStrategy(true, true);
    accessStrategy.setRequireAllAttributes(true);
    accessStrategy.setRequiredAttributes(requiredAttributes);
    accessStrategy.setUnauthorizedRedirectUrl(new URI("https://www.github.com"));
    s.setAccessStrategy(accessStrategy);
    s.setLogo("https://logo.example.org/logo.png");
    s.setLogoutType(RegisteredServiceLogoutType.BACK_CHANNEL);
    s.setLogoutUrl("https://sys.example.org/logout.png");
    s.setProxyPolicy(new RegexMatchingRegisteredServiceProxyPolicy("^http.+"));
    s.setPublicKey(new RegisteredServicePublicKeyImpl("classpath:RSA1024Public.key", "RSA"));
    val policy = new ReturnAllowedAttributeReleasePolicy();
    policy.setAuthorizedToReleaseCredentialPassword(true);
    policy.setAuthorizedToReleaseProxyGrantingTicket(true);
    val repo = new CachingPrincipalAttributesRepository(TimeUnit.SECONDS.name(), 10);
    repo.setMergingStrategy(PrincipalAttributesCoreProperties.MergingStrategyTypes.ADD);
    policy.setPrincipalAttributesRepository(repo);
    policy.setAttributeFilter(new RegisteredServiceRegexAttributeFilter("https://.+"));
    policy.setAllowedAttributes(new ArrayList<>(getTestAttributes().keySet()));
    s.setAttributeReleasePolicy(policy);
    return s;
}
Also used : lombok.val(lombok.val) RegisteredServiceRegexAttributeFilter(org.apereo.cas.services.support.RegisteredServiceRegexAttributeFilter) URI(java.net.URI) CachingPrincipalAttributesRepository(org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository) SneakyThrows(lombok.SneakyThrows)

Example 8 with CachingPrincipalAttributesRepository

use of org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository 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)

Aggregations

CachingPrincipalAttributesRepository (org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository)8 lombok.val (lombok.val)3 HashMap (java.util.HashMap)2 SneakyThrows (lombok.SneakyThrows)2 DefaultPrincipalAttributesRepository (org.apereo.cas.authentication.principal.DefaultPrincipalAttributesRepository)2 RegisteredServiceRegexAttributeFilter (org.apereo.cas.services.support.RegisteredServiceRegexAttributeFilter)2 MergingPersonAttributeDaoImpl (org.apereo.services.persondir.support.MergingPersonAttributeDaoImpl)2 StubPersonAttributeDao (org.apereo.services.persondir.support.StubPersonAttributeDao)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 URI (java.net.URI)1 URL (java.net.URL)1 PrincipalAttributesProperties (org.apereo.cas.configuration.model.core.authentication.PrincipalAttributesProperties)1 RegisteredServiceAttributeReleasePolicyEditBean (org.apereo.cas.mgmt.services.web.beans.RegisteredServiceAttributeReleasePolicyEditBean)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 RefreshScope (org.springframework.cloud.context.config.annotation.RefreshScope)1 Bean (org.springframework.context.annotation.Bean)1