|
7 | 7 | import com.nimbusds.jose.crypto.RSASSASigner; |
8 | 8 | import com.nimbusds.jose.jwk.RSAKey; |
9 | 9 | import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
10 | | -import sun.security.tools.keytool.CertAndKeyGen; |
11 | | -import sun.security.x509.X500Name; |
12 | 10 |
|
13 | 11 | import java.io.File; |
14 | 12 | import java.io.FileWriter; |
|
17 | 15 | import java.nio.file.Files; |
18 | 16 | import java.nio.file.Paths; |
19 | 17 | import java.security.*; |
20 | | -import java.security.cert.CertificateException; |
21 | 18 | import java.security.cert.X509Certificate; |
22 | 19 | import java.util.Base64; |
23 | 20 | import java.util.Collections; |
@@ -49,6 +46,11 @@ class TestHelper { |
49 | 46 | static X509Certificate x509Cert = getX509Cert(); |
50 | 47 | static PrivateKey privateKey = getPrivateKey(); |
51 | 48 |
|
| 49 | + public static String CERTIFICATE_ALIAS = "LabAuth.MSIDLab.com"; |
| 50 | + private static final String WIN_KEYSTORE = "Windows-MY"; |
| 51 | + private static final String KEYSTORE_PROVIDER = "SunMSCAPI"; |
| 52 | + private static final String MAC_KEYSTORE = "KeychainStore"; |
| 53 | + |
52 | 54 | static String readResource(Class<?> classInstance, String resource) { |
53 | 55 | try { |
54 | 56 | return new String(Files.readAllBytes(Paths.get(classInstance.getResource(resource).toURI()))); |
@@ -137,15 +139,20 @@ static String createIdToken(HashMap<String, String> idTokenValues) { |
137 | 139 |
|
138 | 140 | static void setPrivateKeyAndCert() { |
139 | 141 | try { |
140 | | - CertAndKeyGen certGen = new CertAndKeyGen("RSA", "SHA256WithRSA", null); |
141 | | - certGen.generate(2048); |
142 | | - X509Certificate cert = certGen.getSelfCertificate( |
143 | | - new X500Name(""), 3600); |
144 | | - |
145 | | - x509Cert = cert; |
146 | | - privateKey = certGen.getPrivateKey(); |
147 | | - } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeyException | IOException | CertificateException | SignatureException e) { |
148 | | - throw new RuntimeException(e); |
| 142 | + String os = System.getProperty("os.name"); |
| 143 | + KeyStore keystore; |
| 144 | + if (os.toLowerCase().contains("windows")) { |
| 145 | + keystore = KeyStore.getInstance(WIN_KEYSTORE, KEYSTORE_PROVIDER); |
| 146 | + } else { |
| 147 | + keystore = KeyStore.getInstance(MAC_KEYSTORE); |
| 148 | + } |
| 149 | + |
| 150 | + keystore.load(null, null); |
| 151 | + privateKey = (PrivateKey) keystore.getKey(CERTIFICATE_ALIAS, null); |
| 152 | + x509Cert = (X509Certificate) keystore.getCertificate( |
| 153 | + CERTIFICATE_ALIAS); |
| 154 | + } catch (Exception e) { |
| 155 | + throw new RuntimeException("Error getting certificate from keystore: " + e.getMessage()); |
149 | 156 | } |
150 | 157 | } |
151 | 158 |
|
|
0 commit comments