Skip to content

Commit e6186a1

Browse files
Add shouldInflate property
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent a8edcca commit e6186a1

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

+17-5
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-2025 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.
@@ -43,6 +43,8 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo
4343

4444
private Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> authenticationRequestRepository;
4545

46+
private Boolean shouldInflate;
47+
4648
/**
4749
* Constructs a {@link Saml2AuthenticationTokenConverter} given a strategy for
4850
* resolving {@link RelyingPartyRegistration}s
@@ -86,16 +88,26 @@ public void setAuthenticationRequestRepository(
8688
this.authenticationRequestRepository = authenticationRequestRepository;
8789
}
8890

91+
/**
92+
* Use the given {@code shouldInflate} to inflate request.
93+
* @param shouldInflate the {@code shouldInflate} to use
94+
* @since 7.0
95+
*/
96+
public void setShouldInflateResponse(boolean shouldInflate) {
97+
this.shouldInflate = shouldInflate;
98+
}
99+
89100
private String decode(HttpServletRequest request) {
101+
// prevent to break passivity in Saml2LoginBeanDefinitionParserTests
102+
if (this.shouldInflate == null) {
103+
this.shouldInflate = HttpMethod.GET.matches(request.getMethod());
104+
}
90105
String encoded = request.getParameter(Saml2ParameterNames.SAML_RESPONSE);
91106
if (encoded == null) {
92107
return null;
93108
}
94109
try {
95-
return Saml2Utils.withEncoded(encoded)
96-
.requireBase64(true)
97-
.inflate(HttpMethod.GET.matches(request.getMethod()))
98-
.decode();
110+
return Saml2Utils.withEncoded(encoded).requireBase64(true).inflate(this.shouldInflate).decode();
99111
}
100112
catch (Exception ex) {
101113
throw new Saml2AuthenticationException(new Saml2Error(Saml2ErrorCodes.INVALID_RESPONSE, ex.getMessage()),

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -61,6 +61,7 @@ public class Saml2AuthenticationTokenConverterTests {
6161
public void convertWhenSamlResponseThenToken() {
6262
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(
6363
this.relyingPartyRegistrationResolver);
64+
converter.setShouldInflateResponse(false);
6465
given(this.relyingPartyRegistrationResolver.resolve(any(HttpServletRequest.class), any()))
6566
.willReturn(this.relyingPartyRegistration);
6667
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -76,6 +77,7 @@ public void convertWhenSamlResponseThenToken() {
7677
public void convertWhenSamlResponseWithRelyingPartyRegistrationResolver(
7778
@Mock RelyingPartyRegistrationResolver resolver) {
7879
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(resolver);
80+
converter.setShouldInflateResponse(false);
7981
given(resolver.resolve(any(HttpServletRequest.class), any())).willReturn(this.relyingPartyRegistration);
8082
MockHttpServletRequest request = new MockHttpServletRequest();
8183
request.setParameter(Saml2ParameterNames.SAML_RESPONSE,
@@ -161,6 +163,7 @@ public void convertWhenGetRequestInvalidDeflatedThenSaml2AuthenticationException
161163
public void convertWhenUsingSamlUtilsBase64ThenXmlIsValid() throws Exception {
162164
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(
163165
this.relyingPartyRegistrationResolver);
166+
converter.setShouldInflateResponse(false);
164167
given(this.relyingPartyRegistrationResolver.resolve(any(HttpServletRequest.class), any()))
165168
.willReturn(this.relyingPartyRegistration);
166169
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -178,6 +181,7 @@ public void convertWhenSavedAuthenticationRequestThenToken() {
178181
.willReturn(this.relyingPartyRegistration.getRegistrationId());
179182
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(
180183
this.relyingPartyRegistrationResolver);
184+
converter.setShouldInflateResponse(false);
181185
converter.setAuthenticationRequestRepository(authenticationRequestRepository);
182186
given(this.relyingPartyRegistrationResolver.resolve(any(HttpServletRequest.class), any()))
183187
.willReturn(this.relyingPartyRegistration);
@@ -203,6 +207,7 @@ public void convertWhenSavedAuthenticationRequestThenTokenWithRelyingPartyRegist
203207
.willReturn(this.relyingPartyRegistration.getRegistrationId());
204208
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(resolver);
205209
converter.setAuthenticationRequestRepository(authenticationRequestRepository);
210+
converter.setShouldInflateResponse(false);
206211
given(resolver.resolve(any(HttpServletRequest.class), any())).willReturn(this.relyingPartyRegistration);
207212
given(authenticationRequestRepository.loadAuthenticationRequest(any(HttpServletRequest.class)))
208213
.willReturn(authenticationRequest);

0 commit comments

Comments
 (0)