|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth2.jwt; |
18 | 18 |
|
19 | | -import java.security.KeyFactory; |
20 | | -import java.security.NoSuchAlgorithmException; |
21 | | -import java.security.PrivateKey; |
22 | | -import java.security.interfaces.RSAPrivateKey; |
23 | | -import java.security.interfaces.RSAPublicKey; |
24 | | -import java.security.spec.EncodedKeySpec; |
25 | | -import java.security.spec.InvalidKeySpecException; |
26 | | -import java.security.spec.X509EncodedKeySpec; |
27 | | -import java.text.ParseException; |
28 | | -import java.time.Instant; |
29 | | -import java.util.Arrays; |
30 | | -import java.util.Base64; |
31 | | -import java.util.Collections; |
32 | | -import java.util.Date; |
33 | | -import java.util.List; |
34 | | -import java.util.Map; |
35 | | - |
36 | | -import javax.crypto.SecretKey; |
37 | | - |
38 | 19 | import com.nimbusds.jose.JOSEException; |
39 | 20 | import com.nimbusds.jose.JOSEObjectType; |
40 | 21 | import com.nimbusds.jose.JWSAlgorithm; |
41 | 22 | import com.nimbusds.jose.JWSHeader; |
42 | 23 | import com.nimbusds.jose.JWSSigner; |
43 | 24 | import com.nimbusds.jose.crypto.MACSigner; |
44 | 25 | import com.nimbusds.jose.crypto.RSASSASigner; |
| 26 | +import com.nimbusds.jose.jwk.JWKSet; |
45 | 27 | import com.nimbusds.jose.jwk.source.JWKSource; |
46 | 28 | import com.nimbusds.jose.proc.BadJOSEException; |
47 | 29 | import com.nimbusds.jose.proc.DefaultJOSEObjectTypeVerifier; |
|
57 | 39 | import org.junit.jupiter.api.BeforeAll; |
58 | 40 | import org.junit.jupiter.api.Test; |
59 | 41 | import org.mockito.ArgumentCaptor; |
60 | | - |
61 | 42 | import org.springframework.cache.Cache; |
62 | 43 | import org.springframework.cache.concurrent.ConcurrentMapCache; |
63 | 44 | import org.springframework.core.ParameterizedTypeReference; |
|
75 | 56 | import org.springframework.web.client.RestClientException; |
76 | 57 | import org.springframework.web.client.RestOperations; |
77 | 58 |
|
78 | | -import static org.assertj.core.api.Assertions.assertThat; |
79 | | -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
80 | | -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
81 | | -import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 59 | +import javax.crypto.SecretKey; |
| 60 | +import java.security.KeyFactory; |
| 61 | +import java.security.NoSuchAlgorithmException; |
| 62 | +import java.security.PrivateKey; |
| 63 | +import java.security.interfaces.RSAPrivateKey; |
| 64 | +import java.security.interfaces.RSAPublicKey; |
| 65 | +import java.security.spec.EncodedKeySpec; |
| 66 | +import java.security.spec.InvalidKeySpecException; |
| 67 | +import java.security.spec.X509EncodedKeySpec; |
| 68 | +import java.text.ParseException; |
| 69 | +import java.time.Instant; |
| 70 | +import java.util.Arrays; |
| 71 | +import java.util.Base64; |
| 72 | +import java.util.Collections; |
| 73 | +import java.util.Date; |
| 74 | +import java.util.List; |
| 75 | +import java.util.Map; |
| 76 | + |
| 77 | +import static org.assertj.core.api.Assertions.*; |
82 | 78 | import static org.mockito.ArgumentMatchers.any; |
83 | 79 | import static org.mockito.ArgumentMatchers.eq; |
84 | 80 | import static org.mockito.BDDMockito.given; |
85 | | -import static org.mockito.Mockito.mock; |
86 | | -import static org.mockito.Mockito.times; |
87 | | -import static org.mockito.Mockito.verify; |
88 | | -import static org.mockito.Mockito.verifyNoInteractions; |
89 | | -import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 81 | +import static org.mockito.Mockito.*; |
90 | 82 |
|
91 | 83 | /** |
92 | 84 | * Tests for {@link NimbusJwtDecoder} |
@@ -559,6 +551,22 @@ public void decodeWhenUsingSecretKeyWithKidThenStillUsesKey() throws Exception { |
559 | 551 | // @formatter:on |
560 | 552 | } |
561 | 553 |
|
| 554 | + // gh-7056 |
| 555 | + @Test |
| 556 | + public void decodeWhenUsingJwkSource() throws Exception { |
| 557 | + JWKSource<SecurityContext> source = (a, b) -> { |
| 558 | + try { |
| 559 | + return JWKSet.parse(JWK_SET).getKeys(); |
| 560 | + } |
| 561 | + catch (ParseException e) { |
| 562 | + throw new RuntimeException(e); |
| 563 | + } |
| 564 | + }; |
| 565 | + NimbusJwtDecoder decoder = NimbusJwtDecoder.withJwkSource(source).build(); |
| 566 | + Jwt jwt = decoder.decode(SIGNED_JWT); |
| 567 | + assertThat(jwt.getClaimAsString("sub")).isEqualTo("test-subject"); |
| 568 | + } |
| 569 | + |
562 | 570 | // gh-8730 |
563 | 571 | @Test |
564 | 572 | public void withSecretKeyWhenUsingCustomTypeHeaderThenSuccessfullyDecodes() throws Exception { |
|
0 commit comments