Search in sources :

Example 1 with DelegatedClientIdentityProviderConfiguration

use of org.apereo.cas.web.DelegatedClientIdentityProviderConfiguration in project cas by apereo.

the class DefaultDelegatedClientIdentityProviderConfigurationProducer method produce.

@Override
public Optional<DelegatedClientIdentityProviderConfiguration> produce(final RequestContext requestContext, final IndirectClient client) {
    val request = WebUtils.getHttpServletRequestFromExternalWebflowContext(requestContext);
    val response = WebUtils.getHttpServletResponseFromExternalWebflowContext(requestContext);
    val webContext = new JEEContext(request, response);
    val currentService = WebUtils.getService(requestContext);
    LOGGER.debug("Initializing client [{}] with request parameters [{}] and service [{}]", client, requestContext.getRequestParameters(), currentService);
    client.init();
    if (delegatedClientAuthenticationRequestCustomizers.isEmpty() || delegatedClientAuthenticationRequestCustomizers.stream().anyMatch(c -> c.isAuthorized(webContext, client, currentService))) {
        return DelegatedClientIdentityProviderConfigurationFactory.builder().client(client).webContext(webContext).service(currentService).casProperties(casProperties).build().resolve();
    }
    return Optional.empty();
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) DelegatedClientIdentityProviderRedirectionStrategy(org.apereo.cas.pac4j.client.DelegatedClientIdentityProviderRedirectionStrategy) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DelegatedClientIdentityProviderConfiguration(org.apereo.cas.web.DelegatedClientIdentityProviderConfiguration) RequestContext(org.springframework.webflow.execution.RequestContext) WebApplicationService(org.apereo.cas.authentication.principal.WebApplicationService) HashSet(java.util.HashSet) LoggingUtils(org.apereo.cas.util.LoggingUtils) HttpServletRequest(javax.servlet.http.HttpServletRequest) Clients(org.pac4j.core.client.Clients) Client(org.pac4j.core.client.Client) IndirectClient(org.pac4j.core.client.IndirectClient) JEEContext(org.pac4j.core.context.JEEContext) DelegatedClientIdentityProviderConfigurationFactory(org.apereo.cas.web.DelegatedClientIdentityProviderConfigurationFactory) LinkedHashSet(java.util.LinkedHashSet) DelegatedClientAuthenticationRequestCustomizer(org.apereo.cas.pac4j.client.DelegatedClientAuthenticationRequestCustomizer) lombok.val(lombok.val) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) Set(java.util.Set) HttpStatus(org.springframework.http.HttpStatus) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DelegatedAuthenticationAccessStrategyHelper(org.apereo.cas.validation.DelegatedAuthenticationAccessStrategyHelper) Service(org.apereo.cas.authentication.principal.Service) Optional(java.util.Optional) WebUtils(org.apereo.cas.web.support.WebUtils) JEEContext(org.pac4j.core.context.JEEContext)

Example 2 with DelegatedClientIdentityProviderConfiguration

use of org.apereo.cas.web.DelegatedClientIdentityProviderConfiguration in project cas by apereo.

the class GroovyDelegatedClientIdentityProviderRedirectionStrategy method getPrimaryDelegatedAuthenticationProvider.

@Override
public Optional<DelegatedClientIdentityProviderConfiguration> getPrimaryDelegatedAuthenticationProvider(final RequestContext context, final WebApplicationService service, final DelegatedClientIdentityProviderConfiguration provider) {
    val registeredService = servicesManager.findServiceBy(service);
    val args = new Object[] { context, service, registeredService, provider, LOGGER };
    return Optional.ofNullable(watchableScript.execute(args, DelegatedClientIdentityProviderConfiguration.class));
}
Also used : lombok.val(lombok.val) DelegatedClientIdentityProviderConfiguration(org.apereo.cas.web.DelegatedClientIdentityProviderConfiguration)

Example 3 with DelegatedClientIdentityProviderConfiguration

use of org.apereo.cas.web.DelegatedClientIdentityProviderConfiguration in project cas by apereo.

the class DelegatedClientAuthenticationActionTests method assertStartAuthentication.

@SneakyThrows
private void assertStartAuthentication(final Service service) {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val flow = new Flow("mockFlow");
    flow.addVariable(new FlowVariable("credential", new BeanFactoryVariableValueFactory(UsernamePasswordCredential.class, applicationContext.getAutowireCapableBeanFactory())));
    val locale = Locale.ENGLISH.getLanguage();
    request.setParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, "theme");
    LOGGER.debug("Setting locale [{}] for request parameter as [{}]", locale, request.getParameterMap());
    request.setParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, locale);
    request.setParameter(CasProtocolConstants.PARAMETER_METHOD, HttpMethod.POST.name());
    LOGGER.debug("Set request parameters as [{}]", request.getParameterMap());
    val requestContext = new MockRequestContext();
    requestContext.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(requestContext);
    ExternalContextHolder.setExternalContext(requestContext.getExternalContext());
    val mockExecutionContext = new MockFlowExecutionContext(new MockFlowSession(flow));
    requestContext.setFlowExecutionContext(mockExecutionContext);
    if (service != null) {
        WebUtils.putServiceIntoFlowScope(requestContext, service);
    }
    val client = builtClients.findClient("SAML2Client").get();
    val webContext = new JEEContext(request, response);
    val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
    request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
    LOGGER.debug("Initializing action with request parameters [{}]", webContext.getRequestParameters());
    val event = delegatedAuthenticationAction.execute(requestContext);
    assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, event.getId());
    delegatedClientAuthenticationWebflowManager.retrieve(requestContext, webContext, client);
    assertEquals("theme", request.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
    assertEquals(locale, request.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
    assertEquals(HttpMethod.POST.name(), request.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
    val urls = (Set<DelegatedClientIdentityProviderConfiguration>) WebUtils.getDelegatedAuthenticationProviderConfigurations(requestContext);
    assertFalse(urls.isEmpty());
    assertSame(4, urls.size());
    urls.stream().map(url -> {
        LOGGER.debug("Redirect URL [{}]", url.getRedirectUrl());
        return UriComponentsBuilder.fromUriString(url.getRedirectUrl()).build();
    }).forEach(uriComponents -> {
        assertEquals(DelegatedClientIdentityProviderConfigurationFactory.ENDPOINT_URL_REDIRECT, uriComponents.getPath());
        val clientName = uriComponents.getQueryParams().get(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER);
        assertEquals(1, clientName.size());
        val serviceName = uriComponents.getQueryParams().get(CasProtocolConstants.PARAMETER_SERVICE);
        if (service != null) {
            assertEquals(1, serviceName.size());
            assertTrue(serviceName.contains(EncodingUtils.urlEncode(RegisteredServiceTestUtils.CONST_TEST_URL)));
        } else {
            assertNull(serviceName);
        }
        val method = uriComponents.getQueryParams().get(CasProtocolConstants.PARAMETER_METHOD);
        assertEquals(1, method.size());
        assertTrue(method.contains(HttpMethod.POST.toString()));
        val theme = uriComponents.getQueryParams().get(ThemeChangeInterceptor.DEFAULT_PARAM_NAME);
        assertEquals(1, theme.size());
        assertTrue(theme.contains("theme"));
        val testLocale = uriComponents.getQueryParams().get(LocaleChangeInterceptor.DEFAULT_PARAM_NAME);
        assertEquals(1, testLocale.size());
        assertTrue(testLocale.contains(locale));
    });
}
Also used : lombok.val(lombok.val) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) SneakyThrows(lombok.SneakyThrows) ZonedDateTime(java.time.ZonedDateTime) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) DelegatedClientIdentityProviderConfiguration(org.apereo.cas.web.DelegatedClientIdentityProviderConfiguration) Autowired(org.springframework.beans.factory.annotation.Autowired) Pac4jConstants(org.pac4j.core.util.Pac4jConstants) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Clients(org.pac4j.core.client.Clients) Locale(java.util.Locale) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Map(java.util.Map) RegisteredServiceTestUtils(org.apereo.cas.services.RegisteredServiceTestUtils) MockFlowSession(org.springframework.webflow.test.MockFlowSession) ClientCredential(org.apereo.cas.authentication.principal.ClientCredential) Tag(org.junit.jupiter.api.Tag) ZoneOffset(java.time.ZoneOffset) DelegatedClientIdentityProviderConfigurationFactory(org.apereo.cas.web.DelegatedClientIdentityProviderConfigurationFactory) CasProtocolConstants(org.apereo.cas.CasProtocolConstants) FlowVariable(org.springframework.webflow.engine.FlowVariable) DefaultDelegatedClientAuthenticationWebflowManager(org.apereo.cas.web.DefaultDelegatedClientAuthenticationWebflowManager) DefaultRegisteredServiceAccessStrategy(org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy) AllAuthenticationHandlersRegisteredServiceAuthenticationPolicyCriteria(org.apereo.cas.services.AllAuthenticationHandlersRegisteredServiceAuthenticationPolicyCriteria) Set(java.util.Set) BeanFactoryVariableValueFactory(org.springframework.webflow.engine.support.BeanFactoryVariableValueFactory) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UUID(java.util.UUID) Action(org.springframework.webflow.execution.Action) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) BaseDelegatedAuthenticationTests(org.apereo.cas.web.BaseDelegatedAuthenticationTests) Slf4j(lombok.extern.slf4j.Slf4j) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) List(java.util.List) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) EncodingUtils(org.apereo.cas.util.EncodingUtils) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) WebUtils(org.apereo.cas.web.support.WebUtils) MockServletContext(org.apereo.cas.util.MockServletContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) DefaultRegisteredServiceAuthenticationPolicy(org.apereo.cas.services.DefaultRegisteredServiceAuthenticationPolicy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JEEContext(org.pac4j.core.context.JEEContext) ServicesManager(org.apereo.cas.services.ServicesManager) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) InvalidTicketException(org.apereo.cas.ticket.InvalidTicketException) ExternalContextHolder(org.springframework.webflow.context.ExternalContextHolder) lombok.val(lombok.val) HttpMethod(org.springframework.http.HttpMethod) Flow(org.springframework.webflow.engine.Flow) HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) Service(org.apereo.cas.authentication.principal.Service) RequestContextHolder(org.springframework.webflow.execution.RequestContextHolder) Assertions(org.junit.jupiter.api.Assertions) CoreAuthenticationTestUtils(org.apereo.cas.authentication.CoreAuthenticationTestUtils) Set(java.util.Set) MockFlowSession(org.springframework.webflow.test.MockFlowSession) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) BeanFactoryVariableValueFactory(org.springframework.webflow.engine.support.BeanFactoryVariableValueFactory) MockServletContext(org.apereo.cas.util.MockServletContext) Flow(org.springframework.webflow.engine.Flow) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) FlowVariable(org.springframework.webflow.engine.FlowVariable) SneakyThrows(lombok.SneakyThrows)

Aggregations

lombok.val (lombok.val)3 DelegatedClientIdentityProviderConfiguration (org.apereo.cas.web.DelegatedClientIdentityProviderConfiguration)3 List (java.util.List)2 Set (java.util.Set)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Service (org.apereo.cas.authentication.principal.Service)2 DelegatedClientIdentityProviderConfigurationFactory (org.apereo.cas.web.DelegatedClientIdentityProviderConfigurationFactory)2 WebUtils (org.apereo.cas.web.support.WebUtils)2 Clients (org.pac4j.core.client.Clients)2 JEEContext (org.pac4j.core.context.JEEContext)2 StandardCharsets (java.nio.charset.StandardCharsets)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1