Skip to content

Commit aba0e90

Browse files
committed
Read SigningMethod Elements
Closes gh-9177
1 parent e1826a0 commit aba0e90

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/registration/OpenSamlAssertingPartyMetadataConverter.java

+25
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
import org.opensaml.core.xml.config.XMLObjectProviderRegistry;
2929
import org.opensaml.core.xml.io.Unmarshaller;
3030
import org.opensaml.saml.common.xml.SAMLConstants;
31+
import org.opensaml.saml.ext.saml2alg.SigningMethod;
3132
import org.opensaml.saml.saml2.metadata.EntitiesDescriptor;
3233
import org.opensaml.saml.saml2.metadata.EntityDescriptor;
34+
import org.opensaml.saml.saml2.metadata.Extensions;
3335
import org.opensaml.saml.saml2.metadata.IDPSSODescriptor;
3436
import org.opensaml.saml.saml2.metadata.KeyDescriptor;
3537
import org.opensaml.saml.saml2.metadata.SingleSignOnService;
@@ -98,6 +100,11 @@ RelyingPartyRegistration.Builder convert(InputStream inputStream) {
98100
.wantAuthnRequestsSigned(Boolean.TRUE.equals(idpssoDescriptor.getWantAuthnRequestsSigned()))
99101
.verificationX509Credentials((c) -> c.addAll(verification))
100102
.encryptionX509Credentials((c) -> c.addAll(encryption)));
103+
List<SigningMethod> signingMethods = signingMethods(idpssoDescriptor);
104+
for (SigningMethod method : signingMethods) {
105+
builder.assertingPartyDetails(
106+
(party) -> party.signingAlgorithms((algorithms) -> algorithms.add(method.getAlgorithm())));
107+
}
101108
for (SingleSignOnService singleSignOnService : idpssoDescriptor.getSingleSignOnServices()) {
102109
Saml2MessageBinding binding;
103110
if (singleSignOnService.getBinding().equals(Saml2MessageBinding.POST.getUrn())) {
@@ -127,6 +134,17 @@ private List<X509Certificate> certificates(KeyDescriptor keyDescriptor) {
127134
}
128135
}
129136

137+
private List<SigningMethod> signingMethods(IDPSSODescriptor idpssoDescriptor) {
138+
Extensions extensions = idpssoDescriptor.getExtensions();
139+
List<SigningMethod> result = signingMethods(extensions);
140+
if (!result.isEmpty()) {
141+
return result;
142+
}
143+
EntityDescriptor descriptor = (EntityDescriptor) idpssoDescriptor.getParent();
144+
extensions = descriptor.getExtensions();
145+
return signingMethods(extensions);
146+
}
147+
130148
private EntityDescriptor entityDescriptor(InputStream inputStream) {
131149
Document document = document(inputStream);
132150
Element element = document.getDocumentElement();
@@ -158,4 +176,11 @@ private Document document(InputStream inputStream) {
158176
}
159177
}
160178

179+
private <T> List<T> signingMethods(Extensions extensions) {
180+
if (extensions != null) {
181+
return (List<T>) extensions.getUnknownXMLObjects(SigningMethod.DEFAULT_ELEMENT_NAME);
182+
}
183+
return new ArrayList<>();
184+
}
185+
161186
}

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/registration/OpenSamlAssertingPartyMetadataConverterTests.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.junit.Before;
2626
import org.junit.Test;
27+
import org.opensaml.xmlsec.signature.support.SignatureConstants;
2728

2829
import org.springframework.security.saml2.Saml2Exception;
2930

@@ -37,7 +38,7 @@ public class OpenSamlAssertingPartyMetadataConverterTests {
3738
private static final String ENTITIES_DESCRIPTOR_TEMPLATE = "<md:EntitiesDescriptor xmlns:md=\"urn:oasis:names:tc:SAML:2.0:metadata\">\n%s</md:EntitiesDescriptor>";
3839

3940
private static final String ENTITY_DESCRIPTOR_TEMPLATE = "<md:EntityDescriptor xmlns:md=\"urn:oasis:names:tc:SAML:2.0:metadata\" "
40-
+ "entityID=\"entity-id\" "
41+
+ "xmlns:alg=\"urn:oasis:names:tc:SAML:metadata:algsupport\" " + "entityID=\"entity-id\" "
4142
+ "ID=\"_bf133aac099b99b3d81286e1a341f2d34188043a77fe15bf4bf1487dae9b2ea3\">\n%s"
4243
+ "</md:EntityDescriptor>";
4344

@@ -49,6 +50,9 @@ public class OpenSamlAssertingPartyMetadataConverterTests {
4950
+ "<ds:X509Certificate>" + CERTIFICATE + "</ds:X509Certificate>\n" + "</ds:X509Data>\n" + "</ds:KeyInfo>\n"
5051
+ "</md:KeyDescriptor>";
5152

53+
private static final String EXTENSIONS_TEMPLATE = "<md:Extensions>" + "<alg:SigningMethod Algorithm=\""
54+
+ SignatureConstants.ALGO_ID_DIGEST_SHA512 + "\"/>" + "</md:Extensions>";
55+
5256
private static final String SINGLE_SIGN_ON_SERVICE_TEMPLATE = "<md:SingleSignOnService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect\" "
5357
+ "Location=\"sso-location\"/>";
5458

@@ -91,12 +95,13 @@ public void readWhenDescriptorFullySpecifiedThenConfigures() throws Exception {
9195
String payload = String.format(ENTITY_DESCRIPTOR_TEMPLATE,
9296
String.format(IDP_SSO_DESCRIPTOR_TEMPLATE,
9397
String.format(KEY_DESCRIPTOR_TEMPLATE, "use=\"signing\"")
94-
+ String.format(KEY_DESCRIPTOR_TEMPLATE, "use=\"encryption\"")
98+
+ String.format(KEY_DESCRIPTOR_TEMPLATE, "use=\"encryption\"") + EXTENSIONS_TEMPLATE
9599
+ String.format(SINGLE_SIGN_ON_SERVICE_TEMPLATE)));
96100
InputStream inputStream = new ByteArrayInputStream(payload.getBytes());
97101
RelyingPartyRegistration registration = this.converter.convert(inputStream).registrationId("one").build();
98102
RelyingPartyRegistration.AssertingPartyDetails details = registration.getAssertingPartyDetails();
99103
assertThat(details.getWantAuthnRequestsSigned()).isFalse();
104+
assertThat(details.getSigningAlgorithms()).containsExactly(SignatureConstants.ALGO_ID_DIGEST_SHA512);
100105
assertThat(details.getSingleSignOnServiceLocation()).isEqualTo("sso-location");
101106
assertThat(details.getSingleSignOnServiceBinding()).isEqualTo(Saml2MessageBinding.REDIRECT);
102107
assertThat(details.getEntityId()).isEqualTo("entity-id");

0 commit comments

Comments
 (0)