diff --git a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurerTests.java b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurerTests.java index be71694088..4d79181b3f 100644 --- a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurerTests.java +++ b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurerTests.java @@ -16,17 +16,12 @@ package org.springframework.security.config.annotation.web.configurers.saml2; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; import java.time.Duration; -import java.util.Arrays; import java.util.Base64; import java.util.Collection; import java.util.Collections; -import java.util.zip.Inflater; -import java.util.zip.InflaterOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -63,7 +58,6 @@ import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; -import org.springframework.security.saml2.Saml2Exception; import org.springframework.security.saml2.core.Saml2ErrorCodes; import org.springframework.security.saml2.core.Saml2Utils; import org.springframework.security.saml2.core.TestSaml2X509Credentials; @@ -112,10 +106,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class Saml2LoginConfigurerTests { private static final Converter> AUTHORITIES_EXTRACTOR = ( - a) -> Arrays.asList(new SimpleGrantedAuthority("TEST")); + a) -> Collections.singletonList(new SimpleGrantedAuthority("TEST")); - private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = (authorities) -> Arrays - .asList(new SimpleGrantedAuthority("TEST CONVERTED")); + private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = (authorities) -> Collections + .singletonList(new SimpleGrantedAuthority("TEST CONVERTED")); private static final Duration RESPONSE_TIME_VALIDATION_SKEW = Duration.ZERO; @@ -194,7 +188,7 @@ public class Saml2LoginConfigurerTests { UriComponents components = UriComponentsBuilder.fromHttpUrl(result.getResponse().getRedirectedUrl()).build(); String samlRequest = components.getQueryParams().getFirst("SAMLRequest"); String decoded = URLDecoder.decode(samlRequest, "UTF-8"); - String inflated = samlInflate(samlDecode(decoded)); + String inflated = Saml2Utils.samlInflate(Saml2Utils.samlDecode(decoded)); assertThat(inflated).contains("ForceAuthn=\"true\""); } @@ -205,7 +199,7 @@ public class Saml2LoginConfigurerTests { .assertingPartyDetails((party) -> party.verificationX509Credentials( (c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential()))) .build(); - String response = new String(samlDecode(SIGNED_RESPONSE)); + String response = new String(Saml2Utils.samlDecode(SIGNED_RESPONSE)); given(CustomAuthenticationConverter.authenticationConverter.convert(any(HttpServletRequest.class))) .willReturn(new Saml2AuthenticationToken(relyingPartyRegistration, response)); // @formatter:off @@ -268,26 +262,6 @@ public class Saml2LoginConfigurerTests { .hasToString(expected); } - private static org.apache.commons.codec.binary.Base64 BASE64 = new org.apache.commons.codec.binary.Base64(0, - new byte[] { '\n' }); - - private static byte[] samlDecode(String s) { - return BASE64.decode(s); - } - - private static String samlInflate(byte[] b) { - try { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - InflaterOutputStream iout = new InflaterOutputStream(out, new Inflater(true)); - iout.write(b); - iout.finish(); - return new String(out.toByteArray(), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new Saml2Exception("Unable to inflate string", ex); - } - } - private static AuthenticationManager getAuthenticationManagerMock(String role) { return new AuthenticationManager() { @Override diff --git a/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverter.java b/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverter.java index 9a43a880cf..274f78617f 100644 --- a/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverter.java +++ b/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverter.java @@ -17,7 +17,6 @@ package org.springframework.security.saml2.provider.service.web; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.zip.Inflater; import java.util.zip.InflaterOutputStream; @@ -84,9 +83,9 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo return new String(b, StandardCharsets.UTF_8); } - private byte[] samlDecode(String s) { + private byte[] samlDecode(String base64EncodedPayload) { try { - return BASE64.decode(s); + return BASE64.decode(base64EncodedPayload); } catch (Exception ex) { throw new Saml2AuthenticationException( @@ -100,7 +99,7 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(out, new Inflater(true)); inflaterOutputStream.write(b); inflaterOutputStream.finish(); - return new String(out.toByteArray(), StandardCharsets.UTF_8); + return out.toString(StandardCharsets.UTF_8.name()); } catch (Exception ex) { throw new Saml2AuthenticationException( diff --git a/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/core/Saml2Utils.java b/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/core/Saml2Utils.java index a518b911a3..6f5d9e48d0 100644 --- a/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/core/Saml2Utils.java +++ b/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/core/Saml2Utils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,7 +63,7 @@ public final class Saml2Utils { InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(out, new Inflater(true)); inflaterOutputStream.write(b); inflaterOutputStream.finish(); - return new String(out.toByteArray(), StandardCharsets.UTF_8); + return out.toString(StandardCharsets.UTF_8.name()); } catch (IOException ex) { throw new Saml2Exception("Unable to inflate string", ex);