Skip to content

Commit fc653bb

Browse files
houssembajzheaux
authored andcommitted
make SAML authentication request uri configurable
Closes gh-10840
1 parent 759d799 commit fc653bb

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ class OpenSamlAuthenticationRequestResolver {
6161
OpenSamlInitializationService.initialize();
6262
}
6363

64-
private final RequestMatcher requestMatcher = new AntPathRequestMatcher("/saml2/authenticate/{registrationId}");
65-
6664
private final RelyingPartyRegistrationResolver relyingPartyRegistrationResolver;
6765

6866
private final AuthnRequestBuilder authnRequestBuilder;
@@ -73,6 +71,8 @@ class OpenSamlAuthenticationRequestResolver {
7371

7472
private final NameIDBuilder nameIdBuilder;
7573

74+
private RequestMatcher requestMatcher = new AntPathRequestMatcher("/saml2/authenticate/{registrationId}");
75+
7676
private Converter<HttpServletRequest, String> relayStateResolver = (request) -> UUID.randomUUID().toString();
7777

7878
/**
@@ -101,6 +101,10 @@ void setRelayStateResolver(Converter<HttpServletRequest, String> relayStateResol
101101
this.relayStateResolver = relayStateResolver;
102102
}
103103

104+
void setRequestMatcher(RequestMatcher requestMatcher) {
105+
this.requestMatcher = requestMatcher;
106+
}
107+
104108
<T extends AbstractSaml2AuthenticationRequest> T resolve(HttpServletRequest request) {
105109
return resolve(request, (registration, logoutRequest) -> {
106110
});

saml2/saml2-service-provider/src/opensaml4Main/java/org/springframework/security/saml2/provider/service/web/authentication/OpenSaml4AuthenticationRequestResolver.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
2929
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
3030
import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver;
31+
import org.springframework.security.web.util.matcher.RequestMatcher;
3132
import org.springframework.util.Assert;
3233

3334
/**
@@ -70,6 +71,19 @@ public void setAuthnRequestCustomizer(Consumer<AuthnRequestContext> contextConsu
7071
this.contextConsumer = contextConsumer;
7172
}
7273

74+
/**
75+
* Set the {@link RequestMatcher} to use for setting the
76+
* {@link OpenSamlAuthenticationRequestResolver#setRequestMatcher(RequestMatcher)}
77+
* (RequestMatcher)}
78+
* @param requestMatcher the {@link RequestMatcher} to identify authentication
79+
* requests.
80+
* @since 5.8
81+
*/
82+
public void setRequestMatcher(RequestMatcher requestMatcher) {
83+
Assert.notNull(requestMatcher, "requestMatcher cannot be null");
84+
this.authnRequestResolver.setRequestMatcher(requestMatcher);
85+
}
86+
7387
/**
7488
* Use this {@link Clock} for generating the issued {@link Instant}
7589
* @param clock the {@link Clock} to use

saml2/saml2-service-provider/src/opensaml4Test/java/org/springframework/security/saml2/provider/service/web/authentication/OpenSaml4AuthenticationRequestResolverTests.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding;
3030
import org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations;
3131
import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver;
32+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
3435
import static org.mockito.ArgumentMatchers.any;
@@ -44,8 +45,7 @@ public class OpenSaml4AuthenticationRequestResolverTests {
4445

4546
@BeforeEach
4647
void setup() {
47-
this.request = new MockHttpServletRequest();
48-
this.request.setServletPath("/saml2/authenticate/registration-id");
48+
this.request = givenRequest("/saml2/authenticate/registration-id");
4949
this.registration = TestRelyingPartyRegistrations.full().build();
5050
}
5151

@@ -86,4 +86,25 @@ void resolveWhenCustomRelayStateThenUses() {
8686
verify(relayState).convert(any());
8787
}
8888

89+
@Test
90+
void resolveWhenCustomAuthenticationUrlTHenUses() {
91+
RelyingPartyRegistrationResolver relyingParties = mock(RelyingPartyRegistrationResolver.class);
92+
given(relyingParties.resolve(any(), any())).willReturn(this.registration);
93+
OpenSaml4AuthenticationRequestResolver resolver = new OpenSaml4AuthenticationRequestResolver(relyingParties);
94+
resolver.setRequestMatcher(new AntPathRequestMatcher("/custom/authentication/{registrationId}"));
95+
Saml2RedirectAuthenticationRequest authnRequest = resolver
96+
.resolve(givenRequest("/custom/authentication/registration-id"));
97+
98+
assertThat(authnRequest.getBinding()).isEqualTo(Saml2MessageBinding.REDIRECT);
99+
assertThat(authnRequest.getAuthenticationRequestUri())
100+
.isEqualTo(this.registration.getAssertingPartyDetails().getSingleSignOnServiceLocation());
101+
102+
}
103+
104+
private MockHttpServletRequest givenRequest(String path) {
105+
MockHttpServletRequest request = new MockHttpServletRequest();
106+
request.setServletPath(path);
107+
return request;
108+
}
109+
89110
}

0 commit comments

Comments
 (0)