Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor JWTEncryptionImpl to remove redundant code paths #249

Merged
merged 3 commits into from
May 26, 2020
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
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