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 1 commit
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 @@ -43,7 +43,7 @@ JwtSignatureException unsupportedSignatureAlgorithm(String algorithmName,
JwtEncryptionException encryptionKeyNotFound(String keyLocation);

@Message(id = 5008, value = "Please set a 'smallrye.jwt.encrypt.key-location' property")
JwtEncryptionException keyLocationPropertyEmpty();
JwtSignatureException keyLocationPropertyEmpty();

@Message(id = 5009, value = "")
JwtSignatureException signatureException(@Cause Throwable throwable);
Expand Down
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 {
bobbyphilip marked this conversation as resolved.
Show resolved Hide resolved
return encryptInternal(getKeyEncryptionKeyFromConfig((String) headers.get("kid")));
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