|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.saml2.provider.service.servlet.filter; |
| 18 | + |
| 19 | +import org.apache.commons.codec.binary.Base64; |
| 20 | +import org.junit.Test; |
| 21 | +import org.springframework.core.io.ClassPathResource; |
| 22 | +import org.springframework.util.StreamUtils; |
| 23 | +import org.springframework.web.util.UriUtils; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | + |
| 28 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +public class Saml2UtilsTests { |
| 32 | + |
| 33 | + private static Base64 UNCHUNKED_ENCODER = new Base64(0, new byte[]{'\n'}); |
| 34 | + private static final Base64 CHUNKED_ENCODER = new Base64(76, new byte[] { '\n' }); |
| 35 | + |
| 36 | + @Test |
| 37 | + public void decodeWhenUsingApacheCommonsBase64ThenXmlIsValid() throws Exception { |
| 38 | + String responseUrlDecoded = getSsoCircleEncodedXml(); |
| 39 | + String xml = new String(UNCHUNKED_ENCODER.decode(responseUrlDecoded.getBytes(UTF_8)), UTF_8); |
| 40 | + validateSsoCircleXml(xml); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void decodeWhenUsingApacheCommonsBase64ChunkedThenXmlIsValid() throws Exception { |
| 45 | + String responseUrlDecoded = getSsoCircleEncodedXml(); |
| 46 | + String xml = new String(CHUNKED_ENCODER.decode(responseUrlDecoded.getBytes(UTF_8)), UTF_8); |
| 47 | + validateSsoCircleXml(xml); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void decodeWhenUsingSamlUtilsBase64ThenXmlIsValid() throws Exception { |
| 52 | + String responseUrlDecoded = getSsoCircleEncodedXml(); |
| 53 | + String xml = new String(Saml2Utils.decode(responseUrlDecoded), UTF_8); |
| 54 | + validateSsoCircleXml(xml); |
| 55 | + } |
| 56 | + |
| 57 | + private void validateSsoCircleXml(String xml) { |
| 58 | + assertThat(xml) |
| 59 | + .contains("InResponseTo=\"ARQ9a73ead-7dcf-45a8-89eb-26f3c9900c36\"") |
| 60 | + .contains(" ID=\"s246d157446618e90e43fb79bdd4d9e9e19cf2c7c4\"") |
| 61 | + .contains("<saml:Issuer>https://idp.ssocircle.com</saml:Issuer>"); |
| 62 | + } |
| 63 | + |
| 64 | + private String getSsoCircleEncodedXml() throws IOException { |
| 65 | + ClassPathResource resource = new ClassPathResource("saml2-response-sso-circle.encoded"); |
| 66 | + String response = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8); |
| 67 | + return UriUtils.decode(response, UTF_8); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments