1818import java .io .IOException ;
1919import java .net .MalformedURLException ;
2020import java .net .URL ;
21- import java .text .ParseException ;
22- import java .time .Instant ;
2321import java .util .Collections ;
24- import java .util .LinkedHashMap ;
2522import java .util .Map ;
2623
2724import com .nimbusds .jose .JWSAlgorithm ;
28- import com .nimbusds .jose .RemoteKeySourceException ;
2925import com .nimbusds .jose .jwk .source .JWKSource ;
3026import com .nimbusds .jose .jwk .source .RemoteJWKSet ;
3127import com .nimbusds .jose .proc .JWSKeySelector ;
3228import com .nimbusds .jose .proc .JWSVerificationKeySelector ;
3329import com .nimbusds .jose .proc .SecurityContext ;
3430import com .nimbusds .jose .util .Resource ;
3531import com .nimbusds .jose .util .ResourceRetriever ;
36- import com .nimbusds .jwt .JWT ;
37- import com .nimbusds .jwt .JWTClaimsSet ;
38- import com .nimbusds .jwt .JWTParser ;
39- import com .nimbusds .jwt .SignedJWT ;
4032import com .nimbusds .jwt .proc .ConfigurableJWTProcessor ;
4133import com .nimbusds .jwt .proc .DefaultJWTProcessor ;
4234
4739import org .springframework .http .RequestEntity ;
4840import org .springframework .http .ResponseEntity ;
4941import org .springframework .security .oauth2 .core .OAuth2TokenValidator ;
50- import org .springframework .security .oauth2 .core .OAuth2TokenValidatorResult ;
5142import org .springframework .security .oauth2 .jose .jws .JwsAlgorithms ;
5243import org .springframework .util .Assert ;
5344import org .springframework .web .client .RestOperations ;
6253 * <p>
6354 * <b>NOTE:</b> This implementation uses the Nimbus JOSE + JWT SDK internally.
6455 *
56+ * @deprecated Use {@link NimbusJwtDecoder} instead
57+ *
6558 * @author Joe Grandja
6659 * @author Josh Cummings
6760 * @since 5.0
6861 * @see JwtDecoder
62+ * @see NimbusJwtDecoder
6963 * @see <a target="_blank" href="https://tools.ietf.org/html/rfc7519">JSON Web Token (JWT)</a>
7064 * @see <a target="_blank" href="https://tools.ietf.org/html/rfc7515">JSON Web Signature (JWS)</a>
7165 * @see <a target="_blank" href="https://tools.ietf.org/html/rfc7517">JSON Web Key (JWK)</a>
7266 * @see <a target="_blank" href="https://connect2id.com/products/nimbus-jose-jwt">Nimbus JOSE + JWT SDK</a>
7367 */
68+ @ Deprecated
7469public final class NimbusJwtDecoderJwkSupport implements JwtDecoder {
75- private static final String DECODING_ERROR_MESSAGE_TEMPLATE =
76- "An error occurred while attempting to decode the Jwt: %s" ;
77-
7870 private final JWSAlgorithm jwsAlgorithm ;
79- private final ConfigurableJWTProcessor <SecurityContext > jwtProcessor ;
8071 private final RestOperationsResourceRetriever jwkSetRetriever = new RestOperationsResourceRetriever ();
8172
82- private Converter <Map <String , Object >, Map <String , Object >> claimSetConverter =
83- MappedJwtClaimSetConverter .withDefaults (Collections .emptyMap ());
84- private OAuth2TokenValidator <Jwt > jwtValidator = JwtValidators .createDefault ();
85-
73+ private NimbusJwtDecoder delegate ;
8674
8775 /**
8876 * Constructs a {@code NimbusJwtDecoderJwkSupport} using the provided parameters.
@@ -111,21 +99,18 @@ public NimbusJwtDecoderJwkSupport(String jwkSetUrl, String jwsAlgorithm) {
11199 this .jwsAlgorithm = JWSAlgorithm .parse (jwsAlgorithm );
112100 JWSKeySelector <SecurityContext > jwsKeySelector =
113101 new JWSVerificationKeySelector <>(this .jwsAlgorithm , jwkSource );
114- this . jwtProcessor = new DefaultJWTProcessor <>();
115- this . jwtProcessor .setJWSKeySelector (jwsKeySelector );
102+ ConfigurableJWTProcessor < SecurityContext > jwtProcessor = new DefaultJWTProcessor <>();
103+ jwtProcessor .setJWSKeySelector (jwsKeySelector );
116104
117105 // Spring Security validates the claim set independent from Nimbus
118- this .jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> {});
106+ jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> {});
107+
108+ this .delegate = new NimbusJwtDecoder (jwtProcessor );
119109 }
120110
121111 @ Override
122112 public Jwt decode (String token ) throws JwtException {
123- JWT jwt = this .parse (token );
124- if (jwt instanceof SignedJWT ) {
125- Jwt createdJwt = this .createJwt (token , jwt );
126- return this .validateJwt (createdJwt );
127- }
128- throw new JwtException ("Unsupported algorithm of " + jwt .getHeader ().getAlgorithm ());
113+ return this .delegate .decode (token );
129114 }
130115
131116 /**
@@ -135,7 +120,7 @@ public Jwt decode(String token) throws JwtException {
135120 */
136121 public void setJwtValidator (OAuth2TokenValidator <Jwt > jwtValidator ) {
137122 Assert .notNull (jwtValidator , "jwtValidator cannot be null" );
138- this .jwtValidator = jwtValidator ;
123+ this .delegate . setJwtValidator ( jwtValidator ) ;
139124 }
140125
141126 /**
@@ -145,57 +130,7 @@ public void setJwtValidator(OAuth2TokenValidator<Jwt> jwtValidator) {
145130 */
146131 public final void setClaimSetConverter (Converter <Map <String , Object >, Map <String , Object >> claimSetConverter ) {
147132 Assert .notNull (claimSetConverter , "claimSetConverter cannot be null" );
148- this .claimSetConverter = claimSetConverter ;
149- }
150-
151- private JWT parse (String token ) {
152- try {
153- return JWTParser .parse (token );
154- } catch (Exception ex ) {
155- throw new JwtException (String .format (DECODING_ERROR_MESSAGE_TEMPLATE , ex .getMessage ()), ex );
156- }
157- }
158-
159- private Jwt createJwt (String token , JWT parsedJwt ) {
160- Jwt jwt ;
161-
162- try {
163- // Verify the signature
164- JWTClaimsSet jwtClaimsSet = this .jwtProcessor .process (parsedJwt , null );
165-
166- Map <String , Object > headers = new LinkedHashMap <>(parsedJwt .getHeader ().toJSONObject ());
167- Map <String , Object > claims = this .claimSetConverter .convert (jwtClaimsSet .getClaims ());
168-
169- Instant expiresAt = (Instant ) claims .get (JwtClaimNames .EXP );
170- Instant issuedAt = (Instant ) claims .get (JwtClaimNames .IAT );
171- jwt = new Jwt (token , issuedAt , expiresAt , headers , claims );
172- } catch (RemoteKeySourceException ex ) {
173- if (ex .getCause () instanceof ParseException ) {
174- throw new JwtException (String .format (DECODING_ERROR_MESSAGE_TEMPLATE , "Malformed Jwk set" ));
175- } else {
176- throw new JwtException (String .format (DECODING_ERROR_MESSAGE_TEMPLATE , ex .getMessage ()), ex );
177- }
178- } catch (Exception ex ) {
179- if (ex .getCause () instanceof ParseException ) {
180- throw new JwtException (String .format (DECODING_ERROR_MESSAGE_TEMPLATE , "Malformed payload" ));
181- } else {
182- throw new JwtException (String .format (DECODING_ERROR_MESSAGE_TEMPLATE , ex .getMessage ()), ex );
183- }
184- }
185-
186- return jwt ;
187- }
188-
189- private Jwt validateJwt (Jwt jwt ){
190- OAuth2TokenValidatorResult result = this .jwtValidator .validate (jwt );
191- if (result .hasErrors ()) {
192- String description = result .getErrors ().iterator ().next ().getDescription ();
193- throw new JwtValidationException (
194- String .format (DECODING_ERROR_MESSAGE_TEMPLATE , description ),
195- result .getErrors ());
196- }
197-
198- return jwt ;
133+ this .delegate .setClaimSetConverter (claimSetConverter );
199134 }
200135
201136 /**
0 commit comments