|
30 | 30 | import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
|
31 | 31 | import org.opensaml.saml.common.xml.SAMLConstants;
|
32 | 32 | import org.opensaml.saml.saml2.metadata.AssertionConsumerService;
|
| 33 | +import org.opensaml.saml.saml2.metadata.EntitiesDescriptor; |
33 | 34 | import org.opensaml.saml.saml2.metadata.EntityDescriptor;
|
34 | 35 | import org.opensaml.saml.saml2.metadata.KeyDescriptor;
|
35 | 36 | import org.opensaml.saml.saml2.metadata.NameIDFormat;
|
36 | 37 | import org.opensaml.saml.saml2.metadata.SPSSODescriptor;
|
37 | 38 | import org.opensaml.saml.saml2.metadata.SingleLogoutService;
|
| 39 | +import org.opensaml.saml.saml2.metadata.impl.EntitiesDescriptorMarshaller; |
38 | 40 | import org.opensaml.saml.saml2.metadata.impl.EntityDescriptorMarshaller;
|
39 | 41 | import org.opensaml.security.credential.UsageType;
|
40 | 42 | import org.opensaml.xmlsec.signature.KeyInfo;
|
@@ -65,24 +67,47 @@ public final class OpenSamlMetadataResolver implements Saml2MetadataResolver {
|
65 | 67 |
|
66 | 68 | private final EntityDescriptorMarshaller entityDescriptorMarshaller;
|
67 | 69 |
|
| 70 | + private final EntitiesDescriptorMarshaller entitiesDescriptorMarshaller; |
| 71 | + |
68 | 72 | private Consumer<EntityDescriptorParameters> entityDescriptorCustomizer = (parameters) -> {
|
69 | 73 | };
|
70 | 74 |
|
71 | 75 | public OpenSamlMetadataResolver() {
|
72 | 76 | this.entityDescriptorMarshaller = (EntityDescriptorMarshaller) XMLObjectProviderRegistrySupport
|
73 | 77 | .getMarshallerFactory().getMarshaller(EntityDescriptor.DEFAULT_ELEMENT_NAME);
|
74 | 78 | Assert.notNull(this.entityDescriptorMarshaller, "entityDescriptorMarshaller cannot be null");
|
| 79 | + this.entitiesDescriptorMarshaller = (EntitiesDescriptorMarshaller) XMLObjectProviderRegistrySupport |
| 80 | + .getMarshallerFactory().getMarshaller(EntitiesDescriptor.DEFAULT_ELEMENT_NAME); |
| 81 | + Assert.notNull(this.entitiesDescriptorMarshaller, "entitiesDescriptorMarshaller cannot be null"); |
75 | 82 | }
|
76 | 83 |
|
77 | 84 | @Override
|
78 | 85 | public String resolve(RelyingPartyRegistration relyingPartyRegistration) {
|
| 86 | + EntityDescriptor entityDescriptor = entityDescriptor(relyingPartyRegistration); |
| 87 | + return serialize(entityDescriptor); |
| 88 | + } |
| 89 | + |
| 90 | + public String resolve(Iterable<RelyingPartyRegistration> relyingPartyRegistrations) { |
| 91 | + Collection<EntityDescriptor> entityDescriptors = new ArrayList<>(); |
| 92 | + for (RelyingPartyRegistration registration : relyingPartyRegistrations) { |
| 93 | + EntityDescriptor entityDescriptor = entityDescriptor(registration); |
| 94 | + entityDescriptors.add(entityDescriptor); |
| 95 | + } |
| 96 | + if (entityDescriptors.size() == 1) { |
| 97 | + return serialize(entityDescriptors.iterator().next()); |
| 98 | + } |
| 99 | + EntitiesDescriptor entities = build(EntitiesDescriptor.DEFAULT_ELEMENT_NAME); |
| 100 | + entities.getEntityDescriptors().addAll(entityDescriptors); |
| 101 | + return serialize(entities); |
| 102 | + } |
| 103 | + |
| 104 | + private EntityDescriptor entityDescriptor(RelyingPartyRegistration registration) { |
79 | 105 | EntityDescriptor entityDescriptor = build(EntityDescriptor.DEFAULT_ELEMENT_NAME);
|
80 |
| - entityDescriptor.setEntityID(relyingPartyRegistration.getEntityId()); |
81 |
| - SPSSODescriptor spSsoDescriptor = buildSpSsoDescriptor(relyingPartyRegistration); |
| 106 | + entityDescriptor.setEntityID(registration.getEntityId()); |
| 107 | + SPSSODescriptor spSsoDescriptor = buildSpSsoDescriptor(registration); |
82 | 108 | entityDescriptor.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME).add(spSsoDescriptor);
|
83 |
| - this.entityDescriptorCustomizer |
84 |
| - .accept(new EntityDescriptorParameters(entityDescriptor, relyingPartyRegistration)); |
85 |
| - return serialize(entityDescriptor); |
| 109 | + this.entityDescriptorCustomizer.accept(new EntityDescriptorParameters(entityDescriptor, registration)); |
| 110 | + return entityDescriptor; |
86 | 111 | }
|
87 | 112 |
|
88 | 113 | /**
|
@@ -184,6 +209,16 @@ private String serialize(EntityDescriptor entityDescriptor) {
|
184 | 209 | }
|
185 | 210 | }
|
186 | 211 |
|
| 212 | + private String serialize(EntitiesDescriptor entities) { |
| 213 | + try { |
| 214 | + Element element = this.entitiesDescriptorMarshaller.marshall(entities); |
| 215 | + return SerializeSupport.prettyPrintXML(element); |
| 216 | + } |
| 217 | + catch (Exception ex) { |
| 218 | + throw new Saml2Exception(ex); |
| 219 | + } |
| 220 | + } |
| 221 | + |
187 | 222 | /**
|
188 | 223 | * A tuple containing an OpenSAML {@link EntityDescriptor} and its associated
|
189 | 224 | * {@link RelyingPartyRegistration}
|
|
0 commit comments