Skip to content

Commit 4d7e97b

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

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

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

Lines changed: 14 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 shouldInflateResponse = true;
46+
4647
/**
4748
* Constructs a {@link Saml2AuthenticationTokenConverter} given a strategy for
4849
* resolving {@link RelyingPartyRegistration}s
@@ -86,16 +87,23 @@ public void setAuthenticationRequestRepository(
8687
this.authenticationRequestRepository = authenticationRequestRepository;
8788
}
8889

90+
/**
91+
* Use the given {@code shouldInflateResponse} to inflate request. Default is
92+
* {@code true}.
93+
* @param shouldInflateResponse the {@code shouldInflateResponse} to use
94+
* @since 7.0
95+
*/
96+
public void setShouldInflateResponse(boolean shouldInflateResponse) {
97+
this.shouldInflateResponse = shouldInflateResponse;
98+
}
99+
89100
private String decode(HttpServletRequest request) {
90101
String encoded = request.getParameter(Saml2ParameterNames.SAML_RESPONSE);
91102
if (encoded == null) {
92103
return null;
93104
}
94105
try {
95-
return Saml2Utils.withEncoded(encoded)
96-
.requireBase64(true)
97-
.inflate(HttpMethod.GET.matches(request.getMethod()))
98-
.decode();
106+
return Saml2Utils.withEncoded(encoded).requireBase64(true).inflate(this.shouldInflateResponse).decode();
99107
}
100108
catch (Exception ex) {
101109
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 convertWhenGetRequestAndShouldNotInflateResponse() {
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)