Skip to content

Commit c15589e

Browse files
committed
Merge branch '5.7.x' into 5.8.x
Closes gh-12935
2 parents 6bda1d2 + a106188 commit c15589e

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.
@@ -31,10 +31,12 @@
3131
import org.opensaml.saml.saml2.core.AuthnRequest;
3232
import org.opensaml.saml.saml2.core.Issuer;
3333
import org.opensaml.saml.saml2.core.NameID;
34+
import org.opensaml.saml.saml2.core.NameIDPolicy;
3435
import org.opensaml.saml.saml2.core.impl.AuthnRequestBuilder;
3536
import org.opensaml.saml.saml2.core.impl.AuthnRequestMarshaller;
3637
import org.opensaml.saml.saml2.core.impl.IssuerBuilder;
3738
import org.opensaml.saml.saml2.core.impl.NameIDBuilder;
39+
import org.opensaml.saml.saml2.core.impl.NameIDPolicyBuilder;
3840
import org.w3c.dom.Element;
3941

4042
import org.springframework.core.convert.converter.Converter;
@@ -71,6 +73,8 @@ class OpenSamlAuthenticationRequestResolver {
7173

7274
private final NameIDBuilder nameIdBuilder;
7375

76+
private final NameIDPolicyBuilder nameIdPolicyBuilder;
77+
7478
private RequestMatcher requestMatcher = new AntPathRequestMatcher("/saml2/authenticate/{registrationId}");
7579

7680
private Converter<HttpServletRequest, String> relayStateResolver = (request) -> UUID.randomUUID().toString();
@@ -95,6 +99,9 @@ class OpenSamlAuthenticationRequestResolver {
9599
Assert.notNull(this.issuerBuilder, "issuerBuilder must be configured in OpenSAML");
96100
this.nameIdBuilder = (NameIDBuilder) registry.getBuilderFactory().getBuilder(NameID.DEFAULT_ELEMENT_NAME);
97101
Assert.notNull(this.nameIdBuilder, "nameIdBuilder must be configured in OpenSAML");
102+
this.nameIdPolicyBuilder = (NameIDPolicyBuilder) registry.getBuilderFactory()
103+
.getBuilder(NameIDPolicy.DEFAULT_ELEMENT_NAME);
104+
Assert.notNull(this.nameIdPolicyBuilder, "nameIdPolicyBuilder must be configured in OpenSAML");
98105
}
99106

100107
void setRelayStateResolver(Converter<HttpServletRequest, String> relayStateResolver) {
@@ -130,6 +137,11 @@ <T extends AbstractSaml2AuthenticationRequest> T resolve(HttpServletRequest requ
130137
authnRequest.setIssuer(iss);
131138
authnRequest.setDestination(registration.getAssertingPartyDetails().getSingleSignOnServiceLocation());
132139
authnRequest.setAssertionConsumerServiceURL(registration.getAssertionConsumerServiceLocation());
140+
if (registration.getNameIdFormat() != null) {
141+
NameIDPolicy nameIdPolicy = this.nameIdPolicyBuilder.buildObject();
142+
nameIdPolicy.setFormat(registration.getNameIdFormat());
143+
authnRequest.setNameIDPolicy(nameIdPolicy);
144+
}
133145
authnRequestConsumer.accept(registration, authnRequest);
134146
if (authnRequest.getID() == null) {
135147
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-2020 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).credentials((c) -> c.add(signingCredential))
4444
.providerDetails((c) -> c.entityId(apEntityId).webSsoUrl(singleSignOnServiceLocation))

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())
@@ -75,6 +76,7 @@ public void resolveAuthenticationRequestWhenUnsignedRedirectThenRedirectsAndNoSi
7576
.assertingPartyDetails((party) -> party.wantAuthnRequestsSigned(false)).build();
7677
OpenSamlAuthenticationRequestResolver resolver = authenticationRequestResolver(registration);
7778
Saml2RedirectAuthenticationRequest result = resolver.resolve(request, (r, authnRequest) -> {
79+
assertThat(authnRequest.getNameIDPolicy().getFormat()).isEqualTo(registration.getNameIdFormat());
7880
assertThat(authnRequest.getAssertionConsumerServiceURL())
7981
.isEqualTo(registration.getAssertionConsumerServiceLocation());
8082
assertThat(authnRequest.getProtocolBinding())
@@ -110,6 +112,7 @@ public void resolveAuthenticationRequestWhenUnsignedPostThenOnlyPosts() {
110112
.build();
111113
OpenSamlAuthenticationRequestResolver resolver = authenticationRequestResolver(registration);
112114
Saml2PostAuthenticationRequest result = resolver.resolve(request, (r, authnRequest) -> {
115+
assertThat(authnRequest.getNameIDPolicy().getFormat()).isEqualTo(registration.getNameIdFormat());
113116
assertThat(authnRequest.getAssertionConsumerServiceURL())
114117
.isEqualTo(registration.getAssertionConsumerServiceLocation());
115118
assertThat(authnRequest.getProtocolBinding())
@@ -132,6 +135,7 @@ public void resolveAuthenticationRequestWhenSignedPostThenSignsAndPosts() {
132135
.assertingPartyDetails((party) -> party.singleSignOnServiceBinding(Saml2MessageBinding.POST)).build();
133136
OpenSamlAuthenticationRequestResolver resolver = authenticationRequestResolver(registration);
134137
Saml2PostAuthenticationRequest result = resolver.resolve(request, (r, authnRequest) -> {
138+
assertThat(authnRequest.getNameIDPolicy().getFormat()).isEqualTo(registration.getNameIdFormat());
135139
assertThat(authnRequest.getAssertionConsumerServiceURL())
136140
.isEqualTo(registration.getAssertionConsumerServiceLocation());
137141
assertThat(authnRequest.getProtocolBinding())

0 commit comments

Comments
 (0)