|
17 | 17 |
|
18 | 18 | import java.util.Arrays;
|
19 | 19 | import java.util.Collections;
|
| 20 | +import java.util.List; |
20 | 21 | import java.util.Map;
|
21 | 22 |
|
22 | 23 | import com.nimbusds.jose.JWSAlgorithm;
|
|
31 | 32 | import org.assertj.core.api.Assertions;
|
32 | 33 | import org.junit.Test;
|
33 | 34 | import org.junit.runner.RunWith;
|
| 35 | +import org.mockito.ArgumentCaptor; |
34 | 36 | import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
35 | 37 | import org.powermock.core.classloader.annotations.PrepareForTest;
|
36 | 38 | import org.powermock.modules.junit4.PowerMockRunner;
|
37 | 39 |
|
38 | 40 | import org.springframework.core.convert.converter.Converter;
|
| 41 | +import org.springframework.http.HttpStatus; |
| 42 | +import org.springframework.http.MediaType; |
39 | 43 | import org.springframework.http.RequestEntity;
|
| 44 | +import org.springframework.http.ResponseEntity; |
40 | 45 | import org.springframework.security.oauth2.core.OAuth2Error;
|
41 | 46 | import org.springframework.security.oauth2.core.OAuth2TokenValidator;
|
42 | 47 | import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
|
43 | 48 | import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
|
| 49 | +import org.springframework.web.client.RestOperations; |
44 | 50 | import org.springframework.web.client.RestTemplate;
|
45 | 51 |
|
46 | 52 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -76,6 +82,8 @@ public class NimbusJwtDecoderJwkSupportTests {
|
76 | 82 | private static final String MALFORMED_JWT = "eyJhbGciOiJSUzI1NiJ9.eyJuYmYiOnt9LCJleHAiOjQ2ODQyMjUwODd9.guoQvujdWvd3xw7FYQEn4D6-gzM_WqFvXdmvAUNSLbxG7fv2_LLCNujPdrBHJoYPbOwS1BGNxIKQWS1tylvqzmr1RohQ-RZ2iAM1HYQzboUlkoMkcd8ENM__ELqho8aNYBfqwkNdUOyBFoy7Syu_w2SoJADw2RTjnesKO6CVVa05bW118pDS4xWxqC4s7fnBjmZoTn4uQ-Kt9YSQZQk8YQxkJSiyanozzgyfgXULA6mPu1pTNU3FVFaK1i1av_xtH_zAPgb647ZeaNe4nahgqC5h8nhOlm8W2dndXbwAt29nd2ZWBsru_QwZz83XSKLhTPFz-mPBByZZDsyBbIHf9A";
|
77 | 83 | private static final String UNSIGNED_JWT = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJleHAiOi0yMDMzMjI0OTcsImp0aSI6IjEyMyIsInR5cCI6IkpXVCJ9.";
|
78 | 84 |
|
| 85 | + private static final MediaType APPLICATION_JWK_SET_JSON = new MediaType("application", "jwk-set+json"); |
| 86 | + |
79 | 87 | private NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport(JWK_SET_URL, JWS_ALGORITHM);
|
80 | 88 |
|
81 | 89 | @Test
|
@@ -256,4 +264,19 @@ public void setClaimSetConverterWhenIsNullThenThrowsIllegalArgumentException() {
|
256 | 264 | assertThatCode(() -> jwtDecoder.setClaimSetConverter(null))
|
257 | 265 | .isInstanceOf(IllegalArgumentException.class);
|
258 | 266 | }
|
| 267 | + |
| 268 | + // gh-7290 |
| 269 | + @Test |
| 270 | + public void decodeWhenJwkSetRequestedThenAcceptHeaderJsonAndJwkSetJson() { |
| 271 | + RestOperations restOperations = mock(RestOperations.class); |
| 272 | + when(restOperations.exchange(any(RequestEntity.class), eq(String.class))) |
| 273 | + .thenReturn(new ResponseEntity<>(JWK_SET, HttpStatus.OK)); |
| 274 | + NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport(JWK_SET_URL); |
| 275 | + jwtDecoder.setRestOperations(restOperations); |
| 276 | + jwtDecoder.decode(SIGNED_JWT); |
| 277 | + ArgumentCaptor<RequestEntity> requestEntityCaptor = ArgumentCaptor.forClass(RequestEntity.class); |
| 278 | + verify(restOperations).exchange(requestEntityCaptor.capture(), eq(String.class)); |
| 279 | + List<MediaType> acceptHeader = requestEntityCaptor.getValue().getHeaders().getAccept(); |
| 280 | + assertThat(acceptHeader).contains(MediaType.APPLICATION_JSON, APPLICATION_JWK_SET_JSON); |
| 281 | + } |
259 | 282 | }
|
0 commit comments