Search in sources :

Example 66 with org.opensaml.saml.saml2.metadata

use of org.opensaml.saml.saml2.metadata in project spring-security by spring-projects.

the class OpenSamlMetadataAssertingPartyDetailsConverter method convert.

RelyingPartyRegistration.AssertingPartyDetails.Builder convert(EntityDescriptor descriptor) {
    IDPSSODescriptor idpssoDescriptor = descriptor.getIDPSSODescriptor(SAMLConstants.SAML20P_NS);
    if (idpssoDescriptor == null) {
        throw new Saml2Exception("Metadata response is missing the necessary IDPSSODescriptor element");
    }
    List<Saml2X509Credential> verification = new ArrayList<>();
    List<Saml2X509Credential> encryption = new ArrayList<>();
    for (KeyDescriptor keyDescriptor : idpssoDescriptor.getKeyDescriptors()) {
        if (keyDescriptor.getUse().equals(UsageType.SIGNING)) {
            List<X509Certificate> certificates = certificates(keyDescriptor);
            for (X509Certificate certificate : certificates) {
                verification.add(Saml2X509Credential.verification(certificate));
            }
        }
        if (keyDescriptor.getUse().equals(UsageType.ENCRYPTION)) {
            List<X509Certificate> certificates = certificates(keyDescriptor);
            for (X509Certificate certificate : certificates) {
                encryption.add(Saml2X509Credential.encryption(certificate));
            }
        }
        if (keyDescriptor.getUse().equals(UsageType.UNSPECIFIED)) {
            List<X509Certificate> certificates = certificates(keyDescriptor);
            for (X509Certificate certificate : certificates) {
                verification.add(Saml2X509Credential.verification(certificate));
                encryption.add(Saml2X509Credential.encryption(certificate));
            }
        }
    }
    if (verification.isEmpty()) {
        throw new Saml2Exception("Metadata response is missing verification certificates, necessary for verifying SAML assertions");
    }
    RelyingPartyRegistration.AssertingPartyDetails.Builder party = OpenSamlAssertingPartyDetails.withEntityDescriptor(descriptor).entityId(descriptor.getEntityID()).wantAuthnRequestsSigned(Boolean.TRUE.equals(idpssoDescriptor.getWantAuthnRequestsSigned())).verificationX509Credentials((c) -> c.addAll(verification)).encryptionX509Credentials((c) -> c.addAll(encryption));
    List<SigningMethod> signingMethods = signingMethods(idpssoDescriptor);
    for (SigningMethod method : signingMethods) {
        party.signingAlgorithms((algorithms) -> algorithms.add(method.getAlgorithm()));
    }
    if (idpssoDescriptor.getSingleSignOnServices().isEmpty()) {
        throw new Saml2Exception("Metadata response is missing a SingleSignOnService, necessary for sending AuthnRequests");
    }
    for (SingleSignOnService singleSignOnService : idpssoDescriptor.getSingleSignOnServices()) {
        Saml2MessageBinding binding;
        if (singleSignOnService.getBinding().equals(Saml2MessageBinding.POST.getUrn())) {
            binding = Saml2MessageBinding.POST;
        } else if (singleSignOnService.getBinding().equals(Saml2MessageBinding.REDIRECT.getUrn())) {
            binding = Saml2MessageBinding.REDIRECT;
        } else {
            continue;
        }
        party.singleSignOnServiceLocation(singleSignOnService.getLocation()).singleSignOnServiceBinding(binding);
        break;
    }
    for (SingleLogoutService singleLogoutService : idpssoDescriptor.getSingleLogoutServices()) {
        Saml2MessageBinding binding;
        if (singleLogoutService.getBinding().equals(Saml2MessageBinding.POST.getUrn())) {
            binding = Saml2MessageBinding.POST;
        } else if (singleLogoutService.getBinding().equals(Saml2MessageBinding.REDIRECT.getUrn())) {
            binding = Saml2MessageBinding.REDIRECT;
        } else {
            continue;
        }
        String responseLocation = (singleLogoutService.getResponseLocation() == null) ? singleLogoutService.getLocation() : singleLogoutService.getResponseLocation();
        party.singleLogoutServiceLocation(singleLogoutService.getLocation()).singleLogoutServiceResponseLocation(responseLocation).singleLogoutServiceBinding(binding);
        break;
    }
    return party;
}
Also used : X509Certificate(java.security.cert.X509Certificate) Arrays(java.util.Arrays) OpenSamlInitializationService(org.springframework.security.saml2.core.OpenSamlInitializationService) KeyDescriptor(org.opensaml.saml.saml2.metadata.KeyDescriptor) XMLObjectProviderRegistry(org.opensaml.core.xml.config.XMLObjectProviderRegistry) Extensions(org.opensaml.saml.saml2.metadata.Extensions) ArrayList(java.util.ArrayList) SigningMethod(org.opensaml.saml.ext.saml2alg.SigningMethod) SingleSignOnService(org.opensaml.saml.saml2.metadata.SingleSignOnService) Document(org.w3c.dom.Document) IDPSSODescriptor(org.opensaml.saml.saml2.metadata.IDPSSODescriptor) XMLObject(org.opensaml.core.xml.XMLObject) SAMLConstants(org.opensaml.saml.common.xml.SAMLConstants) EntitiesDescriptor(org.opensaml.saml.saml2.metadata.EntitiesDescriptor) UsageType(org.opensaml.security.credential.UsageType) SingleLogoutService(org.opensaml.saml.saml2.metadata.SingleLogoutService) Collection(java.util.Collection) Unmarshaller(org.opensaml.core.xml.io.Unmarshaller) Saml2Exception(org.springframework.security.saml2.Saml2Exception) ConfigurationService(org.opensaml.core.config.ConfigurationService) Saml2X509Credential(org.springframework.security.saml2.core.Saml2X509Credential) CertificateException(java.security.cert.CertificateException) KeyInfoSupport(org.opensaml.xmlsec.keyinfo.KeyInfoSupport) ParserPool(net.shibboleth.utilities.java.support.xml.ParserPool) List(java.util.List) Element(org.w3c.dom.Element) EntityDescriptor(org.opensaml.saml.saml2.metadata.EntityDescriptor) InputStream(java.io.InputStream) IDPSSODescriptor(org.opensaml.saml.saml2.metadata.IDPSSODescriptor) SingleLogoutService(org.opensaml.saml.saml2.metadata.SingleLogoutService) KeyDescriptor(org.opensaml.saml.saml2.metadata.KeyDescriptor) Saml2X509Credential(org.springframework.security.saml2.core.Saml2X509Credential) ArrayList(java.util.ArrayList) SingleSignOnService(org.opensaml.saml.saml2.metadata.SingleSignOnService) Saml2Exception(org.springframework.security.saml2.Saml2Exception) X509Certificate(java.security.cert.X509Certificate) SigningMethod(org.opensaml.saml.ext.saml2alg.SigningMethod)

Aggregations

EntityDescriptor (org.opensaml.saml.saml2.metadata.EntityDescriptor)24 SamlRegisteredServiceServiceProviderMetadataFacade (org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade)12 lombok.val (lombok.val)11 AuthnRequest (org.opensaml.saml.saml2.core.AuthnRequest)11 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 UnauthorizedServiceException (org.apereo.cas.services.UnauthorizedServiceException)9 Document (org.w3c.dom.Document)9 Element (org.w3c.dom.Element)9 List (java.util.List)8 CriteriaSet (net.shibboleth.utilities.java.support.resolver.CriteriaSet)8 SamlRegisteredServiceCachingMetadataResolver (org.apereo.cas.support.saml.services.idp.metadata.cache.SamlRegisteredServiceCachingMetadataResolver)8 Map (java.util.Map)7 SamlRegisteredService (org.apereo.cas.support.saml.services.SamlRegisteredService)7 XMLObject (org.opensaml.core.xml.XMLObject)7 X509Certificate (java.security.cert.X509Certificate)6 HashMap (java.util.HashMap)6 Slf4j (lombok.extern.slf4j.Slf4j)6 EntityIdCriterion (org.opensaml.core.criterion.EntityIdCriterion)6