Skip to content

Commit

Permalink
Refactor JWTEncryptionImpl to remove redundant code paths (#249)
Browse files Browse the repository at this point in the history
* Refactor JWTEncryptionImpl to remove redundant code paths
Updated the ExceptionMessage to match the one declared in the API

* Updated the encrypt() API singature to throw the right Exception

* Corrected the java doc
  • Loading branch information
bobbyphilip authored May 26, 2020
1 parent f98c883 commit cc83d11
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public interface JwtEncryption {
* unless different ones have been set with {@code JwtEncryptionBuilder}.
* A key of size 2048 bits or larger MUST be used with the 'RSA-OAEP' and 'RSA-OAEP-256' algorithms.
*
* @return signed JWT token
* @throws JwtSignatureException the exception if the signing operation has failed
* @return encrypted JWT token
* @throws JwtEncryptionException the exception if the encryption operation has failed
*/
String encrypt() throws JwtSignatureException;
String encrypt() throws JwtEncryptionException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public String encrypt(String keyLocation) throws JwtEncryptionException {
} catch (Exception ex) {
throw new JwtEncryptionException(ex);
}
return key instanceof PublicKey ? encryptInternal((PublicKey) key) : encryptInternal((SecretKey) key);
return encryptInternal(key);
}

/**
* {@inheritDoc}
*/
@Override
public String encrypt() throws JwtSignatureException {
return encryptInternal(getKeyEncryptionKeyFromConfig((String) headers.get("kid")));
public String encrypt() throws JwtEncryptionException {
return encrypt(readKeyLocationFromConfig());
}

/**
Expand Down Expand Up @@ -147,42 +147,28 @@ private String getKeyEncryptionAlgorithm(Key keyEncryptionKey) {
if ("dir".equals(alg)) {
throw ImplMessages.msg.directContentEncryptionUnsupported();
}

if (keyEncryptionKey instanceof RSAPublicKey) {
if (alg == null) {
return KeyEncryptionAlgorithm.RSA_OAEP_256.getAlgorithm();
} else {
return alg;
}
} else if (keyEncryptionKey instanceof SecretKey) {
if (alg == null) {
return KeyEncryptionAlgorithm.A256KW.getAlgorithm();
} else {
return alg;
}
} else if (keyEncryptionKey instanceof ECPublicKey) {
if (alg == null) {
return KeyEncryptionAlgorithm.ECDH_ES_A256KW.getAlgorithm();
} else {
return alg;
if (alg == null) {
if (keyEncryptionKey instanceof RSAPublicKey) {
alg = KeyEncryptionAlgorithm.RSA_OAEP_256.getAlgorithm();
} else if (keyEncryptionKey instanceof SecretKey) {
alg = KeyEncryptionAlgorithm.A256KW.getAlgorithm();
} else if (keyEncryptionKey instanceof ECPublicKey) {
alg = KeyEncryptionAlgorithm.ECDH_ES_A256KW.getAlgorithm();
}
}

throw ImplMessages.msg.unsupportedKeyEncryptionAlgorithm(keyEncryptionKey.getAlgorithm());
if (alg == null) {
throw ImplMessages.msg.unsupportedKeyEncryptionAlgorithm(keyEncryptionKey.getAlgorithm());
}
return alg;
}

private String getContentEncryptionAlgorithm() {
return headers.containsKey("enc") ? headers.get("enc").toString() : ContentEncryptionAlgorithm.A256GCM.name();
}

static Key getKeyEncryptionKeyFromConfig(String kid) {
private static String readKeyLocationFromConfig() {
try {
String keyLocation = ConfigProvider.getConfig().getValue("smallrye.jwt.encrypt.key-location", String.class);
try {
return KeyUtils.readEncryptionKey(keyLocation, kid);
} catch (Exception ex) {
throw ImplMessages.msg.encryptionKeyNotFound(keyLocation);
}
return ConfigProvider.getConfig().getValue("smallrye.jwt.encrypt.key-location", String.class);
} catch (NoSuchElementException ex) {
throw ImplMessages.msg.keyLocationPropertyEmpty();
}
Expand Down

0 comments on commit cc83d11

Please sign in to comment.