Skip to content

Commit

Permalink
Remove use of package that is not available in Java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
Avery-Dunn committed Jan 28, 2025
1 parent a71b7eb commit 1b90d43
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator;
import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;

import java.io.File;
import java.io.FileWriter;
Expand All @@ -17,7 +15,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.Collections;
Expand Down Expand Up @@ -49,6 +46,11 @@ class TestHelper {
static X509Certificate x509Cert = getX509Cert();
static PrivateKey privateKey = getPrivateKey();

public static String CERTIFICATE_ALIAS = "LabAuth.MSIDLab.com";
private static final String WIN_KEYSTORE = "Windows-MY";
private static final String KEYSTORE_PROVIDER = "SunMSCAPI";
private static final String MAC_KEYSTORE = "KeychainStore";

static String readResource(Class<?> classInstance, String resource) {
try {
return new String(Files.readAllBytes(Paths.get(classInstance.getResource(resource).toURI())));
Expand Down Expand Up @@ -137,15 +139,20 @@ static String createIdToken(HashMap<String, String> idTokenValues) {

static void setPrivateKeyAndCert() {
try {
CertAndKeyGen certGen = new CertAndKeyGen("RSA", "SHA256WithRSA", null);
certGen.generate(2048);
X509Certificate cert = certGen.getSelfCertificate(
new X500Name(""), 3600);

x509Cert = cert;
privateKey = certGen.getPrivateKey();
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeyException | IOException | CertificateException | SignatureException e) {
throw new RuntimeException(e);
String os = System.getProperty("os.name");
KeyStore keystore;
if (os.toLowerCase().contains("windows")) {
keystore = KeyStore.getInstance(WIN_KEYSTORE, KEYSTORE_PROVIDER);
} else {
keystore = KeyStore.getInstance(MAC_KEYSTORE);
}

keystore.load(null, null);
privateKey = (PrivateKey) keystore.getKey(CERTIFICATE_ALIAS, null);
x509Cert = (X509Certificate) keystore.getCertificate(
CERTIFICATE_ALIAS);
} catch (Exception e) {
throw new RuntimeException("Error getting certificate from keystore: " + e.getMessage());
}
}

Expand Down

0 comments on commit 1b90d43

Please sign in to comment.