Skip to content

Commit 1b90d43

Browse files
committed
Remove use of package that is not available in Java 17
1 parent a71b7eb commit 1b90d43

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TestHelper.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import com.nimbusds.jose.crypto.RSASSASigner;
88
import com.nimbusds.jose.jwk.RSAKey;
99
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator;
10-
import sun.security.tools.keytool.CertAndKeyGen;
11-
import sun.security.x509.X500Name;
1210

1311
import java.io.File;
1412
import java.io.FileWriter;
@@ -17,7 +15,6 @@
1715
import java.nio.file.Files;
1816
import java.nio.file.Paths;
1917
import java.security.*;
20-
import java.security.cert.CertificateException;
2118
import java.security.cert.X509Certificate;
2219
import java.util.Base64;
2320
import java.util.Collections;
@@ -49,6 +46,11 @@ class TestHelper {
4946
static X509Certificate x509Cert = getX509Cert();
5047
static PrivateKey privateKey = getPrivateKey();
5148

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+
5254
static String readResource(Class<?> classInstance, String resource) {
5355
try {
5456
return new String(Files.readAllBytes(Paths.get(classInstance.getResource(resource).toURI())));
@@ -137,15 +139,20 @@ static String createIdToken(HashMap<String, String> idTokenValues) {
137139

138140
static void setPrivateKeyAndCert() {
139141
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());
149156
}
150157
}
151158

0 commit comments

Comments
 (0)