|
| 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.web; |
| 18 | + |
| 19 | +import net.shibboleth.utilities.java.support.xml.SerializeSupport; |
| 20 | +import org.opensaml.core.xml.XMLObjectBuilder; |
| 21 | +import org.opensaml.core.xml.XMLObjectBuilderFactory; |
| 22 | +import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport; |
| 23 | +import org.opensaml.core.xml.io.Marshaller; |
| 24 | +import org.opensaml.saml.common.xml.SAMLConstants; |
| 25 | +import org.opensaml.saml.saml2.metadata.AssertionConsumerService; |
| 26 | +import org.opensaml.saml.saml2.metadata.EntityDescriptor; |
| 27 | +import org.opensaml.saml.saml2.metadata.KeyDescriptor; |
| 28 | +import org.opensaml.saml.saml2.metadata.NameIDFormat; |
| 29 | +import org.opensaml.saml.saml2.metadata.SPSSODescriptor; |
| 30 | +import org.opensaml.security.credential.UsageType; |
| 31 | +import org.opensaml.xmlsec.signature.KeyInfo; |
| 32 | +import org.opensaml.xmlsec.signature.X509Certificate; |
| 33 | +import org.opensaml.xmlsec.signature.X509Data; |
| 34 | +import org.springframework.security.saml2.Saml2Exception; |
| 35 | +import org.springframework.security.saml2.credentials.Saml2X509Credential; |
| 36 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 37 | +import org.springframework.security.saml2.provider.service.servlet.filter.Saml2ServletUtils; |
| 38 | +import org.w3c.dom.Element; |
| 39 | + |
| 40 | +import javax.servlet.http.HttpServletRequest; |
| 41 | +import javax.xml.namespace.QName; |
| 42 | +import java.security.cert.CertificateEncodingException; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Base64; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author Jakub Kubrynski |
| 49 | + * @since 5.4 |
| 50 | + */ |
| 51 | +public class OpenSamlMetadataResolver implements Saml2MetadataResolver { |
| 52 | + |
| 53 | + @Override |
| 54 | + public String resolveMetadata(HttpServletRequest request, RelyingPartyRegistration registration) { |
| 55 | + |
| 56 | + XMLObjectBuilderFactory builderFactory = XMLObjectProviderRegistrySupport.getBuilderFactory(); |
| 57 | + |
| 58 | + EntityDescriptor entityDescriptor = buildObject(builderFactory, EntityDescriptor.ELEMENT_QNAME); |
| 59 | + |
| 60 | + entityDescriptor.setEntityID( |
| 61 | + resolveTemplate(registration.getEntityId(), registration, request)); |
| 62 | + |
| 63 | + SPSSODescriptor spSsoDescriptor = buildSpSsoDescriptor(registration, builderFactory, request); |
| 64 | + entityDescriptor.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME).add(spSsoDescriptor); |
| 65 | + |
| 66 | + return serializeToXmlString(entityDescriptor); |
| 67 | + } |
| 68 | + |
| 69 | + private String serializeToXmlString(EntityDescriptor entityDescriptor) { |
| 70 | + Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(entityDescriptor); |
| 71 | + if (marshaller == null) { |
| 72 | + throw new Saml2Exception("Unable to resolve Marshaller"); |
| 73 | + } |
| 74 | + Element element; |
| 75 | + try { |
| 76 | + element = marshaller.marshall(entityDescriptor); |
| 77 | + } catch (Exception e) { |
| 78 | + throw new Saml2Exception(e); |
| 79 | + } |
| 80 | + return SerializeSupport.prettyPrintXML(element); |
| 81 | + } |
| 82 | + |
| 83 | + private SPSSODescriptor buildSpSsoDescriptor(RelyingPartyRegistration registration, |
| 84 | + XMLObjectBuilderFactory builderFactory, HttpServletRequest request) { |
| 85 | + |
| 86 | + SPSSODescriptor spSsoDescriptor = buildObject(builderFactory, SPSSODescriptor.DEFAULT_ELEMENT_NAME); |
| 87 | + spSsoDescriptor.setAuthnRequestsSigned(registration.getAssertingPartyDetails().getWantAuthnRequestsSigned()); |
| 88 | + spSsoDescriptor.setWantAssertionsSigned(true); |
| 89 | + spSsoDescriptor.addSupportedProtocol(SAMLConstants.SAML20P_NS); |
| 90 | + |
| 91 | + NameIDFormat nameIdFormat = buildObject(builderFactory, NameIDFormat.DEFAULT_ELEMENT_NAME); |
| 92 | + nameIdFormat.setFormat(registration.getAssertingPartyDetails().getNameIdFormat()); |
| 93 | + spSsoDescriptor.getNameIDFormats().add(nameIdFormat); |
| 94 | + |
| 95 | + spSsoDescriptor.getAssertionConsumerServices().add( |
| 96 | + buildAssertionConsumerService(registration, builderFactory, request)); |
| 97 | + |
| 98 | + spSsoDescriptor.getKeyDescriptors().addAll(buildKeys(builderFactory, |
| 99 | + registration.getSigningCredentials(), UsageType.SIGNING)); |
| 100 | + spSsoDescriptor.getKeyDescriptors().addAll(buildKeys(builderFactory, |
| 101 | + registration.getEncryptionCredentials(), UsageType.ENCRYPTION)); |
| 102 | + |
| 103 | + return spSsoDescriptor; |
| 104 | + } |
| 105 | + |
| 106 | + private List<KeyDescriptor> buildKeys(XMLObjectBuilderFactory builderFactory, |
| 107 | + List<Saml2X509Credential> credentials, UsageType usageType) { |
| 108 | + List<KeyDescriptor> list = new ArrayList<>(); |
| 109 | + for (Saml2X509Credential credential : credentials) { |
| 110 | + KeyDescriptor keyDescriptor = buildKeyDescriptor(builderFactory, usageType, credential.getCertificate()); |
| 111 | + list.add(keyDescriptor); |
| 112 | + } |
| 113 | + return list; |
| 114 | + } |
| 115 | + |
| 116 | + private KeyDescriptor buildKeyDescriptor(XMLObjectBuilderFactory builderFactory, UsageType usageType, |
| 117 | + java.security.cert.X509Certificate certificate) { |
| 118 | + KeyDescriptor keyDescriptor = buildObject(builderFactory, KeyDescriptor.DEFAULT_ELEMENT_NAME); |
| 119 | + KeyInfo keyInfo = buildObject(builderFactory, KeyInfo.DEFAULT_ELEMENT_NAME); |
| 120 | + X509Certificate x509Certificate = buildObject(builderFactory, X509Certificate.DEFAULT_ELEMENT_NAME); |
| 121 | + X509Data x509Data = buildObject(builderFactory, X509Data.DEFAULT_ELEMENT_NAME); |
| 122 | + |
| 123 | + try { |
| 124 | + x509Certificate.setValue(new String(Base64.getEncoder().encode(certificate.getEncoded()))); |
| 125 | + } catch (CertificateEncodingException e) { |
| 126 | + throw new Saml2Exception("Cannot encode certificate " + certificate.toString()); |
| 127 | + } |
| 128 | + |
| 129 | + x509Data.getX509Certificates().add(x509Certificate); |
| 130 | + keyInfo.getX509Datas().add(x509Data); |
| 131 | + |
| 132 | + keyDescriptor.setUse(usageType); |
| 133 | + keyDescriptor.setKeyInfo(keyInfo); |
| 134 | + return keyDescriptor; |
| 135 | + } |
| 136 | + |
| 137 | + private AssertionConsumerService buildAssertionConsumerService(RelyingPartyRegistration registration, |
| 138 | + XMLObjectBuilderFactory builderFactory, HttpServletRequest request) { |
| 139 | + AssertionConsumerService assertionConsumerService = buildObject(builderFactory, AssertionConsumerService.DEFAULT_ELEMENT_NAME); |
| 140 | + |
| 141 | + assertionConsumerService.setLocation( |
| 142 | + resolveTemplate(registration.getAssertionConsumerServiceLocation(), registration, request)); |
| 143 | + assertionConsumerService.setBinding(registration.getAssertingPartyDetails().getSingleSignOnServiceBinding().getUrn()); |
| 144 | + assertionConsumerService.setIndex(1); |
| 145 | + return assertionConsumerService; |
| 146 | + } |
| 147 | + |
| 148 | + @SuppressWarnings("unchecked") |
| 149 | + private <T> T buildObject(XMLObjectBuilderFactory builderFactory, QName elementName) { |
| 150 | + XMLObjectBuilder<?> builder = builderFactory.getBuilder(elementName); |
| 151 | + if (builder == null) { |
| 152 | + throw new Saml2Exception("Cannot build object - builder not defined for element " + elementName); |
| 153 | + } |
| 154 | + return (T) builder.buildObject(elementName); |
| 155 | + } |
| 156 | + |
| 157 | + private String resolveTemplate(String template, RelyingPartyRegistration registration, HttpServletRequest request) { |
| 158 | + return Saml2ServletUtils.resolveUrlTemplate(template, Saml2ServletUtils.getApplicationUri(request), registration); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments