KeyLocationResolver extends AbstractKeyLocationResolver implements VerificationKeyResolver { @Override public Key resolveKey(JsonWebSignature jws, List<JsonWebStructure> nestingContext) throws UnresolvableKeyException { verifyKid(jws, authContextInfo.getTokenKeyId()); if (key != null) { return key; } Key theKey = tryAsVerificationJwk(jws); if (theKey == null) { reportUnresolvableKeyException(authContextInfo.getPublicKeyContent(), authContextInfo.getPublicKeyLocation()); } return theKey; } KeyLocationResolver(JWTAuthContextInfo authContextInfo); @Override Key resolveKey(JsonWebSignature jws, List<JsonWebStructure> nestingContext);  }
@Test public void testLoadPemKeyWithWrongLocation() throws Exception { expectedEx.expect(UnresolvableKeyException.class); KeyLocationResolver keyLocationResolver = new KeyLocationResolver(new JWTAuthContextInfo("wrong_location.pem", null)); keyLocationResolver.resolveKey(signature, emptyList()); }
@Test public void testLoadRsaKeyFromHttpsJwks() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("https: contextInfo.setJwksRefreshInterval(10); KeyLocationResolver keyLocationResolver = new KeyLocationResolver(contextInfo) { protected HttpsJwks initializeHttpsJwks(String loc) { return mockedHttpsJwks; } }; RsaJsonWebKey jwk = new RsaJsonWebKey(rsaKey); jwk.setKeyId("1"); when(mockedHttpsJwks.getJsonWebKeys()).thenReturn(Collections.singletonList(jwk)); keyLocationResolver = Mockito.spy(keyLocationResolver); when(signature.getHeaders()).thenReturn(headers); when(headers.getStringHeaderValue(JsonWebKey.KEY_ID_PARAMETER)).thenReturn("1"); assertEquals(rsaKey, keyLocationResolver.resolveKey(signature, emptyList())); assertNull(keyLocationResolver.key); }
@Test public void testLoadSecretKeyFromHttpsJwks() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("https: contextInfo.setJwksRefreshInterval(10); KeyLocationResolver keyLocationResolver = new KeyLocationResolver(contextInfo) { protected HttpsJwks initializeHttpsJwks(String loc) { return mockedHttpsJwks; } }; OctetSequenceJsonWebKey jwk = new OctetSequenceJsonWebKey(secretKey); jwk.setKeyId("1"); when(mockedHttpsJwks.getJsonWebKeys()).thenReturn(Collections.singletonList(jwk)); keyLocationResolver = Mockito.spy(keyLocationResolver); when(signature.getHeaders()).thenReturn(headers); when(headers.getStringHeaderValue(JsonWebKey.KEY_ID_PARAMETER)).thenReturn("1"); assertEquals(secretKey, keyLocationResolver.resolveKey(signature, emptyList())); assertNull(keyLocationResolver.key); }
@Test public void testLoadHttpsJwksNonMathchingKidAndRefresh() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("https: contextInfo.setJwksRefreshInterval(10); KeyLocationResolver keyLocationResolver = new KeyLocationResolver(contextInfo) { protected HttpsJwks initializeHttpsJwks(String loc) { return mockedHttpsJwks; } }; when(signature.getHeaders()).thenReturn(headers); when(headers.getStringHeaderValue(JsonWebKey.KEY_ID_PARAMETER)).thenReturn("1"); final RsaJsonWebKey jwk = new RsaJsonWebKey(rsaKey); jwk.setKeyId("2"); when(mockedHttpsJwks.getJsonWebKeys()).thenReturn(Collections.singletonList(jwk)); doAnswer((i) -> { jwk.setKeyId("1"); return null; }).when(mockedHttpsJwks).refresh(); keyLocationResolver = Mockito.spy(keyLocationResolver); assertEquals(rsaKey, keyLocationResolver.resolveKey(signature, emptyList())); assertNull(keyLocationResolver.key); }
@Test(expected = UnresolvableKeyException.class) public void testLoadHttpsJwksNonMathchingKidAndRefreshDeclined() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("https: contextInfo.setJwksRefreshInterval(10); contextInfo.setForcedJwksRefreshInterval(10); KeyLocationResolver keyLocationResolver = new KeyLocationResolver(contextInfo) { protected HttpsJwks initializeHttpsJwks(String loc) { return mockedHttpsJwks; } }; when(signature.getHeaders()).thenReturn(headers); when(headers.getStringHeaderValue(JsonWebKey.KEY_ID_PARAMETER)).thenReturn("1"); final RsaJsonWebKey jwk = new RsaJsonWebKey(rsaKey); jwk.setKeyId("2"); when(mockedHttpsJwks.getJsonWebKeys()).thenReturn(Collections.singletonList(jwk)); doAnswer((i) -> { jwk.setKeyId("1"); return null; }).when(mockedHttpsJwks).refresh(); keyLocationResolver = Mockito.spy(keyLocationResolver); assertEquals(rsaKey, keyLocationResolver.resolveKey(signature, emptyList())); assertNull(keyLocationResolver.key); jwk.setKeyId("2"); keyLocationResolver.resolveKey(signature, emptyList()); }
@Test public void testLoadJWKOnClassPath() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("publicKey.jwk", "issuer"); contextInfo.setTokenKeyId("key1"); KeyLocationResolver keyLocationResolver = new KeyLocationResolver(contextInfo); when(signature.getHeaders()).thenReturn(headers); when(headers.getStringHeaderValue(JsonWebKey.KEY_ID_PARAMETER)).thenReturn("key1"); assertNotNull(keyLocationResolver.key); assertEquals(keyLocationResolver.key, keyLocationResolver.resolveKey(signature, emptyList())); assertEquals(keyLocationResolver.key, keyLocationResolver.getJsonWebKey("key1", null).getKey()); }