11/*
2- * Copyright 2002-2017 the original author or authors.
2+ * Copyright 2002-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2424import com .nimbusds .jwt .proc .DefaultJWTProcessor ;
2525import okhttp3 .mockwebserver .MockResponse ;
2626import okhttp3 .mockwebserver .MockWebServer ;
27+ import org .assertj .core .api .Assertions ;
2728import org .junit .Test ;
2829import org .junit .runner .RunWith ;
2930import org .powermock .core .classloader .annotations .PowerMockIgnore ;
3031import org .powermock .core .classloader .annotations .PrepareForTest ;
3132import org .powermock .modules .junit4 .PowerMockRunner ;
32-
33+ import org . springframework . http . RequestEntity ;
3334import org .springframework .security .oauth2 .jose .jws .JwsAlgorithms ;
35+ import org .springframework .web .client .RestTemplate ;
3436
3537import static org .assertj .core .api .AssertionsForClassTypes .assertThatCode ;
3638import static org .assertj .core .api .AssertionsForClassTypes .assertThatThrownBy ;
37- import static org .mockito .ArgumentMatchers .any ;
38- import static org .mockito .ArgumentMatchers .anyString ;
39- import static org .mockito .ArgumentMatchers .eq ;
39+ import static org .mockito .ArgumentMatchers .*;
4040import static org .mockito .Mockito .mock ;
41- import static org .powermock .api .mockito .PowerMockito .mockStatic ;
42- import static org .powermock .api .mockito .PowerMockito .when ;
43- import static org .powermock .api .mockito .PowerMockito .whenNew ;
41+ import static org .mockito .Mockito .verify ;
42+ import static org .powermock .api .mockito .PowerMockito .*;
4443
4544/**
4645 * Tests for {@link NimbusJwtDecoderJwkSupport}.
@@ -62,6 +61,8 @@ public class NimbusJwtDecoderJwkSupportTests {
6261 private static final String MALFORMED_JWT = "eyJhbGciOiJSUzI1NiJ9.eyJuYmYiOnt9LCJleHAiOjQ2ODQyMjUwODd9.guoQvujdWvd3xw7FYQEn4D6-gzM_WqFvXdmvAUNSLbxG7fv2_LLCNujPdrBHJoYPbOwS1BGNxIKQWS1tylvqzmr1RohQ-RZ2iAM1HYQzboUlkoMkcd8ENM__ELqho8aNYBfqwkNdUOyBFoy7Syu_w2SoJADw2RTjnesKO6CVVa05bW118pDS4xWxqC4s7fnBjmZoTn4uQ-Kt9YSQZQk8YQxkJSiyanozzgyfgXULA6mPu1pTNU3FVFaK1i1av_xtH_zAPgb647ZeaNe4nahgqC5h8nhOlm8W2dndXbwAt29nd2ZWBsru_QwZz83XSKLhTPFz-mPBByZZDsyBbIHf9A" ;
6362 private static final String UNSIGNED_JWT = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJleHAiOi0yMDMzMjI0OTcsImp0aSI6IjEyMyIsInR5cCI6IkpXVCJ9." ;
6463
64+ private NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
65+
6566 @ Test
6667 public void constructorWhenJwkSetUrlIsNullThenThrowIllegalArgumentException () {
6768 assertThatThrownBy (() -> new NimbusJwtDecoderJwkSupport (null ))
@@ -80,10 +81,15 @@ public void constructorWhenJwsAlgorithmIsNullThenThrowIllegalArgumentException()
8081 .isInstanceOf (IllegalArgumentException .class );
8182 }
8283
84+ @ Test
85+ public void setRestOperationsWhenNullThenThrowIllegalArgumentException () {
86+ Assertions .assertThatThrownBy (() -> this .jwtDecoder .setRestOperations (null ))
87+ .isInstanceOf (IllegalArgumentException .class );
88+ }
89+
8390 @ Test
8491 public void decodeWhenJwtInvalidThenThrowJwtException () {
85- NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
86- assertThatThrownBy (() -> jwtDecoder .decode ("invalid" ))
92+ assertThatThrownBy (() -> this .jwtDecoder .decode ("invalid" ))
8793 .isInstanceOf (JwtException .class );
8894 }
8995
@@ -103,16 +109,14 @@ public void decodeWhenExpClaimNullThenDoesNotThrowException() throws Exception {
103109 JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().audience ("resource1" ).build ();
104110 when (jwtProcessor .process (any (JWT .class ), eq (null ))).thenReturn (jwtClaimsSet );
105111
106- NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
112+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL );
107113 assertThatCode (() -> jwtDecoder .decode ("encoded-jwt" )).doesNotThrowAnyException ();
108114 }
109115
110116 // gh-5457
111117 @ Test
112- public void decodeWhenPlainJwtThenExceptionDoesNotMentionClass () throws Exception {
113- NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
114-
115- assertThatCode (() -> jwtDecoder .decode (UNSIGNED_JWT ))
118+ public void decodeWhenPlainJwtThenExceptionDoesNotMentionClass () {
119+ assertThatCode (() -> this .jwtDecoder .decode (UNSIGNED_JWT ))
116120 .isInstanceOf (JwtException .class )
117121 .hasMessageContaining ("Unsupported algorithm of none" );
118122 }
@@ -122,12 +126,11 @@ public void decodeWhenJwtIsMalformedThenReturnsStockException() throws Exception
122126 try ( MockWebServer server = new MockWebServer () ) {
123127 server .enqueue (new MockResponse ().setBody (JWK_SET ));
124128 String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
125-
126- NimbusJwtDecoderJwkSupport decoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
127-
128- assertThatCode (() -> decoder .decode (MALFORMED_JWT ))
129+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
130+ assertThatCode (() -> jwtDecoder .decode (MALFORMED_JWT ))
129131 .isInstanceOf (JwtException .class )
130132 .hasMessage ("An error occurred while attempting to decode the Jwt: Malformed payload" );
133+ server .shutdown ();
131134 }
132135 }
133136
@@ -136,28 +139,39 @@ public void decodeWhenJwkResponseIsMalformedThenReturnsStockException() throws E
136139 try ( MockWebServer server = new MockWebServer () ) {
137140 server .enqueue (new MockResponse ().setBody (MALFORMED_JWK_SET ));
138141 String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
139-
140- NimbusJwtDecoderJwkSupport decoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
141-
142- assertThatCode (() -> decoder .decode (SIGNED_JWT ))
142+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
143+ assertThatCode (() -> jwtDecoder .decode (SIGNED_JWT ))
143144 .isInstanceOf (JwtException .class )
144145 .hasMessage ("An error occurred while attempting to decode the Jwt: Malformed Jwk set" );
146+ server .shutdown ();
145147 }
146148 }
147149
148150 @ Test
149- public void decodeWhenJwkEndpointIsUnresponsiveThenRetrunsJwtException () throws Exception {
151+ public void decodeWhenJwkEndpointIsUnresponsiveThenReturnsJwtException () throws Exception {
150152 try ( MockWebServer server = new MockWebServer () ) {
151153 server .enqueue (new MockResponse ().setBody (MALFORMED_JWK_SET ));
152154 String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
153-
154- NimbusJwtDecoderJwkSupport decoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
155-
156- server .shutdown ();
157-
158- assertThatCode (() -> decoder .decode (SIGNED_JWT ))
155+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
156+ assertThatCode (() -> jwtDecoder .decode (SIGNED_JWT ))
159157 .isInstanceOf (JwtException .class )
160158 .hasMessageContaining ("An error occurred while attempting to decode the Jwt" );
159+ server .shutdown ();
160+ }
161+ }
162+
163+ // gh-5603
164+ @ Test
165+ public void decodeWhenCustomRestOperationsSetThenUsed () throws Exception {
166+ try ( MockWebServer server = new MockWebServer () ) {
167+ server .enqueue (new MockResponse ().setBody (JWK_SET ));
168+ String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
169+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
170+ RestTemplate restTemplate = spy (new RestTemplate ());
171+ jwtDecoder .setRestOperations (restTemplate );
172+ assertThatCode (() -> jwtDecoder .decode (SIGNED_JWT )).doesNotThrowAnyException ();
173+ verify (restTemplate ).exchange (any (RequestEntity .class ), eq (String .class ));
174+ server .shutdown ();
161175 }
162176 }
163177}
0 commit comments