private List<Jwk> getAll() throws SigningKeyNotFoundException { List<Jwk> jwks = Lists.newArrayList(); @SuppressWarnings("unchecked") final List<Map<String, Object>> keys = (List<Map<String, Object>>) getJwks().get("keys"); if (keys == null || keys.isEmpty()) { throw new SigningKeyNotFoundException("No keys found in " + url.toString(), null); } try { for (Map<String, Object> values : keys) { jwks.add(Jwk.fromValues(values)); } } catch (IllegalArgumentException e) { throw new SigningKeyNotFoundException("Failed to parse jwk from json", e); } return jwks; }
@Test public void shouldNotThrowInvalidArgumentExceptionOnMissingKidParam() throws Exception { //kid is optional - https://tools.ietf.org/html/rfc7517#section-4.5 final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, KEY_OPS_LIST); values.remove("kid"); Jwk.fromValues(values); }
@Test public void shouldReturnKeyWithMissingAlgParam() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, KEY_OPS_LIST); values.remove("alg"); Jwk jwk = Jwk.fromValues(values); assertThat(jwk.getPublicKey(), notNullValue()); }
@Test public void shouldThrowInvalidArgumentExceptionOnMissingKtyParam() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, KEY_OPS_LIST); values.remove("kty"); expectedException.expect(IllegalArgumentException.class); Jwk.fromValues(values); }
@Test public void shouldReturnPublicKeyForEmptyKeyOpsParam() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, Lists.newArrayList()); Jwk jwk = Jwk.fromValues(values); assertThat(jwk.getPublicKey(), notNullValue()); assertThat(jwk.getOperationsAsList(), notNullValue()); assertThat(jwk.getOperationsAsList().size(), equalTo(0)); assertThat(jwk.getOperations(), nullValue()); }
@Test public void shouldReturnPublicKeyForNullKeyOpsParam() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, null); Jwk jwk = Jwk.fromValues(values); assertThat(jwk.getPublicKey(), notNullValue()); assertThat(jwk.getOperationsAsList(), nullValue()); assertThat(jwk.getOperations(), nullValue()); }
@Test public void shouldReturnPublicKeyForStringKeyOpsParam() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, KEY_OPS_STRING); Jwk jwk = Jwk.fromValues(values); assertThat(jwk.getPublicKey(), notNullValue()); assertThat(jwk.getOperationsAsList(), is(KEY_OPS_LIST)); assertThat(jwk.getOperations(), is(KEY_OPS_STRING)); }
@Test public void shouldReturnPublicKey() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, KEY_OPS_LIST); Jwk jwk = Jwk.fromValues(values); assertThat(jwk.getPublicKey(), notNullValue()); assertThat(jwk.getOperationsAsList(), is(KEY_OPS_LIST)); assertThat(jwk.getOperations(), is(KEY_OPS_STRING)); }
@Test public void shouldBuildWithMap() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = publicKeyValues(kid, KEY_OPS_LIST); Jwk jwk = Jwk.fromValues(values); assertThat(jwk.getId(), equalTo(kid)); assertThat(jwk.getAlgorithm(), equalTo(RS_256)); assertThat(jwk.getType(), equalTo(RSA)); assertThat(jwk.getUsage(), equalTo(SIG)); assertThat(jwk.getOperationsAsList(), equalTo(KEY_OPS_LIST)); assertThat(jwk.getOperations(), is(KEY_OPS_STRING)); assertThat(jwk.getCertificateThumbprint(), equalTo(THUMBPRINT)); assertThat(jwk.getCertificateChain(), contains(CERT_CHAIN)); }
@Test public void shouldThrowForNonRSAKey() throws Exception { final String kid = randomKeyId(); Map<String, Object> values = nonRSAValues(kid); Jwk jwk = Jwk.fromValues(values); expectedException.expect(InvalidPublicKeyException.class); expectedException.expectMessage("The key is not of type RSA"); jwk.getPublicKey(); }