Skip to content

Commit 1ed532f

Browse files
committed
SAMLUtils: add unit test for SAMLUtils and method to randomly generate X509 certs
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 15fdc17 commit 1ed532f

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

utils/src/org/apache/cloudstack/utils/auth/SAMLUtils.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import com.cloud.utils.HttpUtils;
2323
import org.apache.log4j.Logger;
24+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
25+
import org.bouncycastle.x509.X509V1CertificateGenerator;
2426
import org.joda.time.DateTime;
2527
import org.opensaml.Configuration;
2628
import org.opensaml.common.SAMLVersion;
@@ -57,6 +59,7 @@
5759
import org.w3c.dom.Element;
5860
import org.xml.sax.SAXException;
5961

62+
import javax.security.auth.x500.X500Principal;
6063
import javax.xml.parsers.DocumentBuilder;
6164
import javax.xml.parsers.DocumentBuilderFactory;
6265
import javax.xml.parsers.ParserConfigurationException;
@@ -66,7 +69,17 @@
6669
import java.io.StringWriter;
6770
import java.math.BigInteger;
6871
import java.net.URLEncoder;
72+
import java.security.InvalidKeyException;
73+
import java.security.KeyPair;
74+
import java.security.KeyPairGenerator;
75+
import java.security.NoSuchAlgorithmException;
76+
import java.security.NoSuchProviderException;
6977
import java.security.SecureRandom;
78+
import java.security.Security;
79+
import java.security.SignatureException;
80+
import java.security.cert.CertificateEncodingException;
81+
import java.security.cert.X509Certificate;
82+
import java.util.Date;
7083
import java.util.zip.Deflater;
7184
import java.util.zip.DeflaterOutputStream;
7285

@@ -88,7 +101,7 @@ public static Boolean checkSAMLUserId(String uuid) {
88101
}
89102

90103
public static String generateSecureRandomId() {
91-
return new BigInteger(130, new SecureRandom()).toString(32);
104+
return new BigInteger(160, new SecureRandom()).toString(32);
92105
}
93106

94107
public static AuthnRequest buildAuthnRequestObject(String spId, String idpUrl, String consumerUrl) {
@@ -194,4 +207,26 @@ public static Response decodeSAMLResponse(String responseMessage)
194207
return (Response) unmarshaller.unmarshall(element);
195208
}
196209

210+
public static X509Certificate generateRandomX509Certification() throws NoSuchAlgorithmException, NoSuchProviderException, CertificateEncodingException, SignatureException, InvalidKeyException {
211+
Date validityBeginDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
212+
Date validityEndDate = new Date(System.currentTimeMillis() + 2 * 365 * 24 * 60 * 60 * 1000);
213+
214+
Security.addProvider(new BouncyCastleProvider());
215+
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
216+
keyPairGenerator.initialize(1024, new SecureRandom());
217+
KeyPair keyPair = keyPairGenerator.generateKeyPair();
218+
219+
X500Principal dnName = new X500Principal("CN=John Doe");
220+
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
221+
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
222+
certGen.setSubjectDN(dnName);
223+
certGen.setIssuerDN(dnName); // use the same
224+
certGen.setNotBefore(validityBeginDate);
225+
certGen.setNotAfter(validityEndDate);
226+
certGen.setPublicKey(keyPair.getPublic());
227+
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
228+
229+
return certGen.generate(keyPair.getPrivate(), "BC");
230+
}
231+
197232
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package org.apache.cloudstack.utils.auth;
21+
22+
import junit.framework.TestCase;
23+
import org.junit.Test;
24+
import org.opensaml.saml2.core.AuthnRequest;
25+
import org.opensaml.saml2.core.LogoutRequest;
26+
import org.opensaml.saml2.core.NameID;
27+
import org.opensaml.saml2.core.impl.NameIDBuilder;
28+
29+
public class SAMLUtilsTest extends TestCase {
30+
31+
@Test
32+
public void testSAMLId() throws Exception {
33+
assertTrue(SAMLUtils.checkSAMLUserId(SAMLUtils.createSAMLId("someUID")));
34+
assertFalse(SAMLUtils.checkSAMLUserId("randomUID"));
35+
}
36+
37+
@Test
38+
public void testGenerateSecureRandomId() throws Exception {
39+
assertTrue(SAMLUtils.generateSecureRandomId().length() == 32);
40+
}
41+
42+
@Test
43+
public void testBuildAuthnRequestObject() throws Exception {
44+
String consumerUrl = "http://someurl.com";
45+
String idpUrl = "http://idp.domain.example";
46+
String spId = "cloudstack";
47+
AuthnRequest req = SAMLUtils.buildAuthnRequestObject(spId, idpUrl, consumerUrl);
48+
assertEquals(req.getAssertionConsumerServiceURL(), consumerUrl);
49+
assertEquals(req.getDestination(), idpUrl);
50+
assertEquals(req.getIssuer().getValue(), spId);
51+
}
52+
53+
@Test
54+
public void testBuildLogoutRequest() throws Exception {
55+
String logoutUrl = "http://logoutUrl";
56+
String spId = "cloudstack";
57+
String sessionIndex = "12345";
58+
String nameIdString = "someNameID";
59+
NameID sessionNameId = new NameIDBuilder().buildObject();
60+
sessionNameId.setValue(nameIdString);
61+
LogoutRequest req = SAMLUtils.buildLogoutRequest(logoutUrl, spId, sessionNameId, sessionIndex);
62+
assertEquals(req.getDestination(), logoutUrl);
63+
assertEquals(req.getIssuer().getValue(), spId);
64+
assertEquals(req.getNameID().getValue(), nameIdString);
65+
assertEquals(req.getSessionIndexes().get(0).getSessionIndex(), sessionIndex);
66+
}
67+
}

0 commit comments

Comments
 (0)