Skip to content

Commit

Permalink
updating test implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
tanya732 committed Jan 8, 2025
1 parent 9d027b8 commit 687306c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
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-jdk15on:1.70"
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

0 comments on commit 687306c

Please sign in to comment.