Search in sources :

Example 1 with MarshallingException

use of org.opensaml.core.xml.io.MarshallingException in project pac4j by pac4j.

the class SAML2ServiceProviderMetadataResolver method resolve.

@Override
public final MetadataResolver resolve() {
    final boolean credentialProviderRequired = this.authnRequestSigned || this.wantsAssertionsSigned;
    if (credentialProviderRequired && this.credentialProvider == null) {
        throw new TechnicalException("Credentials Provider can not be null when authnRequestSigned or" + " wantsAssertionsSigned is set to true");
    }
    try {
        final SAML2MetadataGenerator metadataGenerator = new SAML2MetadataGenerator(binding);
        metadataGenerator.setWantAssertionSigned(this.wantsAssertionsSigned);
        metadataGenerator.setAuthnRequestSigned(this.authnRequestSigned);
        metadataGenerator.setNameIdPolicyFormat(this.nameIdPolicyFormat);
        if (credentialProviderRequired) {
            metadataGenerator.setCredentialProvider(this.credentialProvider);
        }
        metadataGenerator.setEntityId(this.spEntityId);
        metadataGenerator.setRequestInitiatorLocation(callbackUrl);
        // Assertion consumer service url is the callback url
        metadataGenerator.setAssertionConsumerServiceUrl(callbackUrl);
        // for now same for logout url
        metadataGenerator.setSingleLogoutServiceUrl(callbackUrl);
        final MetadataResolver spMetadataProvider = metadataGenerator.buildMetadataResolver();
        // Initialize metadata provider for our SP and get the XML as a String
        this.spMetadata = metadataGenerator.getMetadata();
        if (this.spMetadataResource != null) {
            if (spMetadataResource.exists() && !this.forceSpMetadataGeneration) {
                logger.info("Metadata file already exists at {}.", this.spMetadataResource.getFilename());
            } else {
                logger.info("Writing sp metadata to {}", this.spMetadataResource.getFilename());
                final File parent = spMetadataResource.getFile().getParentFile();
                if (parent != null) {
                    logger.info("Attempting to create directory structure for: {}", parent.getCanonicalPath());
                    if (!parent.exists() && !parent.mkdirs()) {
                        logger.warn("Could not construct the directory structure for SP metadata: {}", parent.getCanonicalPath());
                    }
                }
                final Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
                final StreamResult result = new StreamResult(new StringWriter());
                final StreamSource source = new StreamSource(new StringReader(this.spMetadata));
                transformer.transform(source, result);
                try (final OutputStream spMetadataOutputStream = this.spMetadataResource.getOutputStream()) {
                    spMetadataOutputStream.write(result.getWriter().toString().getBytes(StandardCharsets.UTF_8));
                }
            }
        }
        return spMetadataProvider;
    } catch (final ComponentInitializationException e) {
        throw new TechnicalException("Error initializing spMetadataProvider", e);
    } catch (final MarshallingException e) {
        logger.warn("Unable to marshal SP metadata", e);
    } catch (final IOException e) {
        logger.warn("Unable to print SP metadata", e);
    } catch (final Exception e) {
        logger.warn("Unable to transform metadata", e);
    }
    return null;
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ComponentInitializationException(net.shibboleth.utilities.java.support.component.ComponentInitializationException) StreamSource(javax.xml.transform.stream.StreamSource) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver) ComponentInitializationException(net.shibboleth.utilities.java.support.component.ComponentInitializationException) TechnicalException(org.pac4j.core.exception.TechnicalException) MarshallingException(org.opensaml.core.xml.io.MarshallingException) ResolverException(net.shibboleth.utilities.java.support.resolver.ResolverException) IOException(java.io.IOException) SAMLException(org.pac4j.saml.exceptions.SAMLException) StringWriter(java.io.StringWriter) MarshallingException(org.opensaml.core.xml.io.MarshallingException) StringReader(java.io.StringReader) File(java.io.File)

Example 2 with MarshallingException

use of org.opensaml.core.xml.io.MarshallingException in project spring-security by spring-projects.

the class OpenSamlAuthenticationProviderTests method serialize.

private String serialize(XMLObject object) {
    try {
        Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(object);
        Element element = marshaller.marshall(object);
        return SerializeSupport.nodeToString(element);
    } catch (MarshallingException ex) {
        throw new Saml2Exception(ex);
    }
}
Also used : Marshaller(org.opensaml.core.xml.io.Marshaller) MarshallingException(org.opensaml.core.xml.io.MarshallingException) Element(org.w3c.dom.Element) Saml2Exception(org.springframework.security.saml2.Saml2Exception)

Example 3 with MarshallingException

use of org.opensaml.core.xml.io.MarshallingException in project spring-security by spring-projects.

the class TestOpenSamlObjects method signed.

static <T extends SignableSAMLObject> T signed(T signable, Saml2X509Credential credential, String entityId, String signAlgorithmUri) {
    SignatureSigningParameters parameters = new SignatureSigningParameters();
    Credential signingCredential = getSigningCredential(credential, entityId);
    parameters.setSigningCredential(signingCredential);
    parameters.setSignatureAlgorithm(signAlgorithmUri);
    parameters.setSignatureReferenceDigestMethod(SignatureConstants.ALGO_ID_DIGEST_SHA256);
    parameters.setSignatureCanonicalizationAlgorithm(SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
    try {
        SignatureSupport.signObject(signable, parameters);
    } catch (MarshallingException | SignatureException | SecurityException ex) {
        throw new Saml2Exception(ex);
    }
    return signable;
}
Also used : BasicCredential(org.opensaml.security.credential.BasicCredential) Saml2X509Credential(org.springframework.security.saml2.core.Saml2X509Credential) Credential(org.opensaml.security.credential.Credential) MarshallingException(org.opensaml.core.xml.io.MarshallingException) SignatureSigningParameters(org.opensaml.xmlsec.SignatureSigningParameters) SecurityException(org.opensaml.security.SecurityException) SignatureException(org.opensaml.xmlsec.signature.support.SignatureException) Saml2Exception(org.springframework.security.saml2.Saml2Exception)

Example 4 with MarshallingException

use of org.opensaml.core.xml.io.MarshallingException in project spring-security by spring-projects.

the class OpenSamlSigningUtils method serialize.

static String serialize(XMLObject object) {
    try {
        Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(object);
        Element element = marshaller.marshall(object);
        return SerializeSupport.nodeToString(element);
    } catch (MarshallingException ex) {
        throw new Saml2Exception(ex);
    }
}
Also used : Marshaller(org.opensaml.core.xml.io.Marshaller) MarshallingException(org.opensaml.core.xml.io.MarshallingException) Element(org.w3c.dom.Element) Saml2Exception(org.springframework.security.saml2.Saml2Exception)

Example 5 with MarshallingException

use of org.opensaml.core.xml.io.MarshallingException in project spring-security by spring-projects.

the class OpenSaml4AuthenticationProviderTests method serialize.

private String serialize(XMLObject object) {
    try {
        Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(object);
        Element element = marshaller.marshall(object);
        return SerializeSupport.nodeToString(element);
    } catch (MarshallingException ex) {
        throw new Saml2Exception(ex);
    }
}
Also used : Marshaller(org.opensaml.core.xml.io.Marshaller) MarshallingException(org.opensaml.core.xml.io.MarshallingException) Element(org.w3c.dom.Element) Saml2Exception(org.springframework.security.saml2.Saml2Exception)

Aggregations

MarshallingException (org.opensaml.core.xml.io.MarshallingException)12 Saml2Exception (org.springframework.security.saml2.Saml2Exception)7 Marshaller (org.opensaml.core.xml.io.Marshaller)6 Element (org.w3c.dom.Element)6 SignatureException (org.opensaml.xmlsec.signature.support.SignatureException)4 IDPSSODescriptor (org.opensaml.saml.saml2.metadata.IDPSSODescriptor)3 KeyDescriptor (org.opensaml.saml.saml2.metadata.KeyDescriptor)3 Signature (org.opensaml.xmlsec.signature.Signature)2 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Transformer (javax.xml.transform.Transformer)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 ComponentInitializationException (net.shibboleth.utilities.java.support.component.ComponentInitializationException)1 ResolverException (net.shibboleth.utilities.java.support.resolver.ResolverException)1 MetadataResolver (org.opensaml.saml.metadata.resolver.MetadataResolver)1 SecurityException (org.opensaml.security.SecurityException)1