Skip to content

Commit 0793bcf

Browse files
Add shouldInflate property
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 52394c1 commit 0793bcf

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

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

Lines changed: 13 additions & 6 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-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.
@@ -18,7 +18,6 @@
1818

1919
import jakarta.servlet.http.HttpServletRequest;
2020

21-
import org.springframework.http.HttpMethod;
2221
import org.springframework.security.saml2.core.Saml2Error;
2322
import org.springframework.security.saml2.core.Saml2ErrorCodes;
2423
import org.springframework.security.saml2.core.Saml2ParameterNames;
@@ -43,6 +42,8 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo
4342

4443
private Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> authenticationRequestRepository;
4544

45+
private boolean shouldInflate = true;
46+
4647
/**
4748
* Constructs a {@link Saml2AuthenticationTokenConverter} given a strategy for
4849
* resolving {@link RelyingPartyRegistration}s
@@ -86,16 +87,22 @@ public void setAuthenticationRequestRepository(
8687
this.authenticationRequestRepository = authenticationRequestRepository;
8788
}
8889

90+
/**
91+
* Use the given {@code shouldInflate} to inflate request. Default is {@code true}.
92+
* @param shouldInflate the {@code shouldInflate} to use
93+
* @since 7.0
94+
*/
95+
public void setShouldInflateResponse(boolean shouldInflate) {
96+
this.shouldInflate = shouldInflate;
97+
}
98+
8999
private String decode(HttpServletRequest request) {
90100
String encoded = request.getParameter(Saml2ParameterNames.SAML_RESPONSE);
91101
if (encoded == null) {
92102
return null;
93103
}
94104
try {
95-
return Saml2Utils.withEncoded(encoded)
96-
.requireBase64(true)
97-
.inflate(HttpMethod.GET.matches(request.getMethod()))
98-
.decode();
105+
return Saml2Utils.withEncoded(encoded).requireBase64(true).inflate(this.shouldInflate).decode();
99106
}
100107
catch (Exception ex) {
101108
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

Lines changed: 18 additions & 1 deletion
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.
@@ -230,6 +230,23 @@ public void setAuthenticationRequestRepositoryWhenNullThenIllegalArgument() {
230230
.isThrownBy(() -> converter.setAuthenticationRequestRepository(null));
231231
}
232232

233+
@Test
234+
public void convertWhenGetRequestWithDeflateConverter() {
235+
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(
236+
this.relyingPartyRegistrationResolver);
237+
converter.setShouldInflateResponse(false);
238+
given(this.relyingPartyRegistrationResolver.resolve(any(HttpServletRequest.class), any()))
239+
.willReturn(this.relyingPartyRegistration);
240+
MockHttpServletRequest request = new MockHttpServletRequest();
241+
request.setMethod("GET");
242+
request.setParameter(Saml2ParameterNames.SAML_RESPONSE,
243+
Saml2Utils.samlEncode("response".getBytes(StandardCharsets.UTF_8)));
244+
Saml2AuthenticationToken token = converter.convert(request);
245+
assertThat(token.getSaml2Response()).isEqualTo("response");
246+
assertThat(token.getRelyingPartyRegistration().getRegistrationId())
247+
.isEqualTo(this.relyingPartyRegistration.getRegistrationId());
248+
}
249+
233250
private void validateSsoCircleXml(String xml) {
234251
assertThat(xml).contains("InResponseTo=\"ARQ9a73ead-7dcf-45a8-89eb-26f3c9900c36\"")
235252
.contains(" ID=\"s246d157446618e90e43fb79bdd4d9e9e19cf2c7c4\"")

0 commit comments

Comments
 (0)