Search in sources :

Example 16 with JEEContext

use of org.pac4j.jee.context.JEEContext in project cas by apereo.

the class SamlIdPMultifactorAuthenticationTrigger method isActivated.

@Override
public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest request, final HttpServletResponse response, final Service service) {
    val context = new JEEContext(request, response);
    val result = SamlIdPUtils.retrieveSamlRequest(context, distributedSessionStore, openSamlConfigBean, AuthnRequest.class);
    val mappings = getAuthenticationContextMappings();
    return result.map(pair -> (AuthnRequest) pair.getLeft()).flatMap(authnRequest -> authnRequest.getRequestedAuthnContext().getAuthnContextClassRefs().stream().filter(Objects::nonNull).filter(ref -> StringUtils.isNotBlank(ref.getURI())).filter(ref -> {
        val clazz = ref.getURI();
        return mappings.containsKey(clazz);
    }).findFirst().map(mapped -> mappings.get(mapped.getURI()))).flatMap(id -> {
        val providerMap = MultifactorAuthenticationUtils.getAvailableMultifactorAuthenticationProviders(applicationContext);
        return MultifactorAuthenticationUtils.resolveProvider(providerMap, id);
    });
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthnRequest(org.opensaml.saml.saml2.core.AuthnRequest) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) MultifactorAuthenticationTrigger(org.apereo.cas.authentication.MultifactorAuthenticationTrigger) StringUtils(org.apache.commons.lang3.StringUtils) SessionStore(org.pac4j.core.context.session.SessionStore) ApplicationContext(org.springframework.context.ApplicationContext) RegisteredService(org.apereo.cas.services.RegisteredService) SamlIdPUtils(org.apereo.cas.support.saml.SamlIdPUtils) OpenSamlConfigBean(org.apereo.cas.support.saml.OpenSamlConfigBean) HttpRequestUtils(org.apereo.cas.util.HttpRequestUtils) Objects(java.util.Objects) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.apereo.cas.authentication.Authentication) Service(org.apereo.cas.authentication.principal.Service) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) Optional(java.util.Optional) MultifactorAuthenticationUtils(org.apereo.cas.authentication.MultifactorAuthenticationUtils) JEEContext(org.pac4j.jee.context.JEEContext) JEEContext(org.pac4j.jee.context.JEEContext) Objects(java.util.Objects)

Example 17 with JEEContext

use of org.pac4j.jee.context.JEEContext in project cas by apereo.

the class SamlProfileSaml2ResponseBuilderTests method verifySamlResponseWithAttributeQuery.

@Test
public void verifySamlResponseWithAttributeQuery() throws Exception {
    val request = buildHttpRequest();
    val response = new MockHttpServletResponse();
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    val webContext = new JEEContext(request, response);
    samlIdPDistributedSessionStore.set(webContext, WebUtils.PARAMETER_TICKET_GRANTING_TICKET_ID, tgt.getId());
    val service = getSamlRegisteredServiceForTestShib(true, true);
    service.setIssuerEntityId("https://issuer.example.org");
    service.getAttributeValueTypes().put("permissions", XSObject.class.getSimpleName());
    val adaptor = SamlRegisteredServiceServiceProviderMetadataFacade.get(samlRegisteredServiceCachingMetadataResolver, service, service.getServiceId()).get();
    val authnRequest = getAuthnRequestFor(service);
    val assertion = getAssertion();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));
    val samlResponse = buildResponse(request, response, service, adaptor, authnRequest, assertion, SAMLConstants.SAML2_ARTIFACT_BINDING_URI);
    assertNotNull(samlResponse);
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JEEContext(org.pac4j.jee.context.JEEContext) XSObject(org.apache.xerces.xs.XSObject) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 18 with JEEContext

use of org.pac4j.jee.context.JEEContext in project cas by apereo.

the class SSOSamlIdPPostProfileHandlerControllerTests method verifyPostRequestWithUnknownCookie.

@Test
@Order(7)
public void verifyPostRequestWithUnknownCookie() throws Exception {
    val response = new MockHttpServletResponse();
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketGrantingTicketCookieGenerator.addCookie(response, tgt.getId());
    val request = new MockHttpServletRequest();
    request.setCookies(response.getCookies());
    request.setMethod("POST");
    val authnRequest = getAuthnRequest();
    val xml = SamlUtils.transformSamlObject(openSamlConfigBean, authnRequest).toString();
    request.addParameter(SamlProtocolConstants.PARAMETER_SAML_REQUEST, EncodingUtils.encodeBase64(xml));
    samlIdPDistributedSessionStore.set(new JEEContext(request, response), SamlProtocolConstants.PARAMETER_SAML_RELAY_STATE, "relay-state");
    val mv = controller.handleSaml2ProfileSsoPostRequest(response, request);
    assertEquals(HttpStatus.FOUND, mv.getStatus());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.jee.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 19 with JEEContext

use of org.pac4j.jee.context.JEEContext in project cas by apereo.

the class SSOSamlIdPPostProfileHandlerControllerTests method verifyPostRequestWithSso.

@Test
@Order(5)
public void verifyPostRequestWithSso() throws Exception {
    val response = new MockHttpServletResponse();
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    ticketGrantingTicketCookieGenerator.addCookie(response, tgt.getId());
    val request = new MockHttpServletRequest();
    request.setCookies(response.getCookies());
    request.setMethod("POST");
    val authnRequest = getAuthnRequest();
    val xml = SamlUtils.transformSamlObject(openSamlConfigBean, authnRequest).toString();
    request.addParameter(SamlProtocolConstants.PARAMETER_SAML_REQUEST, EncodingUtils.encodeBase64(xml));
    samlIdPDistributedSessionStore.set(new JEEContext(request, response), SamlProtocolConstants.PARAMETER_SAML_RELAY_STATE, "relay-state");
    val mv = controller.handleSaml2ProfileSsoPostRequest(response, request);
    assertNull(mv);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertNotNull(response.getContentAsString());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.jee.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 20 with JEEContext

use of org.pac4j.jee.context.JEEContext in project cas by apereo.

the class SSOSamlIdPProfileCallbackHandlerControllerWithBrowserStorageTests method verifyResumeFromStorage.

@Test
public void verifyResumeFromStorage() throws Exception {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val authn = getAuthnRequest();
    authn.setProtocolBinding(SAMLConstants.SAML2_POST_BINDING_URI);
    val xml = SamlUtils.transformSamlObject(openSamlConfigBean, getAuthnRequest()).toString();
    request.getSession().setAttribute(SamlProtocolConstants.PARAMETER_SAML_REQUEST, EncodingUtils.encodeBase64(xml));
    request.getSession().setAttribute(SamlProtocolConstants.PARAMETER_SAML_RELAY_STATE, UUID.randomUUID().toString());
    val context = new MessageContext();
    context.setMessage(getAuthnRequest());
    request.getSession().setAttribute(MessageContext.class.getName(), SamlIdPAuthenticationContext.from(context).encode());
    request.addParameter(CasProtocolConstants.PARAMETER_TICKET, "ST-1234567890");
    val payload = samlIdPDistributedSessionStore.getTrackableSession(new JEEContext(request, response)).map(BrowserSessionStorage.class::cast).map(BrowserSessionStorage::getPayload).orElseThrow();
    request.addParameter(BrowserSessionStorage.KEY_SESSION_STORAGE, payload);
    val mv = controller.handleCallbackProfileRequestPost(response, request);
    assertNull(mv);
    assertEquals(HttpStatus.SC_OK, response.getStatus());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BrowserSessionStorage(org.apereo.cas.web.BrowserSessionStorage) JEEContext(org.pac4j.jee.context.JEEContext) MessageContext(org.opensaml.messaging.context.MessageContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

JEEContext (org.pac4j.jee.context.JEEContext)227 lombok.val (lombok.val)224 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)166 Test (org.junit.jupiter.api.Test)163 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)161 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)34 ProfileManager (org.pac4j.core.profile.ProfileManager)27 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)24 CommonProfile (org.pac4j.core.profile.CommonProfile)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)16 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)14 MockRequestContext (org.springframework.webflow.test.MockRequestContext)14 HashMap (java.util.HashMap)13 CasProfile (org.pac4j.cas.profile.CasProfile)13 RedirectView (org.springframework.web.servlet.view.RedirectView)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Map (java.util.Map)10 Optional (java.util.Optional)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 MockServletContext (org.apereo.cas.util.MockServletContext)10