Search in sources :

Example 21 with SAML2Client

use of org.pac4j.saml.client.SAML2Client in project cas by apereo.

the class DefaultDelegatedClientAuthenticationWebflowManagerTests method verifyExpiredTicketOperation.

@Test
public void verifyExpiredTicketOperation() throws Exception {
    val config = new SAML2Configuration();
    val client = new SAML2Client(config);
    val ticket = delegatedClientAuthenticationWebflowManager.store(context, client);
    assertNotNull(ticketRegistry.getTicket(ticket.getId()));
    assertEquals(ticket.getId(), delegatedClientDistributedSessionStore.get(context, SAML2StateGenerator.SAML_RELAY_STATE_ATTRIBUTE).get());
    httpServletRequest.addParameter("RelayState", ticket.getId());
    ticket.markTicketExpired();
    assertThrows(UnauthorizedServiceException.class, () -> delegatedClientAuthenticationWebflowManager.retrieve(requestContext, context, client));
}
Also used : lombok.val(lombok.val) SAML2Configuration(org.pac4j.saml.config.SAML2Configuration) SAML2Client(org.pac4j.saml.client.SAML2Client) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with SAML2Client

use of org.pac4j.saml.client.SAML2Client in project cas by apereo.

the class DefaultDelegatedClientAuthenticationWebflowManagerTests method setupTestContextFor.

private Pair<SAML2Client, SAML2MessageContext> setupTestContextFor(final String spMetadataPath, final String spEntityId) throws Exception {
    val idpMetadata = new File("src/test/resources/idp-metadata.xml").getCanonicalPath();
    val keystorePath = new File(FileUtils.getTempDirectory(), "keystore").getCanonicalPath();
    val saml2ClientConfiguration = new SAML2Configuration(keystorePath, "changeit", "changeit", idpMetadata);
    saml2ClientConfiguration.setServiceProviderEntityId(spEntityId);
    saml2ClientConfiguration.setServiceProviderMetadataPath(spMetadataPath);
    saml2ClientConfiguration.setForceKeystoreGeneration(true);
    saml2ClientConfiguration.setForceServiceProviderMetadataGeneration(true);
    saml2ClientConfiguration.init();
    val saml2Client = new SAML2Client(saml2ClientConfiguration);
    saml2Client.setCallbackUrl("http://callback.example.org");
    saml2Client.init();
    val saml2MessageContext = new SAML2MessageContext();
    saml2MessageContext.setSaml2Configuration(saml2ClientConfiguration);
    saml2MessageContext.setWebContext(context);
    val peer = saml2MessageContext.getMessageContext().getSubcontext(SAMLPeerEntityContext.class, true);
    assertNotNull(peer);
    peer.setEntityId("https://cas.example.org/idp");
    val md = peer.getSubcontext(SAMLMetadataContext.class, true);
    assertNotNull(md);
    val roleDescriptorResolver = new PredicateRoleDescriptorResolver(saml2Client.getIdpMetadataResolver().resolve());
    roleDescriptorResolver.initialize();
    md.setRoleDescriptor(roleDescriptorResolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(Objects.requireNonNull(peer.getEntityId())), new EntityRoleCriterion(IDPSSODescriptor.DEFAULT_ELEMENT_NAME))));
    val self = saml2MessageContext.getMessageContext().getSubcontext(SAMLSelfEntityContext.class, true);
    assertNotNull(self);
    self.setEntityId(saml2ClientConfiguration.getServiceProviderEntityId());
    val sp = self.getSubcontext(SAMLMetadataContext.class, true);
    assertNotNull(sp);
    val spResolver = new PredicateRoleDescriptorResolver(saml2Client.getSpMetadataResolver().resolve());
    spResolver.initialize();
    sp.setRoleDescriptor(spResolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(Objects.requireNonNull(self.getEntityId())), new EntityRoleCriterion(SPSSODescriptor.DEFAULT_ELEMENT_NAME))));
    return Pair.of(saml2Client, saml2MessageContext);
}
Also used : lombok.val(lombok.val) SAML2MessageContext(org.pac4j.saml.context.SAML2MessageContext) SAML2Configuration(org.pac4j.saml.config.SAML2Configuration) CriteriaSet(net.shibboleth.utilities.java.support.resolver.CriteriaSet) EntityIdCriterion(org.opensaml.core.criterion.EntityIdCriterion) EntityRoleCriterion(org.opensaml.saml.criterion.EntityRoleCriterion) SAML2Client(org.pac4j.saml.client.SAML2Client) PredicateRoleDescriptorResolver(org.opensaml.saml.metadata.resolver.impl.PredicateRoleDescriptorResolver) File(java.io.File)

Example 23 with SAML2Client

use of org.pac4j.saml.client.SAML2Client in project cas by apereo.

the class DelegatedAuthenticationClientFinishLogoutActionTests method verifyOperationFailsWithError.

@Test
public void verifyOperationFailsWithError() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.addParameter(SamlProtocolConstants.PARAMETER_SAML_RELAY_STATE, "SAML2Client");
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val samlClient = (SAML2Client) builtClients.findClient("SAML2Client").get();
    val handler = mock(SAML2ProfileHandler.class);
    when(handler.receive(any())).thenThrow(new FoundAction("https://google.com"));
    samlClient.setLogoutProfileHandler(handler);
    val result = delegatedAuthenticationClientFinishLogoutAction.execute(context);
    assertNull(result);
    assertEquals(HttpStatus.FOUND.value(), response.getStatus());
    assertEquals("https://google.com", response.getHeader("Location"));
}
Also used : lombok.val(lombok.val) FoundAction(org.pac4j.core.exception.http.FoundAction) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) SAML2Client(org.pac4j.saml.client.SAML2Client) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 24 with SAML2Client

use of org.pac4j.saml.client.SAML2Client in project cas by apereo.

the class DelegatedAuthenticationClientFinishLogoutActionTests method verifyOperationWithRedirect.

@Test
public void verifyOperationWithRedirect() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putDelegatedAuthenticationClientName(context, "SAML2Client");
    WebUtils.putLogoutRedirectUrl(context, "https://google.com");
    val result = delegatedAuthenticationClientFinishLogoutAction.execute(context);
    assertNull(result);
    val samlClient = (SAML2Client) builtClients.findClient("SAML2Client").get();
    assertEquals("https://google.com", samlClient.getLogoutValidator().getPostLogoutURL());
    assertNull(WebUtils.getLogoutRedirectUrl(context, String.class));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) SAML2Client(org.pac4j.saml.client.SAML2Client) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

SAML2Client (org.pac4j.saml.client.SAML2Client)24 lombok.val (lombok.val)16 Test (org.junit.jupiter.api.Test)6 CasClient (org.pac4j.cas.client.CasClient)6 OidcClient (org.pac4j.oidc.client.OidcClient)6 SAML2Configuration (org.pac4j.saml.config.SAML2Configuration)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 SAML2ClientConfiguration (org.pac4j.saml.client.SAML2ClientConfiguration)5 File (java.io.File)4 Collection (java.util.Collection)4 Set (java.util.Set)4 StringUtils (org.apache.commons.lang3.StringUtils)4 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)4 MockServletContext (org.apereo.cas.util.MockServletContext)4 Clients (org.pac4j.core.client.Clients)4 JEEContext (org.pac4j.core.context.JEEContext)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Verb (com.github.scribejava.core.model.Verb)3 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)3