Skip to content

Commit 20358e7

Browse files
committed
Merge branch '5.8.x' into 6.0.x
Closes gh-12936
2 parents 6db2b0d + c15589e commit 20358e7

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/OpenSamlAuthenticationRequestResolver.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,10 +30,12 @@
3030
import org.opensaml.saml.saml2.core.AuthnRequest;
3131
import org.opensaml.saml.saml2.core.Issuer;
3232
import org.opensaml.saml.saml2.core.NameID;
33+
import org.opensaml.saml.saml2.core.NameIDPolicy;
3334
import org.opensaml.saml.saml2.core.impl.AuthnRequestBuilder;
3435
import org.opensaml.saml.saml2.core.impl.AuthnRequestMarshaller;
3536
import org.opensaml.saml.saml2.core.impl.IssuerBuilder;
3637
import org.opensaml.saml.saml2.core.impl.NameIDBuilder;
38+
import org.opensaml.saml.saml2.core.impl.NameIDPolicyBuilder;
3739
import org.w3c.dom.Element;
3840

3941
import org.springframework.core.convert.converter.Converter;
@@ -69,6 +71,8 @@ class OpenSamlAuthenticationRequestResolver {
6971

7072
private final NameIDBuilder nameIdBuilder;
7173

74+
private final NameIDPolicyBuilder nameIdPolicyBuilder;
75+
7276
private RequestMatcher requestMatcher = new AntPathRequestMatcher(
7377
Saml2AuthenticationRequestResolver.DEFAULT_AUTHENTICATION_REQUEST_URI);
7478

@@ -94,6 +98,9 @@ class OpenSamlAuthenticationRequestResolver {
9498
Assert.notNull(this.issuerBuilder, "issuerBuilder must be configured in OpenSAML");
9599
this.nameIdBuilder = (NameIDBuilder) registry.getBuilderFactory().getBuilder(NameID.DEFAULT_ELEMENT_NAME);
96100
Assert.notNull(this.nameIdBuilder, "nameIdBuilder must be configured in OpenSAML");
101+
this.nameIdPolicyBuilder = (NameIDPolicyBuilder) registry.getBuilderFactory()
102+
.getBuilder(NameIDPolicy.DEFAULT_ELEMENT_NAME);
103+
Assert.notNull(this.nameIdPolicyBuilder, "nameIdPolicyBuilder must be configured in OpenSAML");
97104
}
98105

99106
void setRelayStateResolver(Converter<HttpServletRequest, String> relayStateResolver) {
@@ -129,6 +136,11 @@ <T extends AbstractSaml2AuthenticationRequest> T resolve(HttpServletRequest requ
129136
authnRequest.setIssuer(iss);
130137
authnRequest.setDestination(registration.getAssertingPartyDetails().getSingleSignOnServiceLocation());
131138
authnRequest.setAssertionConsumerServiceURL(registration.getAssertionConsumerServiceLocation());
139+
if (registration.getNameIdFormat() != null) {
140+
NameIDPolicy nameIdPolicy = this.nameIdPolicyBuilder.buildObject();
141+
nameIdPolicy.setFormat(registration.getNameIdFormat());
142+
authnRequest.setNameIDPolicy(nameIdPolicy);
143+
}
132144
authnRequestConsumer.accept(registration, authnRequest);
133145
if (authnRequest.getID() == null) {
134146
authnRequest.setID("ARQ" + UUID.randomUUID().toString().substring(1));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ public static RelyingPartyRegistration.Builder relyingPartyRegistration() {
3838
Saml2X509Credential verificationCertificate = TestSaml2X509Credentials.relyingPartyVerifyingCredential();
3939
String singleSignOnServiceLocation = "https://simplesaml-for-spring-saml.apps.pcfone.io/saml2/idp/SSOService.php";
4040
String singleLogoutServiceLocation = "{baseUrl}/logout/saml2/slo";
41-
return RelyingPartyRegistration.withRegistrationId(registrationId).entityId(rpEntityId)
41+
return RelyingPartyRegistration.withRegistrationId(registrationId).entityId(rpEntityId).nameIdFormat("format")
4242
.assertionConsumerServiceLocation(assertionConsumerServiceLocation)
4343
.singleLogoutServiceLocation(singleLogoutServiceLocation)
4444
.signingX509Credentials((c) -> c.add(signingCredential)).assertingPartyDetails(

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/authentication/OpenSamlAuthenticationRequestResolverTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@ public void resolveAuthenticationRequestWhenSignedRedirectThenSignsAndRedirects(
5252
RelyingPartyRegistration registration = this.relyingPartyRegistrationBuilder.build();
5353
OpenSamlAuthenticationRequestResolver resolver = authenticationRequestResolver(registration);
5454
Saml2RedirectAuthenticationRequest result = resolver.resolve(request, (r, authnRequest) -> {
55+
assertThat(authnRequest.getNameIDPolicy().getFormat()).isEqualTo(registration.getNameIdFormat());
5556
assertThat(authnRequest.getAssertionConsumerServiceURL())
5657
.isEqualTo(registration.getAssertionConsumerServiceLocation());
5758
assertThat(authnRequest.getProtocolBinding())
@@ -76,6 +77,7 @@ public void resolveAuthenticationRequestWhenUnsignedRedirectThenRedirectsAndNoSi
7677
.assertingPartyDetails((party) -> party.wantAuthnRequestsSigned(false)).build();
7778
OpenSamlAuthenticationRequestResolver resolver = authenticationRequestResolver(registration);
7879
Saml2RedirectAuthenticationRequest result = resolver.resolve(request, (r, authnRequest) -> {
80+
assertThat(authnRequest.getNameIDPolicy().getFormat()).isEqualTo(registration.getNameIdFormat());
7981
assertThat(authnRequest.getAssertionConsumerServiceURL())
8082
.isEqualTo(registration.getAssertionConsumerServiceLocation());
8183
assertThat(authnRequest.getProtocolBinding())
@@ -114,6 +116,7 @@ public void resolveAuthenticationRequestWhenUnsignedPostThenOnlyPosts() {
114116
.build();
115117
OpenSamlAuthenticationRequestResolver resolver = authenticationRequestResolver(registration);
116118
Saml2PostAuthenticationRequest result = resolver.resolve(request, (r, authnRequest) -> {
119+
assertThat(authnRequest.getNameIDPolicy().getFormat()).isEqualTo(registration.getNameIdFormat());
117120
assertThat(authnRequest.getAssertionConsumerServiceURL())
118121
.isEqualTo(registration.getAssertionConsumerServiceLocation());
119122
assertThat(authnRequest.getProtocolBinding())
@@ -137,6 +140,7 @@ public void resolveAuthenticationRequestWhenSignedPostThenSignsAndPosts() {
137140
.assertingPartyDetails((party) -> party.singleSignOnServiceBinding(Saml2MessageBinding.POST)).build();
138141
OpenSamlAuthenticationRequestResolver resolver = authenticationRequestResolver(registration);
139142
Saml2PostAuthenticationRequest result = resolver.resolve(request, (r, authnRequest) -> {
143+
assertThat(authnRequest.getNameIDPolicy().getFormat()).isEqualTo(registration.getNameIdFormat());
140144
assertThat(authnRequest.getAssertionConsumerServiceURL())
141145
.isEqualTo(registration.getAssertionConsumerServiceLocation());
142146
assertThat(authnRequest.getProtocolBinding())

0 commit comments

Comments
 (0)