18
18
import java .io .IOException ;
19
19
import java .net .MalformedURLException ;
20
20
import java .net .URL ;
21
- import java .text .ParseException ;
22
- import java .time .Instant ;
23
21
import java .util .Collections ;
24
- import java .util .LinkedHashMap ;
25
22
import java .util .Map ;
26
23
27
24
import com .nimbusds .jose .JWSAlgorithm ;
28
- import com .nimbusds .jose .RemoteKeySourceException ;
29
25
import com .nimbusds .jose .jwk .source .JWKSource ;
30
26
import com .nimbusds .jose .jwk .source .RemoteJWKSet ;
31
27
import com .nimbusds .jose .proc .JWSKeySelector ;
32
28
import com .nimbusds .jose .proc .JWSVerificationKeySelector ;
33
29
import com .nimbusds .jose .proc .SecurityContext ;
34
30
import com .nimbusds .jose .util .Resource ;
35
31
import 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 ;
40
32
import com .nimbusds .jwt .proc .ConfigurableJWTProcessor ;
41
33
import com .nimbusds .jwt .proc .DefaultJWTProcessor ;
42
34
47
39
import org .springframework .http .RequestEntity ;
48
40
import org .springframework .http .ResponseEntity ;
49
41
import org .springframework .security .oauth2 .core .OAuth2TokenValidator ;
50
- import org .springframework .security .oauth2 .core .OAuth2TokenValidatorResult ;
51
42
import org .springframework .security .oauth2 .jose .jws .JwsAlgorithms ;
52
43
import org .springframework .util .Assert ;
53
44
import org .springframework .web .client .RestOperations ;
62
53
* <p>
63
54
* <b>NOTE:</b> This implementation uses the Nimbus JOSE + JWT SDK internally.
64
55
*
56
+ * @deprecated Use {@link NimbusJwtDecoder} instead
57
+ *
65
58
* @author Joe Grandja
66
59
* @author Josh Cummings
67
60
* @since 5.0
68
61
* @see JwtDecoder
62
+ * @see NimbusJwtDecoder
69
63
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7519">JSON Web Token (JWT)</a>
70
64
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7515">JSON Web Signature (JWS)</a>
71
65
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7517">JSON Web Key (JWK)</a>
72
66
* @see <a target="_blank" href="https://connect2id.com/products/nimbus-jose-jwt">Nimbus JOSE + JWT SDK</a>
73
67
*/
68
+ @ Deprecated
74
69
public 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
-
78
70
private final JWSAlgorithm jwsAlgorithm ;
79
- private final ConfigurableJWTProcessor <SecurityContext > jwtProcessor ;
80
71
private final RestOperationsResourceRetriever jwkSetRetriever = new RestOperationsResourceRetriever ();
81
72
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 ;
86
74
87
75
/**
88
76
* Constructs a {@code NimbusJwtDecoderJwkSupport} using the provided parameters.
@@ -111,21 +99,18 @@ public NimbusJwtDecoderJwkSupport(String jwkSetUrl, String jwsAlgorithm) {
111
99
this .jwsAlgorithm = JWSAlgorithm .parse (jwsAlgorithm );
112
100
JWSKeySelector <SecurityContext > jwsKeySelector =
113
101
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 );
116
104
117
105
// 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 );
119
109
}
120
110
121
111
@ Override
122
112
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 );
129
114
}
130
115
131
116
/**
@@ -135,7 +120,7 @@ public Jwt decode(String token) throws JwtException {
135
120
*/
136
121
public void setJwtValidator (OAuth2TokenValidator <Jwt > jwtValidator ) {
137
122
Assert .notNull (jwtValidator , "jwtValidator cannot be null" );
138
- this .jwtValidator = jwtValidator ;
123
+ this .delegate . setJwtValidator ( jwtValidator ) ;
139
124
}
140
125
141
126
/**
@@ -145,57 +130,7 @@ public void setJwtValidator(OAuth2TokenValidator<Jwt> jwtValidator) {
145
130
*/
146
131
public final void setClaimSetConverter (Converter <Map <String , Object >, Map <String , Object >> claimSetConverter ) {
147
132
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 );
199
134
}
200
135
201
136
/**
0 commit comments