use of org.pac4j.saml.client.SAML2Client in project cas by apereo.
the class SamlObjectSignatureValidatorTests method setupTestContextFor.
private void setupTestContextFor(final String spMetadataPath, final String spEntityId) throws Exception {
val idpMetadata = new File("src/test/resources/metadata/idp-metadata.xml").getCanonicalPath();
val keystorePath = new File(FileUtils.getTempDirectory(), "keystore").getCanonicalPath();
saml2ClientConfiguration = new SAML2Configuration(keystorePath, "changeit", "changeit", idpMetadata);
saml2ClientConfiguration.setServiceProviderEntityId(spEntityId);
saml2ClientConfiguration.setServiceProviderMetadataPath(spMetadataPath);
saml2ClientConfiguration.init();
val saml2Client = new SAML2Client(saml2ClientConfiguration);
saml2Client.setCallbackUrl("http://callback.example.org");
saml2Client.init();
samlContext = new MessageContext();
saml2MessageContext = new SAML2MessageContext();
saml2MessageContext.setSaml2Configuration(saml2ClientConfiguration);
saml2MessageContext.setWebContext(new JEEContext(new MockHttpServletRequest(), new MockHttpServletResponse()));
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 idpResolver = SamlIdPUtils.getRoleDescriptorResolver(casSamlIdPMetadataResolver, true);
md.setRoleDescriptor(idpResolver.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 spRes = new InMemoryResourceMetadataResolver(saml2ClientConfiguration.getServiceProviderMetadataResource(), openSamlConfigBean);
spRes.setId(getClass().getSimpleName());
spRes.initialize();
val spResolver = SamlIdPUtils.getRoleDescriptorResolver(spRes, true);
sp.setRoleDescriptor(spResolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(Objects.requireNonNull(self.getEntityId())), new EntityRoleCriterion(SPSSODescriptor.DEFAULT_ELEMENT_NAME))));
val service = new SamlRegisteredService();
service.setName("Sample");
service.setServiceId(saml2ClientConfiguration.getServiceProviderEntityId());
service.setId(100);
service.setDescription("SAML Service");
service.setMetadataLocation(spMetadataPath);
val facade = SamlRegisteredServiceServiceProviderMetadataFacade.get(samlRegisteredServiceCachingMetadataResolver, service, service.getServiceId());
this.adaptor = facade.get();
}
use of org.pac4j.saml.client.SAML2Client in project cas by apereo.
the class AuthnRequestRequestedAttributesAttributeReleasePolicyTests method initialize.
@BeforeEach
public void initialize() throws Exception {
val idpMetadata = new File("src/test/resources/metadata/idp-metadata.xml").getCanonicalPath();
val keystorePath = new File(FileUtils.getTempDirectory(), "keystore").getCanonicalPath();
val spMetadataPath = new File(FileUtils.getTempDirectory(), "sp-metadata.xml").getCanonicalPath();
saml2Configuration = new SAML2Configuration(keystorePath, "changeit", "changeit", idpMetadata);
saml2Configuration.setServiceProviderEntityId("cas:example:sp");
saml2Configuration.setServiceProviderMetadataPath(spMetadataPath);
saml2Configuration.init();
val saml2Client = new SAML2Client(saml2Configuration);
saml2Client.setCallbackUrl("http://callback.example.org");
saml2Client.init();
saml2MessageContext = new SAML2MessageContext();
saml2MessageContext.setSaml2Configuration(saml2Configuration);
saml2MessageContext.setWebContext(new JEEContext(new MockHttpServletRequest(), new MockHttpServletResponse()));
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 idpResolver = SamlIdPUtils.getRoleDescriptorResolver(casSamlIdPMetadataResolver, true);
md.setRoleDescriptor(idpResolver.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(saml2Configuration.getServiceProviderEntityId());
val sp = self.getSubcontext(SAMLMetadataContext.class, true);
assertNotNull(sp);
val spRes = new InMemoryResourceMetadataResolver(new File(spMetadataPath), openSamlConfigBean);
spRes.setId(getClass().getSimpleName());
spRes.initialize();
val spResolver = SamlIdPUtils.getRoleDescriptorResolver(spRes, true);
sp.setRoleDescriptor(spResolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(Objects.requireNonNull(self.getEntityId())), new EntityRoleCriterion(SPSSODescriptor.DEFAULT_ELEMENT_NAME))));
}
use of org.pac4j.saml.client.SAML2Client in project cas by apereo.
the class DelegatedAuthenticationClientFinishLogoutActionTests method verifyOperationWithRelay.
@Test
public void verifyOperationWithRelay() 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 IllegalArgumentException());
samlClient.setLogoutProfileHandler(handler);
val result = delegatedAuthenticationClientFinishLogoutAction.execute(context);
assertNull(result);
}
use of org.pac4j.saml.client.SAML2Client in project cas by apereo.
the class DelegatedAuthenticationClientFinishLogoutActionTests method verifyOperationNoLogoutRedirectUrl.
@Test
public void verifyOperationNoLogoutRedirectUrl() 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");
val samlClient = (SAML2Client) builtClients.findClient("SAML2Client").get();
samlClient.getLogoutValidator().setPostLogoutURL("https://google.com");
val result = delegatedAuthenticationClientFinishLogoutAction.execute(context);
assertNull(result);
assertEquals("https://google.com", samlClient.getLogoutValidator().getPostLogoutURL());
assertNull(WebUtils.getLogoutRedirectUrl(context, String.class));
}
use of org.pac4j.saml.client.SAML2Client in project cas by apereo.
the class SAML2ClientLogoutAction method doExecute.
@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
try {
final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
final HttpServletResponse response = WebUtils.getHttpServletResponse(requestContext);
final J2EContext context = WebUtils.getPac4jJ2EContext(request, response);
final SAML2Client client = clients.findClient(SAML2Client.class);
if (client != null) {
LOGGER.debug("Located SAML2 client [{}]", client);
final RedirectAction action = client.getLogoutAction(context, null, null);
LOGGER.debug("Preparing logout message to send is [{}]", action.getLocation());
action.perform(context);
}
} catch (final Exception e) {
LOGGER.warn(e.getMessage(), e);
}
return null;
}
Aggregations