|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.saml2.provider.service.metadata; |
| 18 | + |
| 19 | +import java.security.cert.CertificateEncodingException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Base64; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.List; |
| 24 | +import javax.xml.namespace.QName; |
| 25 | + |
| 26 | +import net.shibboleth.utilities.java.support.xml.SerializeSupport; |
| 27 | +import org.opensaml.core.xml.XMLObjectBuilder; |
| 28 | +import org.opensaml.saml.common.xml.SAMLConstants; |
| 29 | +import org.opensaml.saml.saml2.metadata.AssertionConsumerService; |
| 30 | +import org.opensaml.saml.saml2.metadata.EntityDescriptor; |
| 31 | +import org.opensaml.saml.saml2.metadata.KeyDescriptor; |
| 32 | +import org.opensaml.saml.saml2.metadata.SPSSODescriptor; |
| 33 | +import org.opensaml.saml.saml2.metadata.impl.EntityDescriptorMarshaller; |
| 34 | +import org.opensaml.security.credential.UsageType; |
| 35 | +import org.opensaml.xmlsec.signature.KeyInfo; |
| 36 | +import org.opensaml.xmlsec.signature.X509Certificate; |
| 37 | +import org.opensaml.xmlsec.signature.X509Data; |
| 38 | +import org.w3c.dom.Element; |
| 39 | + |
| 40 | +import org.springframework.security.saml2.Saml2Exception; |
| 41 | +import org.springframework.security.saml2.core.OpenSamlInitializationService; |
| 42 | +import org.springframework.security.saml2.core.Saml2X509Credential; |
| 43 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 44 | +import org.springframework.util.Assert; |
| 45 | + |
| 46 | +import static org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport.getBuilderFactory; |
| 47 | +import static org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport.getMarshallerFactory; |
| 48 | + |
| 49 | +/** |
| 50 | + * Resolves the SAML 2.0 Relying Party Metadata for a given {@link RelyingPartyRegistration} |
| 51 | + * using the OpenSAML API. |
| 52 | + * |
| 53 | + * @author Jakub Kubrynski |
| 54 | + * @author Josh Cummings |
| 55 | + * @since 5.4 |
| 56 | + */ |
| 57 | +public final class OpenSamlMetadataResolver implements Saml2MetadataResolver { |
| 58 | + static { |
| 59 | + OpenSamlInitializationService.initialize(); |
| 60 | + } |
| 61 | + |
| 62 | + private final EntityDescriptorMarshaller entityDescriptorMarshaller; |
| 63 | + |
| 64 | + public OpenSamlMetadataResolver() { |
| 65 | + this.entityDescriptorMarshaller = (EntityDescriptorMarshaller) |
| 66 | + getMarshallerFactory().getMarshaller(EntityDescriptor.DEFAULT_ELEMENT_NAME); |
| 67 | + Assert.notNull(this.entityDescriptorMarshaller, "entityDescriptorMarshaller cannot be null"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritDoc} |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public String resolve(RelyingPartyRegistration relyingPartyRegistration) { |
| 75 | + EntityDescriptor entityDescriptor = build(EntityDescriptor.ELEMENT_QNAME); |
| 76 | + entityDescriptor.setEntityID(relyingPartyRegistration.getEntityId()); |
| 77 | + |
| 78 | + SPSSODescriptor spSsoDescriptor = buildSpSsoDescriptor(relyingPartyRegistration); |
| 79 | + entityDescriptor.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME).add(spSsoDescriptor); |
| 80 | + |
| 81 | + return serialize(entityDescriptor); |
| 82 | + } |
| 83 | + |
| 84 | + private SPSSODescriptor buildSpSsoDescriptor(RelyingPartyRegistration registration) { |
| 85 | + SPSSODescriptor spSsoDescriptor = build(SPSSODescriptor.DEFAULT_ELEMENT_NAME); |
| 86 | + spSsoDescriptor.addSupportedProtocol(SAMLConstants.SAML20P_NS); |
| 87 | + spSsoDescriptor.setWantAssertionsSigned(true); |
| 88 | + spSsoDescriptor.getKeyDescriptors().addAll(buildKeys( |
| 89 | + registration.getSigningX509Credentials(), UsageType.SIGNING)); |
| 90 | + spSsoDescriptor.getKeyDescriptors().addAll(buildKeys( |
| 91 | + registration.getDecryptionX509Credentials(), UsageType.ENCRYPTION)); |
| 92 | + spSsoDescriptor.getAssertionConsumerServices().add(buildAssertionConsumerService(registration)); |
| 93 | + return spSsoDescriptor; |
| 94 | + } |
| 95 | + |
| 96 | + private List<KeyDescriptor> buildKeys(Collection<Saml2X509Credential> credentials, UsageType usageType) { |
| 97 | + List<KeyDescriptor> list = new ArrayList<>(); |
| 98 | + for (Saml2X509Credential credential : credentials) { |
| 99 | + KeyDescriptor keyDescriptor = buildKeyDescriptor(usageType, credential.getCertificate()); |
| 100 | + list.add(keyDescriptor); |
| 101 | + } |
| 102 | + return list; |
| 103 | + } |
| 104 | + |
| 105 | + private KeyDescriptor buildKeyDescriptor(UsageType usageType, java.security.cert.X509Certificate certificate) { |
| 106 | + KeyDescriptor keyDescriptor = build(KeyDescriptor.DEFAULT_ELEMENT_NAME); |
| 107 | + KeyInfo keyInfo = build(KeyInfo.DEFAULT_ELEMENT_NAME); |
| 108 | + X509Certificate x509Certificate = build(X509Certificate.DEFAULT_ELEMENT_NAME); |
| 109 | + X509Data x509Data = build(X509Data.DEFAULT_ELEMENT_NAME); |
| 110 | + |
| 111 | + try { |
| 112 | + x509Certificate.setValue(new String(Base64.getEncoder().encode(certificate.getEncoded()))); |
| 113 | + } catch (CertificateEncodingException e) { |
| 114 | + throw new Saml2Exception("Cannot encode certificate " + certificate.toString()); |
| 115 | + } |
| 116 | + |
| 117 | + x509Data.getX509Certificates().add(x509Certificate); |
| 118 | + keyInfo.getX509Datas().add(x509Data); |
| 119 | + |
| 120 | + keyDescriptor.setUse(usageType); |
| 121 | + keyDescriptor.setKeyInfo(keyInfo); |
| 122 | + return keyDescriptor; |
| 123 | + } |
| 124 | + |
| 125 | + private AssertionConsumerService buildAssertionConsumerService(RelyingPartyRegistration registration) { |
| 126 | + AssertionConsumerService assertionConsumerService = build(AssertionConsumerService.DEFAULT_ELEMENT_NAME); |
| 127 | + assertionConsumerService.setLocation(registration.getAssertionConsumerServiceLocation()); |
| 128 | + assertionConsumerService.setBinding(registration.getAssertionConsumerServiceBinding().getUrn()); |
| 129 | + assertionConsumerService.setIndex(1); |
| 130 | + return assertionConsumerService; |
| 131 | + } |
| 132 | + |
| 133 | + @SuppressWarnings("unchecked") |
| 134 | + private <T> T build(QName elementName) { |
| 135 | + XMLObjectBuilder<?> builder = getBuilderFactory().getBuilder(elementName); |
| 136 | + if (builder == null) { |
| 137 | + throw new Saml2Exception("Unable to resolve Builder for " + elementName); |
| 138 | + } |
| 139 | + return (T) builder.buildObject(elementName); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + private String serialize(EntityDescriptor entityDescriptor) { |
| 144 | + try { |
| 145 | + Element element = this.entityDescriptorMarshaller.marshall(entityDescriptor); |
| 146 | + return SerializeSupport.prettyPrintXML(element); |
| 147 | + } catch (Exception e) { |
| 148 | + throw new Saml2Exception(e); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments