3 changed files with 218 additions and 4 deletions
@ -0,0 +1,91 @@
@@ -0,0 +1,91 @@
|
||||
/* |
||||
* Copyright 2002-2019 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.saml2.provider.service.servlet.filter; |
||||
|
||||
import java.io.IOException; |
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.mock.web.MockFilterChain; |
||||
import org.springframework.mock.web.MockHttpServletRequest; |
||||
import org.springframework.mock.web.MockHttpServletResponse; |
||||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
||||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository; |
||||
|
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.when; |
||||
import static org.springframework.security.saml2.provider.service.servlet.filter.TestSaml2SigningCredentials.signingCredential; |
||||
|
||||
public class Saml2WebSsoAuthenticationRequestFilterTests { |
||||
|
||||
private Saml2WebSsoAuthenticationRequestFilter filter; |
||||
private RelyingPartyRegistrationRepository repository = mock(RelyingPartyRegistrationRepository.class); |
||||
private MockHttpServletRequest request; |
||||
private HttpServletResponse response; |
||||
private MockFilterChain filterChain; |
||||
|
||||
@Before |
||||
public void setup() { |
||||
filter = new Saml2WebSsoAuthenticationRequestFilter(repository); |
||||
request = new MockHttpServletRequest(); |
||||
response = new MockHttpServletResponse(); |
||||
request.setPathInfo("/saml2/authenticate/registration-id"); |
||||
|
||||
filterChain = new MockFilterChain(); |
||||
} |
||||
|
||||
@Test |
||||
public void createSamlRequestRedirectUrlAndReturnUrlWithoutRelayState() throws ServletException, IOException { |
||||
RelyingPartyRegistration relyingPartyRegistration = RelyingPartyRegistration |
||||
.withRegistrationId("registration-id") |
||||
.remoteIdpEntityId("idp-entity-id") |
||||
.idpWebSsoUrl("sso-url") |
||||
.assertionConsumerServiceUrlTemplate("template") |
||||
.credentials(c -> c.add(signingCredential())) |
||||
.build(); |
||||
|
||||
when(repository.findByRegistrationId("registration-id")) |
||||
.thenReturn(relyingPartyRegistration); |
||||
|
||||
filter.doFilterInternal(request, response, filterChain); |
||||
|
||||
Assert.assertFalse(response.getHeader("Location").contains("RelayState=")); |
||||
} |
||||
|
||||
@Test |
||||
public void createSamlRequestRedirectUrlAndReturnUrlWithRelayState() throws ServletException, IOException { |
||||
RelyingPartyRegistration relyingPartyRegistration = RelyingPartyRegistration |
||||
.withRegistrationId("registration-id") |
||||
.remoteIdpEntityId("idp-entity-id") |
||||
.idpWebSsoUrl("sso-url") |
||||
.assertionConsumerServiceUrlTemplate("template") |
||||
.credentials(c -> c.add(signingCredential())) |
||||
.build(); |
||||
|
||||
when(repository.findByRegistrationId("registration-id")) |
||||
.thenReturn(relyingPartyRegistration); |
||||
|
||||
request.setParameter("RelayState", "my-relay-state"); |
||||
|
||||
filter.doFilterInternal(request, response, filterChain); |
||||
|
||||
Assert.assertTrue(response.getHeader("Location").contains("RelayState=my-relay-state")); |
||||
} |
||||
} |
||||
@ -0,0 +1,118 @@
@@ -0,0 +1,118 @@
|
||||
/* |
||||
* Copyright 2002-2019 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.saml2.provider.service.servlet.filter; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.security.KeyException; |
||||
import java.security.PrivateKey; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.CertificateFactory; |
||||
import java.security.cert.X509Certificate; |
||||
|
||||
import org.opensaml.security.crypto.KeySupport; |
||||
import org.springframework.security.saml2.Saml2Exception; |
||||
import org.springframework.security.saml2.credentials.Saml2X509Credential; |
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8; |
||||
import static org.springframework.security.saml2.credentials.Saml2X509Credential.Saml2X509CredentialType.DECRYPTION; |
||||
import static org.springframework.security.saml2.credentials.Saml2X509Credential.Saml2X509CredentialType.SIGNING; |
||||
|
||||
final class TestSaml2SigningCredentials { |
||||
|
||||
static Saml2X509Credential signingCredential() { |
||||
return new Saml2X509Credential(idpPrivateKey(), idpCertificate(), SIGNING, DECRYPTION); |
||||
} |
||||
|
||||
private static X509Certificate certificate(String cert) { |
||||
ByteArrayInputStream certBytes = new ByteArrayInputStream(cert.getBytes()); |
||||
try { |
||||
return (X509Certificate) CertificateFactory |
||||
.getInstance("X.509") |
||||
.generateCertificate(certBytes); |
||||
} |
||||
catch (CertificateException e) { |
||||
throw new Saml2Exception(e); |
||||
} |
||||
} |
||||
|
||||
private static PrivateKey privateKey(String key) { |
||||
try { |
||||
return KeySupport.decodePrivateKey(key.getBytes(UTF_8), new char[0]); |
||||
} |
||||
catch (KeyException e) { |
||||
throw new Saml2Exception(e); |
||||
} |
||||
} |
||||
|
||||
private static X509Certificate idpCertificate() { |
||||
return certificate("-----BEGIN CERTIFICATE-----\n" |
||||
+ "MIIEEzCCAvugAwIBAgIJAIc1qzLrv+5nMA0GCSqGSIb3DQEBCwUAMIGfMQswCQYD\n" |
||||
+ "VQQGEwJV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NGzAfBgNVHSMEGDAWgBTTyP6Cc5HlBJ5+ucVCwGc5ogKNGzAMBgNVHRMEBTAD\n" |
||||
+ "AQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAvMS4EQeP/ipV4jOG5lO6/tYCb/iJeAduO\n" |
||||
+ "nRhkJk0DbX329lDLZhTTL/x/w/9muCVcvLrzEp6PN+VWfw5E5FWtZN0yhGtP9R+v\n" |
||||
+ "ZnrV+oc2zGD+no1/ySFOe3EiJCO5dehxKjYEmBRv5sU/LZFKZpozKN/BMEa6CqLu\n" |
||||
+ "xbzb7ykxVr7EVFXwltPxzE9TmL9OACNNyF5eJHWMRMllarUvkcXlh4pux4ks9e6z\n" |
||||
+ "V9DQBy2zds9f1I3qxg0eX6JnGrXi/ZiCT+lJgVe3ZFXiejiLAiKB04sXW3ti0LW3\n" |
||||
+ "lx13Y1YlQ4/tlpgTgfIJxKV6nyPiLoK0nywbMd+vpAirDt2Oc+hk\n" |
||||
+ "-----END CERTIFICATE-----\n"); |
||||
} |
||||
|
||||
private static PrivateKey idpPrivateKey() { |
||||
return privateKey("-----BEGIN PRIVATE KEY-----\n" |
||||
+ "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4cn62E1xLqpN3\n" |
||||
+ "4PmbrKBbkOXFjzWgJ9b+pXuaRft6A339uuIQeoeH5qeSKRVTl32L0gdz2ZivLwZX\n" |
||||
+ "W+cqvftVW1tvEHvzJFyxeTW3fCUeCQsebLnA2qRa07RkxTo6Nf244mWWRDodcoHE\n" |
||||
+ "fDUSbxfTZ6IExSojSIU2RnD6WllYWFdD1GFpBJOmQB8rAc8wJIBdHFdQnX8Ttl7h\n" |
||||
+ "Z6rtgqEYMzYVMuJ2F2r1HSU1zSAvwpdYP6rRGFRJEfdA9mm3WKfNLSc5cljz0X/T\n" |
||||
+ "Xy0vVlAV95l9qcfFzPmrkNIst9FZSwpvB49LyAVke04FQPPwLgVH4gphiJH3jvZ7\n" |
||||
+ "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hE/WrBFaKocnxXx5AFNt8lEyHtDwy4Sg1nygGcIJ4sD6koQk\n" |
||||
+ "RdClT3LkvR04TAiSY80bN/i6ZcPNGUwSaDGZEWAIOSWbkwZijZNFnSGOEgxZX/IG\n" |
||||
+ "yd39766vREEMTwEeiMNEOZQ/dmxkJm4OOVe25cLdAoGACOtPnq1Fxay80UYBf4rQ\n" |
||||
+ "+bJ9yX1ulB8WIree1hD7OHSB2lRHxrVYWrglrTvkh63Lgx+EcsTV788OsvAVfPPz\n" |
||||
+ "BZrn8SdDlQqalMxUBYEFwnsYD3cQ8yOUnijFVC4xNcdDv8OIqVgSk4KKxU5AshaA\n" |
||||
+ "xk6Mox+u8Cc2eAK12H13i+8=\n" |
||||
+ "-----END PRIVATE KEY-----\n"); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue