Search in sources :

Example 1 with MetadataResolver

use of org.opensaml.saml.metadata.resolver.MetadataResolver in project verify-hub by alphagov.

the class SamlEngineModule method getVerifyMetadataResolver.

@Provides
@Singleton
@Named("VerifyMetadataResolver")
private MetadataResolver getVerifyMetadataResolver(Environment environment, SamlEngineConfiguration configuration) {
    final MetadataResolver metadataResolver = new DropwizardMetadataResolverFactory().createMetadataResolver(environment, configuration.getMetadataConfiguration());
    registerMetadataRefreshTask(environment, metadataResolver, configuration.getMetadataConfiguration(), "metadata");
    return metadataResolver;
}
Also used : DropwizardMetadataResolverFactory(uk.gov.ida.saml.metadata.factories.DropwizardMetadataResolverFactory) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver) AbstractReloadingMetadataResolver(org.opensaml.saml.metadata.resolver.impl.AbstractReloadingMetadataResolver) Named(javax.inject.Named) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 2 with MetadataResolver

use of org.opensaml.saml.metadata.resolver.MetadataResolver in project cas by apereo.

the class SamlRegisteredServiceDefaultCachingMetadataResolver method resolve.

@Override
public MetadataResolver resolve(final SamlRegisteredService service) {
    MetadataResolver resolver = null;
    try {
        LOGGER.debug("Resolving metadata for [{}] at [{}].", service.getName(), service.getMetadataLocation());
        resolver = this.cache.get(service);
        return resolver;
    } finally {
        if (resolver != null) {
            LOGGER.debug("Loaded and cached SAML metadata [{}] from [{}]", resolver.getId(), service.getMetadataLocation());
        }
    }
}
Also used : MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver)

Example 3 with MetadataResolver

use of org.opensaml.saml.metadata.resolver.MetadataResolver in project cas by apereo.

the class GroovyResourceMetadataResolver method resolve.

@Override
public Collection<MetadataResolver> resolve(final SamlRegisteredService service) {
    try {
        final String metadataLocation = service.getMetadataLocation();
        LOGGER.info("Loading SAML metadata via [{}]", metadataLocation);
        final AbstractResource metadataResource = ResourceUtils.getResourceFrom(metadataLocation);
        final Object[] args = { service, this.configBean, this.samlIdPProperties, LOGGER };
        final MetadataResolver metadataResolver = ScriptingUtils.executeGroovyScript(metadataResource, args, MetadataResolver.class);
        if (metadataResolver != null) {
            return CollectionUtils.wrap(metadataResolver);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new ArrayList<>(0);
}
Also used : ArrayList(java.util.ArrayList) AbstractResource(org.springframework.core.io.AbstractResource) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver)

Example 4 with MetadataResolver

use of org.opensaml.saml.metadata.resolver.MetadataResolver in project pac4j by pac4j.

the class SAML2Client method initChainingMetadataResolver.

protected ChainingMetadataResolver initChainingMetadataResolver(final MetadataResolver idpMetadataProvider, final MetadataResolver spMetadataProvider) {
    final ChainingMetadataResolver metadataManager = new ChainingMetadataResolver();
    metadataManager.setId(ChainingMetadataResolver.class.getCanonicalName());
    try {
        final List<MetadataResolver> list = new ArrayList<>();
        list.add(idpMetadataProvider);
        list.add(spMetadataProvider);
        metadataManager.setResolvers(list);
        metadataManager.initialize();
    } catch (final ResolverException e) {
        throw new TechnicalException("Error adding idp or sp metadatas to manager", e);
    } catch (final ComponentInitializationException e) {
        throw new TechnicalException("Error initializing manager", e);
    }
    return metadataManager;
}
Also used : ChainingMetadataResolver(org.opensaml.saml.metadata.resolver.ChainingMetadataResolver) ResolverException(net.shibboleth.utilities.java.support.resolver.ResolverException) TechnicalException(org.pac4j.core.exception.TechnicalException) ComponentInitializationException(net.shibboleth.utilities.java.support.component.ComponentInitializationException) ArrayList(java.util.ArrayList) SAML2IdentityProviderMetadataResolver(org.pac4j.saml.metadata.SAML2IdentityProviderMetadataResolver) SAML2ServiceProviderMetadataResolver(org.pac4j.saml.metadata.SAML2ServiceProviderMetadataResolver) SAML2MetadataResolver(org.pac4j.saml.metadata.SAML2MetadataResolver) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver) ChainingMetadataResolver(org.opensaml.saml.metadata.resolver.ChainingMetadataResolver)

Example 5 with MetadataResolver

use of org.opensaml.saml.metadata.resolver.MetadataResolver 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)

Aggregations

MetadataResolver (org.opensaml.saml.metadata.resolver.MetadataResolver)16 ArrayList (java.util.ArrayList)6 ChainingMetadataResolver (org.opensaml.saml.metadata.resolver.ChainingMetadataResolver)6 CriteriaSet (net.shibboleth.utilities.java.support.resolver.CriteriaSet)5 SamlRegisteredService (org.apereo.cas.support.saml.services.SamlRegisteredService)4 Slf4j (lombok.extern.slf4j.Slf4j)3 ResolverException (net.shibboleth.utilities.java.support.resolver.ResolverException)3 Provides (com.google.inject.Provides)2 File (java.io.File)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 List (java.util.List)2 Objects (java.util.Objects)2 Named (javax.inject.Named)2 Singleton (javax.inject.Singleton)2 SneakyThrows (lombok.SneakyThrows)2 lombok.val (lombok.val)2 ComponentInitializationException (net.shibboleth.utilities.java.support.component.ComponentInitializationException)2 StringUtils (org.apache.commons.lang3.StringUtils)2 SamlException (org.apereo.cas.support.saml.SamlException)2