|
19 | 19 |
|
20 | 20 | package org.apache.cloudstack;
|
21 | 21 |
|
22 |
| -import java.security.KeyPair; |
23 |
| -import java.security.PrivateKey; |
24 |
| -import java.security.PublicKey; |
25 |
| -import java.util.regex.Pattern; |
26 |
| - |
| 22 | +import junit.framework.TestCase; |
| 23 | +import org.apache.cloudstack.saml.SAML2AuthManager; |
| 24 | +import org.apache.cloudstack.saml.SAMLProviderMetadata; |
27 | 25 | import org.apache.cloudstack.saml.SAMLUtils;
|
28 | 26 | import org.apache.cloudstack.utils.security.CertUtils;
|
29 | 27 | import org.junit.Test;
|
30 | 28 | import org.opensaml.saml2.core.AuthnRequest;
|
31 | 29 | import org.opensaml.saml2.core.LogoutRequest;
|
32 | 30 |
|
33 |
| -import junit.framework.TestCase; |
| 31 | +import java.net.URI; |
| 32 | +import java.security.KeyPair; |
| 33 | +import java.security.PrivateKey; |
| 34 | +import java.security.PublicKey; |
| 35 | +import java.util.regex.Pattern; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
34 | 38 |
|
35 | 39 | public class SAMLUtilsTest extends TestCase {
|
36 | 40 |
|
@@ -60,6 +64,63 @@ public void testBuildAuthnRequestObject() throws Exception {
|
60 | 64 | assertEquals(req.getIssuer().getValue(), spId);
|
61 | 65 | }
|
62 | 66 |
|
| 67 | + @Test |
| 68 | + public void testBuildAuthnRequestUrlWithoutQueryParam() throws Exception { |
| 69 | + String urlScheme = "http"; |
| 70 | + |
| 71 | + String spDomain = "sp.domain.example"; |
| 72 | + String spUrl = urlScheme + "://" + spDomain; |
| 73 | + String spId = "serviceProviderId"; |
| 74 | + |
| 75 | + String idpDomain = "idp.domain.example"; |
| 76 | + String idpUrl = urlScheme + "://" + idpDomain; |
| 77 | + String idpId = "identityProviderId"; |
| 78 | + |
| 79 | + String authnId = SAMLUtils.generateSecureRandomId(); |
| 80 | + |
| 81 | + SAMLProviderMetadata spMetadata = new SAMLProviderMetadata(); |
| 82 | + spMetadata.setEntityId(spId); |
| 83 | + spMetadata.setSsoUrl(spUrl); |
| 84 | + |
| 85 | + SAMLProviderMetadata idpMetadata = new SAMLProviderMetadata(); |
| 86 | + idpMetadata.setSsoUrl(idpUrl); |
| 87 | + idpMetadata.setEntityId(idpId); |
| 88 | + |
| 89 | + URI redirectUrl = new URI(SAMLUtils.buildAuthnRequestUrl(authnId, spMetadata, idpMetadata, SAML2AuthManager.SAMLSignatureAlgorithm.value())); |
| 90 | + assertThat(redirectUrl).hasScheme(urlScheme).hasHost(idpDomain).hasParameter("SAMLRequest"); |
| 91 | + assertEquals(urlScheme, redirectUrl.getScheme()); |
| 92 | + assertEquals(idpDomain, redirectUrl.getHost()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testBuildAuthnRequestUrlWithQueryParam() throws Exception { |
| 97 | + String urlScheme = "http"; |
| 98 | + |
| 99 | + String spDomain = "sp.domain.example"; |
| 100 | + String spUrl = urlScheme + "://" + spDomain; |
| 101 | + String spId = "cloudstack"; |
| 102 | + |
| 103 | + String idpDomain = "idp.domain.example"; |
| 104 | + String idpQueryParam = "idpid=CX1298373"; |
| 105 | + String idpUrl = urlScheme + "://" + idpDomain + "?" + idpQueryParam; |
| 106 | + String idpId = "identityProviderId"; |
| 107 | + |
| 108 | + String authnId = SAMLUtils.generateSecureRandomId(); |
| 109 | + |
| 110 | + SAMLProviderMetadata spMetadata = new SAMLProviderMetadata(); |
| 111 | + spMetadata.setEntityId(spId); |
| 112 | + spMetadata.setSsoUrl(spUrl); |
| 113 | + |
| 114 | + SAMLProviderMetadata idpMetadata = new SAMLProviderMetadata(); |
| 115 | + idpMetadata.setSsoUrl(idpUrl); |
| 116 | + idpMetadata.setEntityId(idpId); |
| 117 | + |
| 118 | + URI redirectUrl = new URI(SAMLUtils.buildAuthnRequestUrl(authnId, spMetadata, idpMetadata, SAML2AuthManager.SAMLSignatureAlgorithm.value())); |
| 119 | + assertThat(redirectUrl).hasScheme(urlScheme).hasHost(idpDomain).hasParameter("idpid").hasParameter("SAMLRequest"); |
| 120 | + assertEquals(urlScheme, redirectUrl.getScheme()); |
| 121 | + assertEquals(idpDomain, redirectUrl.getHost()); |
| 122 | + } |
| 123 | + |
63 | 124 | @Test
|
64 | 125 | public void testBuildLogoutRequest() throws Exception {
|
65 | 126 | String logoutUrl = "http://logoutUrl";
|
|
0 commit comments