|
| 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.authentication; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.rules.ExpectedException; |
| 24 | +import org.opensaml.saml.common.xml.SAMLConstants; |
| 25 | +import org.opensaml.saml.saml2.core.AuthnRequest; |
| 26 | + |
| 27 | +import static org.hamcrest.CoreMatchers.containsString; |
| 28 | +import static org.springframework.security.saml2.provider.service.authentication.TestSaml2X509Credentials.relyingPartyCredentials; |
| 29 | + |
| 30 | +public class OpenSamlAuthenticationRequestFactoryTests { |
| 31 | + |
| 32 | + private OpenSamlAuthenticationRequestFactory factory; |
| 33 | + private Saml2AuthenticationRequest request; |
| 34 | + |
| 35 | + @Rule |
| 36 | + public ExpectedException exception = ExpectedException.none(); |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUp() { |
| 40 | + request = Saml2AuthenticationRequest.builder() |
| 41 | + .issuer("https://issuer") |
| 42 | + .destination("https://destination/sso") |
| 43 | + .assertionConsumerServiceUrl("https://issuer/sso") |
| 44 | + .credentials(c -> c.addAll(relyingPartyCredentials())) |
| 45 | + .build(); |
| 46 | + factory = new OpenSamlAuthenticationRequestFactory(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void createAuthenticationRequestWhenDefaultThenReturnsPostBinding() { |
| 51 | + AuthnRequest authn = getAuthNRequest(); |
| 52 | + Assert.assertEquals(SAMLConstants.SAML2_POST_BINDING_URI, authn.getProtocolBinding()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void createAuthenticationRequestWhenSetUriThenReturnsCorrectBinding() { |
| 57 | + factory.setProtocolBinding(SAMLConstants.SAML2_REDIRECT_BINDING_URI); |
| 58 | + AuthnRequest authn = getAuthNRequest(); |
| 59 | + Assert.assertEquals(SAMLConstants.SAML2_REDIRECT_BINDING_URI, authn.getProtocolBinding()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void createAuthenticationRequestWhenSetUnsupportredUriThenThrowsIllegalArgumentException() { |
| 64 | + exception.expect(IllegalArgumentException.class); |
| 65 | + exception.expectMessage(containsString("my-invalid-binding")); |
| 66 | + factory.setProtocolBinding("my-invalid-binding"); |
| 67 | + } |
| 68 | + |
| 69 | + private AuthnRequest getAuthNRequest() { |
| 70 | + String xml = factory.createAuthenticationRequest(request); |
| 71 | + return (AuthnRequest) OpenSamlImplementation.getInstance().resolve(xml); |
| 72 | + } |
| 73 | +} |
0 commit comments