auth0 / jwks-rsa-java

MIT License
194 stars 73 forks source link

NetworkException not being thrown when using cached JwkProviderBuilder instead of directly UrlJwkProvider #165

Closed adFrej closed 1 year ago

adFrej commented 1 year ago

Describe the problem

As per: examples we should be able to catch NetworkException when getting JWK using cached JwkProviderBuilder. But in here that error is caught and replaced with SigningKeyNotFoundException (as ExecutionException includes NetworkException):

@Override
public Jwk get(final String keyId) throws JwkException {
    try {
        String cacheKey = keyId == null ? NULL_KID_KEY : keyId;
        return cache.get(cacheKey, new Callable<Jwk>() {
            @Override
            public Jwk call() throws Exception {
                return provider.get(keyId);
            }
        });
    } catch (ExecutionException e) {
        throw new SigningKeyNotFoundException("Failed to get key with kid " + keyId, e);
    }
}

This basically recreates the issue: #79.

What was the expected behavior?

NetworkException being thrown directly from: JwkProviderBuilder(URL(...)).cached(...).build().get(...) method when unable to connect to the url.

Reproduction

Environment

jimmyjames commented 1 year ago

Thanks @adFrej, we will look into this this week.

jimmyjames commented 1 year ago

We should be able to just unwrap the ExecutionException in the GuavaCachedJwkProvider:

@Override
public Jwk get(final String keyId) throws JwkException {
    try {
        String cacheKey = keyId == null ? NULL_KID_KEY : keyId;
        return cache.get(cacheKey, () -> provider.get(keyId));
    } catch (ExecutionException e) {
        if (e.getCause() instanceof JwkException) {
            throw (JwkException) e.getCause();
        }
        throw new JwkException("Unable to obtain key with kid " + keyId, e);
    }
}

I'll get a PR up shortly.

jimmyjames commented 1 year ago

169 should fix this 👍