Search in sources :

Example 6 with StatusResponseType

use of org.opensaml.saml2.core.StatusResponseType in project MaxKey by dromara.

the class WebServicePostEncoder method populateVelocityContext.

@SuppressWarnings("rawtypes")
protected void populateVelocityContext(VelocityContext velocityContext, SAMLMessageContext messageContext) throws MessageEncodingException {
    log.debug("Marshalling and Base64 encoding SAML message");
    if (messageContext.getOutboundSAMLMessage().getDOM() == null) {
        marshallMessage(messageContext.getOutboundSAMLMessage());
    }
    try {
        String messageXML = XMLHelper.nodeToString(messageContext.getOutboundSAMLMessage().getDOM());
        String encodedMessage = Base64.encodeBytes(messageXML.getBytes("UTF-8"), Base64.DONT_BREAK_LINES);
        if (messageContext.getOutboundSAMLMessage() instanceof RequestAbstractType) {
            velocityContext.put("SAMLRequest", encodedMessage);
        } else if (messageContext.getOutboundSAMLMessage() instanceof StatusResponseType) {
            velocityContext.put("SAMLResponse", encodedMessage);
        } else {
            throw new MessageEncodingException("SAML message is neither a SAML RequestAbstractType or StatusResponseType");
        }
    } catch (UnsupportedEncodingException e) {
        log.error("UTF-8 encoding is not supported, this VM is not Java compliant.");
        throw new MessageEncodingException("Unable to encode message, UTF-8 encoding is not supported");
    }
    Credential signingCredential = messageContext.getOuboundSAMLMessageSigningCredential();
    if (signingCredential == null) {
        log.debug("No signing credential was supplied, skipping HTTP-Post simple signing");
        return;
    }
    String sigAlgURI = getSignatureAlgorithmURI(signingCredential, null);
    velocityContext.put("SigAlg", sigAlgURI);
    String formControlData = buildFormDataToSign(velocityContext, messageContext, sigAlgURI);
    velocityContext.put("Signature", generateSignature(signingCredential, sigAlgURI, formControlData));
    KeyInfoGenerator kiGenerator = SecurityHelper.getKeyInfoGenerator(signingCredential, null, null);
    if (kiGenerator != null) {
        String kiBase64 = buildKeyInfo(signingCredential, kiGenerator);
        if (!DatatypeHelper.isEmpty(kiBase64)) {
            velocityContext.put("KeyInfo", kiBase64);
        }
    }
}
Also used : KeyInfoGenerator(org.opensaml.xml.security.keyinfo.KeyInfoGenerator) Credential(org.opensaml.xml.security.credential.Credential) RequestAbstractType(org.opensaml.saml2.core.RequestAbstractType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) StatusResponseType(org.opensaml.saml2.core.StatusResponseType)

Aggregations

SAMLObject (org.opensaml.saml.common.SAMLObject)5 RequestAbstractType (org.opensaml.saml.saml2.core.RequestAbstractType)5 StatusResponseType (org.opensaml.saml.saml2.core.StatusResponseType)5 MessageEncodingException (org.opensaml.messaging.encoder.MessageEncodingException)4 MalformedURLException (java.net.MalformedURLException)2 Pair (net.shibboleth.utilities.java.support.collection.Pair)2 URLBuilder (net.shibboleth.utilities.java.support.net.URLBuilder)2 SignableSAMLObject (org.opensaml.saml.common.SignableSAMLObject)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ComponentInitializationException (net.shibboleth.utilities.java.support.component.ComponentInitializationException)1 MarshallingException (org.opensaml.core.xml.io.MarshallingException)1 BindingException (org.opensaml.saml.common.binding.BindingException)1 Issuer (org.opensaml.saml.saml2.core.Issuer)1 RequestAbstractType (org.opensaml.saml2.core.RequestAbstractType)1 StatusResponseType (org.opensaml.saml2.core.StatusResponseType)1 MessageEncodingException (org.opensaml.ws.message.encoder.MessageEncodingException)1 Credential (org.opensaml.xml.security.credential.Credential)1 KeyInfoGenerator (org.opensaml.xml.security.keyinfo.KeyInfoGenerator)1 SignatureSigningParameters (org.opensaml.xmlsec.SignatureSigningParameters)1 Element (org.w3c.dom.Element)1