mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-01 09:42:13 +00:00
Polish
Issue gh-9310
This commit is contained in:
parent
6e41246a2b
commit
f3fa8e8800
@ -16,17 +16,12 @@
|
|||||||
|
|
||||||
package org.springframework.security.config.annotation.web.configurers.saml2;
|
package org.springframework.security.config.annotation.web.configurers.saml2;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.zip.Inflater;
|
|
||||||
import java.util.zip.InflaterOutputStream;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
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.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
|
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.Saml2ErrorCodes;
|
||||||
import org.springframework.security.saml2.core.Saml2Utils;
|
import org.springframework.security.saml2.core.Saml2Utils;
|
||||||
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
|
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
|
||||||
@ -112,10 +106,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
public class Saml2LoginConfigurerTests {
|
public class Saml2LoginConfigurerTests {
|
||||||
|
|
||||||
private static final Converter<Assertion, Collection<? extends GrantedAuthority>> AUTHORITIES_EXTRACTOR = (
|
private static final Converter<Assertion, Collection<? extends GrantedAuthority>> AUTHORITIES_EXTRACTOR = (
|
||||||
a) -> Arrays.asList(new SimpleGrantedAuthority("TEST"));
|
a) -> Collections.singletonList(new SimpleGrantedAuthority("TEST"));
|
||||||
|
|
||||||
private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = (authorities) -> Arrays
|
private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = (authorities) -> Collections
|
||||||
.asList(new SimpleGrantedAuthority("TEST CONVERTED"));
|
.singletonList(new SimpleGrantedAuthority("TEST CONVERTED"));
|
||||||
|
|
||||||
private static final Duration RESPONSE_TIME_VALIDATION_SKEW = Duration.ZERO;
|
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();
|
UriComponents components = UriComponentsBuilder.fromHttpUrl(result.getResponse().getRedirectedUrl()).build();
|
||||||
String samlRequest = components.getQueryParams().getFirst("SAMLRequest");
|
String samlRequest = components.getQueryParams().getFirst("SAMLRequest");
|
||||||
String decoded = URLDecoder.decode(samlRequest, "UTF-8");
|
String decoded = URLDecoder.decode(samlRequest, "UTF-8");
|
||||||
String inflated = samlInflate(samlDecode(decoded));
|
String inflated = Saml2Utils.samlInflate(Saml2Utils.samlDecode(decoded));
|
||||||
assertThat(inflated).contains("ForceAuthn=\"true\"");
|
assertThat(inflated).contains("ForceAuthn=\"true\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +199,7 @@ public class Saml2LoginConfigurerTests {
|
|||||||
.assertingPartyDetails((party) -> party.verificationX509Credentials(
|
.assertingPartyDetails((party) -> party.verificationX509Credentials(
|
||||||
(c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential())))
|
(c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential())))
|
||||||
.build();
|
.build();
|
||||||
String response = new String(samlDecode(SIGNED_RESPONSE));
|
String response = new String(Saml2Utils.samlDecode(SIGNED_RESPONSE));
|
||||||
given(CustomAuthenticationConverter.authenticationConverter.convert(any(HttpServletRequest.class)))
|
given(CustomAuthenticationConverter.authenticationConverter.convert(any(HttpServletRequest.class)))
|
||||||
.willReturn(new Saml2AuthenticationToken(relyingPartyRegistration, response));
|
.willReturn(new Saml2AuthenticationToken(relyingPartyRegistration, response));
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
@ -268,26 +262,6 @@ public class Saml2LoginConfigurerTests {
|
|||||||
.hasToString(expected);
|
.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) {
|
private static AuthenticationManager getAuthenticationManagerMock(String role) {
|
||||||
return new AuthenticationManager() {
|
return new AuthenticationManager() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
package org.springframework.security.saml2.provider.service.web;
|
package org.springframework.security.saml2.provider.service.web;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.zip.Inflater;
|
import java.util.zip.Inflater;
|
||||||
import java.util.zip.InflaterOutputStream;
|
import java.util.zip.InflaterOutputStream;
|
||||||
@ -84,9 +83,9 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo
|
|||||||
return new String(b, StandardCharsets.UTF_8);
|
return new String(b, StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] samlDecode(String s) {
|
private byte[] samlDecode(String base64EncodedPayload) {
|
||||||
try {
|
try {
|
||||||
return BASE64.decode(s);
|
return BASE64.decode(base64EncodedPayload);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new Saml2AuthenticationException(
|
throw new Saml2AuthenticationException(
|
||||||
@ -100,7 +99,7 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo
|
|||||||
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(out, new Inflater(true));
|
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(out, new Inflater(true));
|
||||||
inflaterOutputStream.write(b);
|
inflaterOutputStream.write(b);
|
||||||
inflaterOutputStream.finish();
|
inflaterOutputStream.finish();
|
||||||
return new String(out.toByteArray(), StandardCharsets.UTF_8);
|
return out.toString(StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new Saml2AuthenticationException(
|
throw new Saml2AuthenticationException(
|
||||||
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 inflaterOutputStream = new InflaterOutputStream(out, new Inflater(true));
|
||||||
inflaterOutputStream.write(b);
|
inflaterOutputStream.write(b);
|
||||||
inflaterOutputStream.finish();
|
inflaterOutputStream.finish();
|
||||||
return new String(out.toByteArray(), StandardCharsets.UTF_8);
|
return out.toString(StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new Saml2Exception("Unable to inflate string", ex);
|
throw new Saml2Exception("Unable to inflate string", ex);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user