in callouts/java/service-callout/src/main/java/example/JwtAuth.java [95:114]
public static PublicKey loadPublicKey(String pemFilePath) throws IOException, GeneralSecurityException {
try (InputStream is = JwtAuth.class.getClassLoader().getResourceAsStream(pemFilePath)) {
if (is == null) {
throw new IOException("PEM file not found: " + pemFilePath);
}
try (PEMParser pemParser = new PEMParser(new InputStreamReader(is, StandardCharsets.UTF_8))) {
Object object = pemParser.readObject();
if (object instanceof SubjectPublicKeyInfo) {
SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) object;
byte[] encoded = spki.getEncoded();
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
} else {
throw new IllegalArgumentException("Invalid PEM file: Not a valid SubjectPublicKeyInfo");
}
}
}
}