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

Throw correct exception when key is not of type RSA #48

Merged
merged 1 commit into from Dec 4, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/main/java/com/auth0/jwk/Jwk.java
Expand Up @@ -33,15 +33,16 @@ public class Jwk {

/**
* Creates a new Jwk
* @param id kid
* @param type kyt
* @param algorithm alg
* @param usage use
* @param operations key_ops
* @param certificateUrl x5u
* @param certificateChain x5c
*
* @param id kid
* @param type kyt
* @param algorithm alg
* @param usage use
* @param operations key_ops
* @param certificateUrl x5u
* @param certificateChain x5c
* @param certificateThumbprint x5t
* @param additionalAttributes additional attributes not part of the standard ones
* @param additionalAttributes additional attributes not part of the standard ones
*/
@SuppressWarnings("WeakerAccess")
public Jwk(String id, String type, String algorithm, String usage, List<String> operations, String certificateUrl, List<String> certificateChain, String certificateThumbprint, Map<String, Object> additionalAttributes) {
Expand All @@ -58,6 +59,7 @@ public Jwk(String id, String type, String algorithm, String usage, List<String>

/**
* Creates a new Jwk
*
* @param id
* @param type
* @param algorithm
Expand All @@ -67,7 +69,6 @@ public Jwk(String id, String type, String algorithm, String usage, List<String>
* @param certificateChain
* @param certificateThumbprint
* @param additionalAttributes
*
* @deprecated The specification states that the 'key_ops' (operations) parameter contains an array value.
* Use {@link #Jwk(String, String, String, String, List, String, List, String, Map)}
*/
Expand All @@ -91,7 +92,7 @@ static Jwk fromValues(Map<String, Object> map) {
if (kty == null) {
throw new IllegalArgumentException("Attributes " + map + " are not from a valid jwk");
}
if(keyOps instanceof String) {
if (keyOps instanceof String) {
return new Jwk(kid, kty, alg, use, (String) keyOps, x5u, x5c, x5t, values);
} else {
return new Jwk(kid, kty, alg, use, (List<String>) keyOps, x5u, x5c, x5t, values);
Expand Down Expand Up @@ -120,12 +121,12 @@ public String getUsage() {

@SuppressWarnings("WeakerAccess")
public String getOperations() {
if(operations == null || operations.isEmpty()) {
if (operations == null || operations.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
String delimiter = ",";
for(String operation : operations) {
for (String operation : operations) {
sb.append(operation);
sb.append(delimiter);
}
Expand Down Expand Up @@ -159,13 +160,14 @@ public Map<String, Object> getAdditionalAttributes() {

/**
* Returns a {@link PublicKey} if the {@code 'alg'} is {@code 'RSA'}
*
* @return a public key
* @throws InvalidPublicKeyException if the key cannot be built or the key type is not RSA
*/
@SuppressWarnings("WeakerAccess")
public PublicKey getPublicKey() throws InvalidPublicKeyException {
if (!PUBLIC_KEY_ALGORITHM.equalsIgnoreCase(type)) {
return null;
throw new InvalidPublicKeyException("The key is not of type RSA", null);
}
try {
KeyFactory kf = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM);
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/auth0/jwk/JwkTest.java
Expand Up @@ -91,11 +91,13 @@ public void shouldReturnPublicKeyForEmptyKeyOpsParam() throws Exception {
}

@Test
public void shouldReturnNullForNonRSAKey() throws Exception {
public void shouldThrowForNonRSAKey() throws Exception {
final String kid = randomKeyId();
Map<String, Object> values = nonRSAValues(kid);
Jwk jwk = Jwk.fromValues(values);
assertThat(jwk.getPublicKey(), nullValue());
expectedException.expect(InvalidPublicKeyException.class);
expectedException.expectMessage("The key is not of type RSA");
jwk.getPublicKey();
}

@Test
Expand Down