From 10aa9743ed7e790a2fa14f44d4d55a91ce1c7026 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Fri, 10 Apr 2020 16:39:01 -0600 Subject: [PATCH] Polish NimbusJwtDecoder - Follow convention to prefix member variable references with "this." - Reduce stack trace when IOException is thrown - Name tests to follow conventions Issue gh-8332 --- .../security/oauth2/jwt/NimbusJwtDecoder.java | 9 ++++++++- .../security/oauth2/jwt/NimbusJwtDecoderTests.java | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoder.java b/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoder.java index 6963cb0a61..b8e805fdfd 100644 --- a/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoder.java +++ b/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoder.java @@ -365,7 +365,14 @@ public final class NimbusJwtDecoder implements JwtDecoder { public Resource retrieveResource(URL url) throws IOException { String jwkSet; try { - jwkSet = cache.get(url.toString(), () -> resourceRetriever.retrieveResource(url).getContent()); + jwkSet = this.cache.get(url.toString(), + () -> this.resourceRetriever.retrieveResource(url).getContent()); + } catch (Cache.ValueRetrievalException ex) { + Throwable thrownByValueLoader = ex.getCause(); + if (thrownByValueLoader instanceof IOException) { + throw (IOException) thrownByValueLoader; + } + throw new IOException(thrownByValueLoader); } catch (Exception ex) { throw new IOException(ex); } diff --git a/oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoderTests.java b/oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoderTests.java index 7ca6469d6d..9e3608b3be 100644 --- a/oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoderTests.java +++ b/oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/NimbusJwtDecoderTests.java @@ -255,7 +255,7 @@ public class NimbusJwtDecoderTests { } @Test - public void shouldThrowJwtExceptionWhenJwkSetEndpointHasNotRespondedAndCacheIsConfigured() throws Exception { + public void decodeWhenJwkEndpointIsUnresponsiveAndCacheIsConfiguredThenReturnsJwtException() throws Exception { try ( MockWebServer server = new MockWebServer() ) { Cache cache = new ConcurrentMapCache("test-jwk-set-cache"); String jwkSetUri = server.url("/.well-known/jwks.json").toString(); @@ -287,7 +287,7 @@ public class NimbusJwtDecoderTests { } @Test - public void shouldThrowIllegalArgumentExceptionWhenJwkSetCacheIsNull() { + public void cacheWhenNullThenThrowsException() { NimbusJwtDecoder.JwkSetUriJwtDecoderBuilder builder = withJwkSetUri(JWK_SET_URI); Assertions.assertThatCode(() -> builder.cache(null)).isInstanceOf(IllegalArgumentException.class); } @@ -465,7 +465,7 @@ public class NimbusJwtDecoderTests { } @Test - public void shouldStoreRetrievedJwkSetToCache() { + public void decodeWhenCacheThenStoreRetrievedJwkSetToCache() { // given Cache cache = new ConcurrentMapCache("test-jwk-set-cache"); RestOperations restOperations = mock(RestOperations.class); @@ -487,7 +487,7 @@ public class NimbusJwtDecoderTests { } @Test - public void shouldDecodeJwtUsingJwkSetCache() { + public void decodeWhenCacheThenRetrieveFromCache() { // given RestOperations restOperations = mock(RestOperations.class); Cache cache = mock(Cache.class); @@ -505,7 +505,7 @@ public class NimbusJwtDecoderTests { } @Test - public void shouldThrowJwtExceptionWhenExceptionOccurredWhileRetrievingJwkSetInsideCachingRetriever() { + public void decodeWhenCacheIsConfiguredAndValueLoaderErrorsThenThrowsJwtException() { // given Cache cache = new ConcurrentMapCache("test-jwk-set-cache"); RestOperations restOperations = mock(RestOperations.class);