Search in sources :

Example 1 with DynamicHtmlView

use of org.apereo.cas.web.view.DynamicHtmlView in project cas by apereo.

the class BaseDelegatedAuthenticationController method getResultingView.

/**
 * Gets resulting view.
 *
 * @param client     the client
 * @param webContext the web context
 * @param ticket     the ticket
 * @return the resulting view
 * @throws Exception the exception
 */
protected View getResultingView(final IndirectClient client, final WebContext webContext, final TransientSessionTicket ticket) throws Exception {
    client.init();
    val actionResult = getRedirectionAction(client, webContext, ticket);
    if (actionResult.isPresent()) {
        val action = actionResult.get();
        LOGGER.debug("Determined final redirect action for client [{}] as [{}]", client, action);
        if (action instanceof WithLocationAction) {
            val foundAction = WithLocationAction.class.cast(action);
            val builder = new URIBuilder(foundAction.getLocation());
            val url = builder.toString();
            LOGGER.debug("Redirecting client [{}] to [{}] based on identifier [{}]", client.getName(), url, ticket.getId());
            return new RedirectView(url);
        }
        if (action instanceof WithContentAction) {
            val seeOtherAction = WithContentAction.class.cast(action);
            return new DynamicHtmlView(seeOtherAction.getContent());
        }
    }
    LOGGER.warn("Unable to determine redirect action for client [{}]", client);
    return null;
}
Also used : lombok.val(lombok.val) WithContentAction(org.pac4j.core.exception.http.WithContentAction) RedirectView(org.springframework.web.servlet.view.RedirectView) WithLocationAction(org.pac4j.core.exception.http.WithLocationAction) DynamicHtmlView(org.apereo.cas.web.view.DynamicHtmlView) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 2 with DynamicHtmlView

use of org.apereo.cas.web.view.DynamicHtmlView in project cas by apereo.

the class DefaultDelegatedAuthenticationNavigationControllerTests method verifyRedirectByAttrPassiveAuth.

@Test
public void verifyRedirectByAttrPassiveAuth() {
    val request = new MockHttpServletRequest();
    request.setAttribute(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "SAML2Client");
    request.setParameter(RedirectionActionBuilder.ATTRIBUTE_PASSIVE, "true");
    val response = new MockHttpServletResponse();
    assertTrue(controller.redirectToProvider(request, response) instanceof DynamicHtmlView);
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) DynamicHtmlView(org.apereo.cas.web.view.DynamicHtmlView) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with DynamicHtmlView

use of org.apereo.cas.web.view.DynamicHtmlView in project cas by apereo.

the class DefaultDelegatedAuthenticationNavigationControllerTests method verifyRedirectWithService.

@Test
public void verifyRedirectWithService() {
    val request = new MockHttpServletRequest();
    request.setAttribute(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "SAML2Client");
    val service = RegisteredServiceTestUtils.getService("https://github.com/apereo/cas");
    servicesManager.save(RegisteredServiceTestUtils.getRegisteredService("https://github.com/apereo/cas"));
    request.setParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
    val response = new MockHttpServletResponse();
    assertTrue(controller.redirectToProvider(request, response) instanceof DynamicHtmlView);
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) DynamicHtmlView(org.apereo.cas.web.view.DynamicHtmlView) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with DynamicHtmlView

use of org.apereo.cas.web.view.DynamicHtmlView in project cas by apereo.

the class DelegatedClientNavigationController method redirectToProvider.

/**
 * Redirect to provider. Receive the client name from the request and then try to determine and build the endpoint url
 * for the redirection. The redirection data/url must contain a delegated client ticket id so that the request be can
 * restored on the trip back. SAML clients use the relay-state session attribute while others use request parameters.
 *
 * @param request  the request
 * @param response the response
 * @return the view
 */
@GetMapping(ENDPOINT_REDIRECT)
public View redirectToProvider(final HttpServletRequest request, final HttpServletResponse response) {
    final String clientName = request.getParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER);
    try {
        final IndirectClient client = (IndirectClient<Credentials, CommonProfile>) this.clients.findClient(clientName);
        final J2EContext webContext = Pac4jUtils.getPac4jJ2EContext(request, response);
        final Ticket ticket = delegatedClientWebflowManager.store(webContext, client);
        final View result;
        final RedirectAction action = client.getRedirectAction(webContext);
        if (RedirectAction.RedirectType.SUCCESS.equals(action.getType())) {
            result = new DynamicHtmlView(action.getContent());
        } else {
            final URIBuilder builder = new URIBuilder(action.getLocation());
            final String url = builder.toString();
            LOGGER.debug("Redirecting client [{}] to [{}] based on identifier [{}]", client.getName(), url, ticket.getId());
            result = new RedirectView(url);
        }
        this.delegatedSessionCookieManager.store(webContext);
        return result;
    } catch (final HttpAction e) {
        if (e.getCode() == HttpStatus.UNAUTHORIZED.value()) {
            LOGGER.debug("Authentication request was denied from the provider [{}]", clientName, e);
        } else {
            LOGGER.warn(e.getMessage(), e);
        }
        throw new UnauthorizedServiceException(e.getMessage(), e);
    }
}
Also used : Ticket(org.apereo.cas.ticket.Ticket) RedirectView(org.springframework.web.servlet.view.RedirectView) IndirectClient(org.pac4j.core.client.IndirectClient) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) J2EContext(org.pac4j.core.context.J2EContext) View(org.springframework.web.servlet.View) DynamicHtmlView(org.apereo.cas.web.view.DynamicHtmlView) RedirectView(org.springframework.web.servlet.view.RedirectView) DynamicHtmlView(org.apereo.cas.web.view.DynamicHtmlView) HttpAction(org.pac4j.core.exception.HttpAction) RedirectAction(org.pac4j.core.redirect.RedirectAction) URIBuilder(org.jasig.cas.client.util.URIBuilder) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with DynamicHtmlView

use of org.apereo.cas.web.view.DynamicHtmlView in project cas by apereo.

the class DefaultDelegatedAuthenticationNavigationControllerTests method verifyRedirectWithServiceSaml2Properties.

@Test
public void verifyRedirectWithServiceSaml2Properties() {
    val request = new MockHttpServletRequest();
    request.setAttribute(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "SAML2Client");
    val service = RegisteredServiceTestUtils.getService("https://github.com/apereo/cas");
    servicesManager.save(RegisteredServiceTestUtils.getRegisteredService("https://github.com/apereo/cas"));
    val registeredService = servicesManager.findServiceBy(service);
    val property1 = new DefaultRegisteredServiceProperty("class1", "class2");
    registeredService.getProperties().put(RegisteredServiceProperties.DELEGATED_AUTHN_SAML2_AUTHN_CONTEXT_CLASS_REFS.getPropertyName(), property1);
    val property2 = new DefaultRegisteredServiceProperty("true");
    registeredService.getProperties().put(RegisteredServiceProperties.DELEGATED_AUTHN_SAML2_WANTS_RESPONSES_SIGNED.getPropertyName(), property2);
    servicesManager.save(registeredService);
    request.setParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
    val response = new MockHttpServletResponse();
    assertTrue(controller.redirectToProvider(request, response) instanceof DynamicHtmlView);
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) DynamicHtmlView(org.apereo.cas.web.view.DynamicHtmlView) DefaultRegisteredServiceProperty(org.apereo.cas.services.DefaultRegisteredServiceProperty) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

DynamicHtmlView (org.apereo.cas.web.view.DynamicHtmlView)7 lombok.val (lombok.val)6 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)5 RedirectView (org.springframework.web.servlet.view.RedirectView)2 URIBuilder (org.apache.http.client.utils.URIBuilder)1 DefaultRegisteredServiceProperty (org.apereo.cas.services.DefaultRegisteredServiceProperty)1 UnauthorizedServiceException (org.apereo.cas.services.UnauthorizedServiceException)1 Ticket (org.apereo.cas.ticket.Ticket)1 URIBuilder (org.jasig.cas.client.util.URIBuilder)1 IndirectClient (org.pac4j.core.client.IndirectClient)1 J2EContext (org.pac4j.core.context.J2EContext)1 HttpAction (org.pac4j.core.exception.HttpAction)1 WithContentAction (org.pac4j.core.exception.http.WithContentAction)1 WithLocationAction (org.pac4j.core.exception.http.WithLocationAction)1 RedirectAction (org.pac4j.core.redirect.RedirectAction)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 View (org.springframework.web.servlet.View)1