Skip to content

Commit

Permalink
Improve handling of file system resources (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
sberyozkin authored Nov 16, 2023
1 parent a91cbc3 commit 335ed3e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ interface JWTUtilLogging extends BasicLogger {
@LogMessage(level = Logger.Level.DEBUG)
@Message(id = 1009, value = "Failed to parse the JWK JSON representation")
void parsingJwksFailed();

@LogMessage(level = Logger.Level.DEBUG)
@Message(id = 1010, value = "File %s is not found")
void fileIsNotFound(String fileLocation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,11 @@ public static KeyStore loadKeyStore(String keyStorePath, String keyStorePassword
? KeyStore.getInstance(theKeyStoreType, provider)
: KeyStore.getInstance(theKeyStoreType);
if (keyStorePath != null) {
try (InputStream is = ResourceUtils.getResourceStream(keyStorePath)) {
keyStore.load(is, keyStorePassword.toCharArray());
InputStream is = ResourceUtils.getResourceStream(keyStorePath.trim());
if (is != null) {
try (InputStream keyStream = is) {
keyStore.load(keyStream, keyStorePassword.toCharArray());
}
}
} else {
keyStore.load(null, keyStorePassword.toCharArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public static InputStream getAsFileSystemResource(String publicKeyLocation) thro
try {
return new FileInputStream(publicKeyLocation);
} catch (FileNotFoundException e) {
JWTUtilLogging.log.fileIsNotFound(publicKeyLocation);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ public String encrypt() throws JwtEncryptionException {
if (keyLocation != null) {
key = JwtBuildUtils.readPublicKeyFromKeystore(keyLocation.trim());
if (key == null) {
try (InputStream keyStream = ResourceUtils.getResourceStream(keyLocation.trim())) {
key = getEncryptionKeyFromKeyContent(new String(ResourceUtils.readBytes(keyStream)));
InputStream is = ResourceUtils.getResourceStream(keyLocation.trim());
if (is != null) {
try (InputStream keyStream = is) {
key = getEncryptionKeyFromKeyContent(new String(ResourceUtils.readBytes(keyStream)));
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public String sign() throws JwtSignatureException {
if (keyLocation != null) {
key = JwtBuildUtils.readPrivateKeyFromKeystore(keyLocation.trim());
if (key == null) {
try (InputStream keyStream = ResourceUtils.getResourceStream(keyLocation.trim())) {
key = getSigningKeyFromKeyContent(new String(ResourceUtils.readBytes(keyStream)));
InputStream is = ResourceUtils.getResourceStream(keyLocation.trim());
if (is != null) {
try (InputStream keyStream = is) {
key = getSigningKeyFromKeyContent(new String(ResourceUtils.readBytes(keyStream)));
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.security.Security;

import org.jose4j.jws.JsonWebSignature;
Expand Down Expand Up @@ -56,10 +57,12 @@ private static boolean isPS256Supported() {

@Test
void signClaimsPS256() throws Exception {
String path = "src/test/resources/privateKey.pem";
File file = new File(path);
String jwt = Jwt.claims()
.claim("customClaim", "custom-value")
.jws().algorithm(SignatureAlgorithm.PS256)
.sign("/privateKey.pem");
.sign("file:" + file.getAbsolutePath());

JsonWebSignature jws = JwtSignTest.getVerifiedJws(jwt, KeyUtils.readPublicKey("/publicKey.pem"));
JwtClaims claims = JwtClaims.parse(jws.getPayload());
Expand Down

0 comments on commit 335ed3e

Please sign in to comment.