Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ dependencies {
implementation "com.auth0:java-jwt:4.4.0"
implementation "net.jodah:failsafe:2.4.4"

testImplementation "org.bouncycastle:bcprov-jdk18on:1.78"
testImplementation "org.mockito:mockito-core:4.8.1"
testImplementation "com.squareup.okhttp3:mockwebserver:${okhttpVersion}"
testImplementation "org.hamcrest:hamcrest:${hamcrestVersion}"
Expand Down
20 changes: 14 additions & 6 deletions src/test/java/com/auth0/utils/tokens/SignatureVerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.auth0.exception.PublicKeyProviderException;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.bouncycastle.util.io.pem.PemReader;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -18,6 +18,7 @@
import java.security.interfaces.RSAPublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Scanner;

import static com.auth0.AssertsUtil.verifyThrows;
Expand Down Expand Up @@ -144,7 +145,7 @@ public RSAPublicKey getPublicKeyById(String keyId) throws PublicKeyProviderExcep

private static RSAPublicKey readPublicKeyFromFile(final String path) throws IOException {
Scanner scanner = null;
PemReader pemReader = null;
BufferedReader reader = null;
try {
scanner = new Scanner(Paths.get(path));
if (scanner.hasNextLine() && scanner.nextLine().startsWith("-----BEGIN CERTIFICATE-----")) {
Expand All @@ -155,8 +156,15 @@ private static RSAPublicKey readPublicKeyFromFile(final String path) throws IOEx
fs.close();
return (RSAPublicKey) key;
} else {
pemReader = new PemReader(new FileReader(path));
byte[] keyBytes = pemReader.readPemObject().getContent();
reader = new BufferedReader(new FileReader(path));
StringBuilder pemContent = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("-----BEGIN") && !line.startsWith("-----END")) {
pemContent.append(line);
}
}
byte[] keyBytes = Base64.getDecoder().decode(pemContent.toString());
KeyFactory kf = KeyFactory.getInstance("RSA");
EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
return (RSAPublicKey) kf.generatePublic(keySpec);
Expand All @@ -167,8 +175,8 @@ private static RSAPublicKey readPublicKeyFromFile(final String path) throws IOEx
if (scanner != null) {
scanner.close();
}
if (pemReader != null) {
pemReader.close();
if (reader != null) {
reader.close();
}
}
}
Expand Down
Loading