X509KeyLocationResolver extends AbstractKeyLocationResolver implements VerificationKeyResolver { @Override public Key resolveKey(JsonWebSignature jws, List<JsonWebStructure> nestingContext) throws UnresolvableKeyException { return resolver.resolveKey(jws, nestingContext); } X509KeyLocationResolver(JWTAuthContextInfo authContextInfo); @Override Key resolveKey(JsonWebSignature jws, List<JsonWebStructure> nestingContext);  }
@Test public void testLoadHttpsJwksWithX5t() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("https: contextInfo.setJwksRefreshInterval(10); RsaJsonWebKey jwk = new RsaJsonWebKey(key); jwk.setOtherParameter("x5c", Collections.singletonList(x5c)); when(mockedHttpsJwks.getJsonWebKeys()).thenReturn(Collections.singletonList(jwk)); X509KeyLocationResolver keyLocationResolver = new X509KeyLocationResolver(contextInfo) { protected HttpsJwks initializeHttpsJwks(String loc) { return mockedHttpsJwks; } }; keyLocationResolver = Mockito.spy(keyLocationResolver); when(signature.getX509CertSha1ThumbprintHeaderValue()).thenReturn(x5t); assertEquals(key, keyLocationResolver.resolveKey(signature, emptyList())); }
@Test public void testLoadHttpsPemCert() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("https: contextInfo.setJwksRefreshInterval(10); Mockito.doThrow(new JoseException("")).when(mockedHttpsJwks).refresh(); Mockito.doReturn(ResourceUtils.getAsClasspathResource("publicCrt.pem")) .when(urlResolver).resolve(Mockito.any()); X509KeyLocationResolver keyLocationResolver = new X509KeyLocationResolver(contextInfo) { protected HttpsJwks initializeHttpsJwks(String loc) { return mockedHttpsJwks; } protected UrlStreamResolver getUrlResolver() { return urlResolver; } }; when(signature.getX509CertSha1ThumbprintHeaderValue()).thenReturn(x5t); assertEquals(key, keyLocationResolver.resolveKey(signature, emptyList())); }
@Test public void testLoadPemCertOnClassPath() throws Exception { JWTAuthContextInfo contextInfo = new JWTAuthContextInfo("publicCrt.pem", "issuer"); X509KeyLocationResolver keyLocationResolver = new X509KeyLocationResolver(contextInfo); when(signature.getX509CertSha1ThumbprintHeaderValue()).thenReturn(x5t); assertEquals(key, keyLocationResolver.resolveKey(signature, emptyList())); }
@Test public void testLoadJWKWithCertOnClassPathWithX5t() throws Exception { RsaJsonWebKey jwk = new RsaJsonWebKey(key); jwk.setOtherParameter("x5c", Collections.singletonList(x5c)); JWTAuthContextInfo contextInfo = new JWTAuthContextInfo(); contextInfo.setPublicKeyContent(jwk.toJson()); X509KeyLocationResolver keyLocationResolver = new X509KeyLocationResolver(contextInfo); when(signature.getX509CertSha1ThumbprintHeaderValue()).thenReturn(x5t); assertEquals(key, keyLocationResolver.resolveKey(signature, emptyList())); }
@Test public void testLoadJWKWithCertOnClassPathWithX5tS256() throws Exception { RsaJsonWebKey jwk = new RsaJsonWebKey(key); jwk.setOtherParameter("x5c", Collections.singletonList(x5c)); JWTAuthContextInfo contextInfo = new JWTAuthContextInfo(); contextInfo.setPublicKeyContent(jwk.toJson()); X509KeyLocationResolver keyLocationResolver = new X509KeyLocationResolver(contextInfo); when(signature.getX509CertSha256ThumbprintHeaderValue()).thenReturn(x5tS256); assertEquals(key, keyLocationResolver.resolveKey(signature, emptyList())); }
@Test public void testLoadJWKWithCertOnClassPathWithWrongX5tS256() throws Exception { expectedEx.expect(UnresolvableKeyException.class); RsaJsonWebKey jwk = new RsaJsonWebKey(key); jwk.setOtherParameter("x5c", Collections.singletonList(x5c)); JWTAuthContextInfo contextInfo = new JWTAuthContextInfo(); contextInfo.setPublicKeyContent(jwk.toJson()); X509KeyLocationResolver keyLocationResolver = new X509KeyLocationResolver(contextInfo); when(signature.getX509CertSha256ThumbprintHeaderValue()).thenReturn(x5tS256 + "1"); keyLocationResolver.resolveKey(signature, emptyList()); }