Search in sources :

Example 6 with JEEContext

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

the class BaseSamlRegisteredServiceAttributeReleasePolicy method getSamlAuthnRequest.

/**
 * Gets saml authn request.
 *
 * @param applicationContext the application context
 * @return the saml authn request
 */
protected static Optional<AuthnRequest> getSamlAuthnRequest(final ApplicationContext applicationContext) {
    val openSamlConfigBean = applicationContext.getBean(OpenSamlConfigBean.DEFAULT_BEAN_NAME, OpenSamlConfigBean.class);
    val sessionStore = applicationContext.getBean(DistributedJEESessionStore.DEFAULT_BEAN_NAME, SessionStore.class);
    val request = HttpRequestUtils.getHttpServletRequestFromRequestAttributes();
    val response = HttpRequestUtils.getHttpServletResponseFromRequestAttributes();
    val context = new JEEContext(request, response);
    val result = SamlIdPUtils.retrieveSamlRequest(context, sessionStore, openSamlConfigBean, AuthnRequest.class);
    val authnRequest = (AuthnRequest) result.orElseThrow(() -> new IllegalArgumentException("SAML request could not be determined from session store")).getLeft();
    return Optional.of(authnRequest);
}
Also used : lombok.val(lombok.val) AuthnRequest(org.opensaml.saml.saml2.core.AuthnRequest) JEEContext(org.pac4j.jee.context.JEEContext)

Example 7 with JEEContext

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

the class ECPSamlIdPProfileHandlerController method extractBasicAuthenticationCredential.

private Credential extractBasicAuthenticationCredential(final HttpServletRequest request, final HttpServletResponse response) {
    val extractor = new BasicAuthExtractor();
    val webContext = new JEEContext(request, response);
    val credentialsResult = extractor.extract(webContext, configurationContext.getSessionStore());
    if (credentialsResult.isPresent()) {
        val credentials = (UsernamePasswordCredentials) credentialsResult.get();
        LOGGER.debug("Received basic authentication ECP request from credentials [{}]", credentials);
        return new UsernamePasswordCredential(credentials.getUsername(), credentials.getPassword());
    }
    return null;
}
Also used : lombok.val(lombok.val) BasicAuthExtractor(org.pac4j.core.credentials.extractor.BasicAuthExtractor) JEEContext(org.pac4j.jee.context.JEEContext) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 8 with JEEContext

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

the class SSOSamlIdPProfileCallbackHandlerController method handleCallbackProfileRequestPost.

/**
 * Handle callback profile request post.
 *
 * @param response the response
 * @param request  the request
 * @return the model and view
 * @throws Exception the exception
 */
@PostMapping(path = SamlIdPConstants.ENDPOINT_SAML2_SSO_PROFILE_CALLBACK)
protected ModelAndView handleCallbackProfileRequestPost(final HttpServletResponse response, final HttpServletRequest request) throws Exception {
    autoConfigureCookiePath(request);
    val properties = configurationContext.getCasProperties();
    val type = properties.getAuthn().getSamlIdp().getCore().getSessionStorageType();
    if (type == SamlIdPCoreProperties.SessionStorageTypes.BROWSER_SESSION_STORAGE) {
        val storage = request.getParameter(BrowserSessionStorage.KEY_SESSION_STORAGE);
        val context = new JEEContext(request, response);
        configurationContext.getSessionStore().buildFromTrackableSession(context, storage);
        return handleProfileRequest(response, request);
    }
    return WebUtils.produceErrorView(new IllegalArgumentException("Unable to build SAML response"));
}
Also used : lombok.val(lombok.val) JEEContext(org.pac4j.jee.context.JEEContext) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 9 with JEEContext

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

the class AuthnRequestRequestedAttributesAttributeReleasePolicyTests method verifyAuthnRequestWithExtensionsNotAllowed.

@Test
public void verifyAuthnRequestWithExtensionsNotAllowed() throws IOException {
    val filter = new AuthnRequestRequestedAttributesAttributeReleasePolicy();
    filter.setAllowedAttributes(List.of("eduPersonPrincipalAttribute"));
    filter.setUseFriendlyName(false);
    val registeredService = SamlIdPTestUtils.getSamlRegisteredService();
    registeredService.setAttributeReleasePolicy(filter);
    val builder = new SAML2AuthnRequestBuilder();
    val authnRequest = builder.build(saml2MessageContext);
    val extensions = ((SAMLObjectBuilder<Extensions>) openSamlConfigBean.getBuilderFactory().getBuilder(Extensions.DEFAULT_ELEMENT_NAME)).buildObject();
    val attrBuilder = (SAMLObjectBuilder<RequestedAttribute>) openSamlConfigBean.getBuilderFactory().getBuilder(RequestedAttribute.DEFAULT_ELEMENT_NAME);
    val requestAttribute = attrBuilder.buildObject(RequestedAttribute.DEFAULT_ELEMENT_NAME);
    requestAttribute.setIsRequired(false);
    requestAttribute.setName("givenName");
    extensions.getUnknownXMLObjects().add(requestAttribute);
    authnRequest.setExtensions(extensions);
    try (val writer = SamlUtils.transformSamlObject(openSamlConfigBean, authnRequest)) {
        val samlRequest = EncodingUtils.encodeBase64(writer.toString().getBytes(StandardCharsets.UTF_8));
        val request = HttpRequestUtils.getHttpServletRequestFromRequestAttributes();
        val response = HttpRequestUtils.getHttpServletResponseFromRequestAttributes();
        val context = new JEEContext(request, response);
        samlIdPDistributedSessionStore.set(context, SamlProtocolConstants.PARAMETER_SAML_REQUEST, samlRequest);
        val messageContext = new MessageContext();
        messageContext.setMessage(authnRequest);
        samlIdPDistributedSessionStore.set(context, MessageContext.class.getName(), SamlIdPAuthenticationContext.from(messageContext).encode());
        val releasePolicyContext = RegisteredServiceAttributeReleasePolicyContext.builder().registeredService(registeredService).service(CoreAuthenticationTestUtils.getService()).principal(getPrincipal("casuser", CollectionUtils.wrap("eduPersonPrincipalName", "casuser", "givenName", "CAS"))).build();
        val attributes = filter.getAttributes(releasePolicyContext);
        assertTrue(attributes.isEmpty());
    }
}
Also used : lombok.val(lombok.val) SAMLObjectBuilder(org.opensaml.saml.common.SAMLObjectBuilder) JEEContext(org.pac4j.jee.context.JEEContext) MessageContext(org.opensaml.messaging.context.MessageContext) SAML2MessageContext(org.pac4j.saml.context.SAML2MessageContext) SAML2AuthnRequestBuilder(org.pac4j.saml.sso.impl.SAML2AuthnRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 10 with JEEContext

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

the class AuthnRequestRequestedAttributesAttributeReleasePolicyTests method verifyAuthnRequestWithExtensionsAllowed.

@Test
public void verifyAuthnRequestWithExtensionsAllowed() throws IOException {
    val filter = new AuthnRequestRequestedAttributesAttributeReleasePolicy();
    filter.setAllowedAttributes(List.of("eduPersonPrincipalName"));
    filter.setUseFriendlyName(false);
    val registeredService = SamlIdPTestUtils.getSamlRegisteredService();
    registeredService.setAttributeReleasePolicy(filter);
    val builder = new SAML2AuthnRequestBuilder();
    val authnRequest = builder.build(saml2MessageContext);
    val extensions = ((SAMLObjectBuilder<Extensions>) openSamlConfigBean.getBuilderFactory().getBuilder(Extensions.DEFAULT_ELEMENT_NAME)).buildObject();
    val attrBuilder = (SAMLObjectBuilder<RequestedAttribute>) openSamlConfigBean.getBuilderFactory().getBuilder(RequestedAttribute.DEFAULT_ELEMENT_NAME);
    val requestAttribute = attrBuilder.buildObject(RequestedAttribute.DEFAULT_ELEMENT_NAME);
    requestAttribute.setIsRequired(false);
    requestAttribute.setName("eduPersonPrincipalName");
    extensions.getUnknownXMLObjects().add(requestAttribute);
    authnRequest.setExtensions(extensions);
    try (val writer = SamlUtils.transformSamlObject(openSamlConfigBean, authnRequest)) {
        val samlRequest = EncodingUtils.encodeBase64(writer.toString().getBytes(StandardCharsets.UTF_8));
        val request = HttpRequestUtils.getHttpServletRequestFromRequestAttributes();
        val response = HttpRequestUtils.getHttpServletResponseFromRequestAttributes();
        val context = new JEEContext(request, response);
        samlIdPDistributedSessionStore.set(context, SamlProtocolConstants.PARAMETER_SAML_REQUEST, samlRequest);
        val messageContext = new MessageContext();
        messageContext.setMessage(authnRequest);
        samlIdPDistributedSessionStore.set(context, MessageContext.class.getName(), SamlIdPAuthenticationContext.from(messageContext).encode());
        val releasePolicyContext = RegisteredServiceAttributeReleasePolicyContext.builder().registeredService(registeredService).service(CoreAuthenticationTestUtils.getService()).principal(getPrincipal("casuser", CollectionUtils.wrap("eduPersonPrincipalName", "casuser", "givenName", "CAS"))).build();
        val attributes = filter.getAttributes(releasePolicyContext);
        assertTrue(attributes.containsKey("eduPersonPrincipalName"));
        val definitions = filter.determineRequestedAttributeDefinitions(releasePolicyContext);
        assertTrue(definitions.contains("eduPersonPrincipalName"));
    }
}
Also used : lombok.val(lombok.val) SAMLObjectBuilder(org.opensaml.saml.common.SAMLObjectBuilder) JEEContext(org.pac4j.jee.context.JEEContext) MessageContext(org.opensaml.messaging.context.MessageContext) SAML2MessageContext(org.pac4j.saml.context.SAML2MessageContext) SAML2AuthnRequestBuilder(org.pac4j.saml.sso.impl.SAML2AuthnRequestBuilder) 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