use of org.opensaml.messaging.context.MessageContext in project cas by apereo.
the class SamlProfileSamlSoap11ResponseBuilder method encode.
@Override
protected Envelope encode(final SamlRegisteredService service, final Envelope envelope, final HttpServletResponse httpResponse, final SamlRegisteredServiceServiceProviderMetadataFacade adaptor, final String relayState) throws SamlException {
try {
final MessageContext result = new MessageContext();
final SOAP11Context ctx = result.getSubcontext(SOAP11Context.class, true);
ctx.setEnvelope(envelope);
final HTTPSOAP11Encoder encoder = new HTTPSOAP11Encoder();
encoder.setHttpServletResponse(httpResponse);
encoder.setMessageContext(result);
encoder.initialize();
encoder.encode();
} catch (final Exception e) {
throw Throwables.propagate(e);
}
return envelope;
}
use of org.opensaml.messaging.context.MessageContext in project cas by apereo.
the class BaseSamlObjectSigner method encode.
/**
* Encode a given saml object by invoking a number of outbound security handlers on the context.
*
* @param <T> the type parameter
* @param samlObject the saml object
* @param service the service
* @param adaptor the adaptor
* @param response the response
* @param request the request
* @return the t
* @throws SamlException the saml exception
*/
public <T extends SAMLObject> T encode(final T samlObject, final SamlRegisteredService service, final SamlRegisteredServiceServiceProviderMetadataFacade adaptor, final HttpServletResponse response, final HttpServletRequest request) throws SamlException {
try {
LOGGER.debug("Attempting to encode [{}] for [{}]", samlObject.getClass().getName(), adaptor.getEntityId());
final MessageContext<T> outboundContext = new MessageContext<>();
prepareOutboundContext(samlObject, adaptor, outboundContext);
prepareSecurityParametersContext(adaptor, outboundContext);
prepareEndpointURLSchemeSecurityHandler(outboundContext);
prepareSamlOutboundDestinationHandler(outboundContext);
prepareSamlOutboundProtocolMessageSigningHandler(outboundContext);
return samlObject;
} catch (final Exception e) {
throw new SamlException(e.getMessage(), e);
}
}
use of org.opensaml.messaging.context.MessageContext in project cas by apereo.
the class AbstractSamlProfileHandlerController method decodeSamlContextFromHttpRequest.
/**
* Decode authentication request saml object.
*
* @param request the request
* @param decoder the decoder
* @param clazz the clazz
* @return the saml object
*/
protected Pair<? extends SignableSAMLObject, MessageContext> decodeSamlContextFromHttpRequest(final HttpServletRequest request, final BaseHttpServletRequestXMLMessageDecoder decoder, final Class<? extends SignableSAMLObject> clazz) {
LOGGER.info("Received SAML profile request [{}]", request.getRequestURI());
try {
decoder.setHttpServletRequest(request);
decoder.setParserPool(this.parserPool);
decoder.initialize();
decoder.decode();
final MessageContext messageContext = decoder.getMessageContext();
final SignableSAMLObject object = (SignableSAMLObject) messageContext.getMessage();
if (object == null) {
throw new SAMLException("No " + clazz.getName() + " could be found in this request context. Decoder has failed.");
}
if (!clazz.isAssignableFrom(object.getClass())) {
throw new ClassCastException("SAML object [" + object.getClass().getName() + " type does not match " + clazz);
}
LOGGER.debug("Decoded SAML object [{}] from http request", object.getElementQName());
return Pair.of(object, messageContext);
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
use of org.opensaml.messaging.context.MessageContext in project cas by apereo.
the class AbstractSamlProfileHandlerController method constructServiceUrl.
/**
* Construct service url string.
*
* @param request the request
* @param response the response
* @param pair the pair
* @return the string
* @throws SamlException the saml exception
*/
protected String constructServiceUrl(final HttpServletRequest request, final HttpServletResponse response, final Pair<? extends SignableSAMLObject, MessageContext> pair) throws SamlException {
final AuthnRequest authnRequest = AuthnRequest.class.cast(pair.getLeft());
final MessageContext messageContext = pair.getRight();
try (StringWriter writer = SamlUtils.transformSamlObject(this.configBean, authnRequest)) {
final URLBuilder builder = new URLBuilder(this.callbackService.getId());
builder.getQueryParams().add(new net.shibboleth.utilities.java.support.collection.Pair<>(SamlProtocolConstants.PARAMETER_ENTITY_ID, SamlIdPUtils.getIssuerFromSamlRequest(authnRequest)));
final String samlRequest = EncodingUtils.encodeBase64(writer.toString().getBytes(StandardCharsets.UTF_8));
builder.getQueryParams().add(new net.shibboleth.utilities.java.support.collection.Pair<>(SamlProtocolConstants.PARAMETER_SAML_REQUEST, samlRequest));
builder.getQueryParams().add(new net.shibboleth.utilities.java.support.collection.Pair<>(SamlProtocolConstants.PARAMETER_SAML_RELAY_STATE, SAMLBindingSupport.getRelayState(messageContext)));
final String url = builder.buildURL();
LOGGER.debug("Built service callback url [{}]", url);
return CommonUtils.constructServiceUrl(request, response, url, this.serverName, CasProtocolConstants.PARAMETER_SERVICE, CasProtocolConstants.PARAMETER_TICKET, false);
} catch (final Exception e) {
throw new SamlException(e.getMessage(), e);
}
}
use of org.opensaml.messaging.context.MessageContext in project cas by apereo.
the class ECPProfileHandlerController method handleEcpRequest.
/**
* Handle ecp request.
*
* @param response the response
* @param request the request
* @param soapContext the soap context
* @param credential the credential
*/
protected void handleEcpRequest(final HttpServletResponse response, final HttpServletRequest request, final MessageContext soapContext, final Credential credential) {
final Envelope envelope = soapContext.getSubcontext(SOAP11Context.class).getEnvelope();
SamlUtils.logSamlObject(configBean, envelope);
final AuthnRequest authnRequest = (AuthnRequest) soapContext.getMessage();
final Pair<AuthnRequest, MessageContext> authenticationContext = Pair.of(authnRequest, soapContext);
try {
final Pair<SamlRegisteredService, SamlRegisteredServiceServiceProviderMetadataFacade> serviceRequest = verifySamlAuthenticationRequest(authenticationContext, request);
final Authentication authentication = authenticateEcpRequest(credential, authenticationContext);
buildSamlResponse(response, request, authenticationContext, buildEcpCasAssertion(authentication, serviceRequest.getKey()));
} catch (final AuthenticationException e) {
LOGGER.error(e.getMessage(), e);
final String error = e.getHandlerErrors().values().stream().map(Class::getSimpleName).collect(Collectors.joining(","));
buildEcpFaultResponse(response, request, Pair.of(authnRequest, error));
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
buildEcpFaultResponse(response, request, Pair.of(authnRequest, e.getMessage()));
}
}
Aggregations