Skip to content

Commit f3fa8e8

Browse files
GitHanterjzheaux
authored andcommitted
Polish
Issue gh-9310
1 parent 6e41246 commit f3fa8e8

File tree

3 files changed

+10
-37
lines changed

3 files changed

+10
-37
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurerTests.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616

1717
package org.springframework.security.config.annotation.web.configurers.saml2;
1818

19-
import java.io.ByteArrayOutputStream;
2019
import java.io.IOException;
2120
import java.net.URLDecoder;
22-
import java.nio.charset.StandardCharsets;
2321
import java.time.Duration;
24-
import java.util.Arrays;
2522
import java.util.Base64;
2623
import java.util.Collection;
2724
import java.util.Collections;
28-
import java.util.zip.Inflater;
29-
import java.util.zip.InflaterOutputStream;
3025

3126
import javax.servlet.ServletException;
3227
import javax.servlet.http.HttpServletRequest;
@@ -63,7 +58,6 @@
6358
import org.springframework.security.core.GrantedAuthority;
6459
import org.springframework.security.core.authority.SimpleGrantedAuthority;
6560
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
66-
import org.springframework.security.saml2.Saml2Exception;
6761
import org.springframework.security.saml2.core.Saml2ErrorCodes;
6862
import org.springframework.security.saml2.core.Saml2Utils;
6963
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
@@ -112,10 +106,10 @@
112106
public class Saml2LoginConfigurerTests {
113107

114108
private static final Converter<Assertion, Collection<? extends GrantedAuthority>> AUTHORITIES_EXTRACTOR = (
115-
a) -> Arrays.asList(new SimpleGrantedAuthority("TEST"));
109+
a) -> Collections.singletonList(new SimpleGrantedAuthority("TEST"));
116110

117-
private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = (authorities) -> Arrays
118-
.asList(new SimpleGrantedAuthority("TEST CONVERTED"));
111+
private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = (authorities) -> Collections
112+
.singletonList(new SimpleGrantedAuthority("TEST CONVERTED"));
119113

120114
private static final Duration RESPONSE_TIME_VALIDATION_SKEW = Duration.ZERO;
121115

@@ -194,7 +188,7 @@ public void authenticationRequestWhenAuthnRequestContextConverterThenUses() thro
194188
UriComponents components = UriComponentsBuilder.fromHttpUrl(result.getResponse().getRedirectedUrl()).build();
195189
String samlRequest = components.getQueryParams().getFirst("SAMLRequest");
196190
String decoded = URLDecoder.decode(samlRequest, "UTF-8");
197-
String inflated = samlInflate(samlDecode(decoded));
191+
String inflated = Saml2Utils.samlInflate(Saml2Utils.samlDecode(decoded));
198192
assertThat(inflated).contains("ForceAuthn=\"true\"");
199193
}
200194

@@ -205,7 +199,7 @@ public void authenticateWhenCustomAuthenticationConverterThenUses() throws Excep
205199
.assertingPartyDetails((party) -> party.verificationX509Credentials(
206200
(c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential())))
207201
.build();
208-
String response = new String(samlDecode(SIGNED_RESPONSE));
202+
String response = new String(Saml2Utils.samlDecode(SIGNED_RESPONSE));
209203
given(CustomAuthenticationConverter.authenticationConverter.convert(any(HttpServletRequest.class)))
210204
.willReturn(new Saml2AuthenticationToken(relyingPartyRegistration, response));
211205
// @formatter:off
@@ -268,26 +262,6 @@ private void performSaml2Login(String expected) throws IOException, ServletExcep
268262
.hasToString(expected);
269263
}
270264

271-
private static org.apache.commons.codec.binary.Base64 BASE64 = new org.apache.commons.codec.binary.Base64(0,
272-
new byte[] { '\n' });
273-
274-
private static byte[] samlDecode(String s) {
275-
return BASE64.decode(s);
276-
}
277-
278-
private static String samlInflate(byte[] b) {
279-
try {
280-
ByteArrayOutputStream out = new ByteArrayOutputStream();
281-
InflaterOutputStream iout = new InflaterOutputStream(out, new Inflater(true));
282-
iout.write(b);
283-
iout.finish();
284-
return new String(out.toByteArray(), StandardCharsets.UTF_8);
285-
}
286-
catch (IOException ex) {
287-
throw new Saml2Exception("Unable to inflate string", ex);
288-
}
289-
}
290-
291265
private static AuthenticationManager getAuthenticationManagerMock(String role) {
292266
return new AuthenticationManager() {
293267
@Override

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.security.saml2.provider.service.web;
1818

1919
import java.io.ByteArrayOutputStream;
20-
import java.io.IOException;
2120
import java.nio.charset.StandardCharsets;
2221
import java.util.zip.Inflater;
2322
import java.util.zip.InflaterOutputStream;
@@ -84,9 +83,9 @@ private String inflateIfRequired(HttpServletRequest request, byte[] b) {
8483
return new String(b, StandardCharsets.UTF_8);
8584
}
8685

87-
private byte[] samlDecode(String s) {
86+
private byte[] samlDecode(String base64EncodedPayload) {
8887
try {
89-
return BASE64.decode(s);
88+
return BASE64.decode(base64EncodedPayload);
9089
}
9190
catch (Exception ex) {
9291
throw new Saml2AuthenticationException(
@@ -100,7 +99,7 @@ private String samlInflate(byte[] b) {
10099
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(out, new Inflater(true));
101100
inflaterOutputStream.write(b);
102101
inflaterOutputStream.finish();
103-
return new String(out.toByteArray(), StandardCharsets.UTF_8);
102+
return out.toString(StandardCharsets.UTF_8.name());
104103
}
105104
catch (Exception ex) {
106105
throw new Saml2AuthenticationException(

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/core/Saml2Utils.java

Lines changed: 2 additions & 2 deletions
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-2021 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.
@@ -63,7 +63,7 @@ public static String samlInflate(byte[] b) {
6363
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(out, new Inflater(true));
6464
inflaterOutputStream.write(b);
6565
inflaterOutputStream.finish();
66-
return new String(out.toByteArray(), StandardCharsets.UTF_8);
66+
return out.toString(StandardCharsets.UTF_8.name());
6767
}
6868
catch (IOException ex) {
6969
throw new Saml2Exception("Unable to inflate string", ex);

0 commit comments

Comments
 (0)