Skip to content

Commit c7ea40a

Browse files
committed
Add nameIdFormat processing to OpenSamlAuthenticationRequestResolver
Closes gh-12825
1 parent 84cca81 commit c7ea40a

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

+13-1
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.security.saml2.Saml2Exception;
@@ -72,6 +74,8 @@ class OpenSamlAuthenticationRequestResolver {
7274

7375
private final NameIDBuilder nameIdBuilder;
7476

77+
private final NameIDPolicyBuilder nameIdPolicyBuilder;
78+
7579
/**
7680
* Construct a {@link OpenSamlAuthenticationRequestResolver} using the provided
7781
* parameters
@@ -92,6 +96,9 @@ class OpenSamlAuthenticationRequestResolver {
9296
Assert.notNull(this.issuerBuilder, "issuerBuilder must be configured in OpenSAML");
9397
this.nameIdBuilder = (NameIDBuilder) registry.getBuilderFactory().getBuilder(NameID.DEFAULT_ELEMENT_NAME);
9498
Assert.notNull(this.nameIdBuilder, "nameIdBuilder must be configured in OpenSAML");
99+
this.nameIdPolicyBuilder = (NameIDPolicyBuilder) registry.getBuilderFactory()
100+
.getBuilder(NameIDPolicy.DEFAULT_ELEMENT_NAME);
101+
Assert.notNull(this.nameIdPolicyBuilder, "nameIdPolicyBuilder must be configured in OpenSAML");
95102
}
96103

97104
<T extends AbstractSaml2AuthenticationRequest> T resolve(HttpServletRequest request) {
@@ -119,6 +126,11 @@ <T extends AbstractSaml2AuthenticationRequest> T resolve(HttpServletRequest requ
119126
authnRequest.setIssuer(iss);
120127
authnRequest.setDestination(registration.getAssertingPartyDetails().getSingleSignOnServiceLocation());
121128
authnRequest.setAssertionConsumerServiceURL(registration.getAssertionConsumerServiceLocation());
129+
if (registration.getNameIdFormat() != null) {
130+
NameIDPolicy nameIdPolicy = this.nameIdPolicyBuilder.buildObject();
131+
nameIdPolicy.setFormat(registration.getNameIdFormat());
132+
authnRequest.setNameIDPolicy(nameIdPolicy);
133+
}
122134
authnRequestConsumer.accept(registration, authnRequest);
123135
if (authnRequest.getID() == null) {
124136
authnRequest.setID("ARQ" + UUID.randomUUID().toString().substring(1));

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

+2-2
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

+5-1
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)