@@ -232,17 +232,34 @@ pub fn decode<T: DeserializeOwned>(
232
232
return Err ( new_error ( ErrorKind :: InvalidAlgorithm ) ) ;
233
233
}
234
234
235
- let decoder = decoder_factory ( & header. alg , key) ? . with_validation ( validation ) ?;
235
+ let verifying_provider = jwt_verifier_factory ( & header. alg , key) ?;
236
236
237
- decoder . decode ( token)
237
+ _decode ( token, validation , verifying_provider )
238
238
}
239
239
240
- /// Return the correct decoder based on the `algorithm`.
241
- fn decoder_factory ( algorithm : & Algorithm , key : & DecodingKey ) -> Result < JwtDecoder > {
240
+ /// # Todo
241
+ ///
242
+ /// - Documentation
243
+ pub fn _decode < T : DeserializeOwned > (
244
+ token : & str ,
245
+ validation : & Validation ,
246
+ verifying_provider : Box < dyn JwtVerifier > ,
247
+ ) -> Result < TokenData < T > > {
248
+ let ( header, claims) = verify_signature ( token, validation, verifying_provider) ?;
249
+
250
+ let decoded_claims = DecodedJwtPartClaims :: from_jwt_part_claims ( claims) ?;
251
+ let claims = decoded_claims. deserialize ( ) ?;
252
+ validate ( decoded_claims. deserialize ( ) ?, validation) ?;
253
+
254
+ Ok ( TokenData { header, claims } )
255
+ }
256
+
257
+ /// Return the correct [`JwtVerifier`] based on the `algorithm`.
258
+ fn jwt_verifier_factory ( algorithm : & Algorithm , key : & DecodingKey ) -> Result < Box < dyn JwtVerifier > > {
242
259
let jwt_encoder = match algorithm {
243
- Algorithm :: HS256 => JwtDecoder :: hs_256 ( key. try_into ( ) ?) ?,
244
- Algorithm :: HS384 => JwtDecoder :: hs_384 ( key. try_into ( ) ?) ?,
245
- Algorithm :: HS512 => JwtDecoder :: hs_512 ( key. try_into ( ) ?) ?,
260
+ Algorithm :: HS256 => Box :: new ( Hs256 :: new ( key. try_into ( ) ?) ?) as Box < dyn JwtVerifier > ,
261
+ Algorithm :: HS384 => Box :: new ( Hs384 :: new ( key. try_into ( ) ?) ?) as Box < dyn JwtVerifier > ,
262
+ Algorithm :: HS512 => Box :: new ( Hs512 :: new ( key. try_into ( ) ?) ?) as Box < dyn JwtVerifier > ,
246
263
Algorithm :: ES256 => todo ! ( ) ,
247
264
Algorithm :: ES384 => todo ! ( ) ,
248
265
Algorithm :: RS256 => todo ! ( ) ,
@@ -287,139 +304,40 @@ pub fn decode_header(token: &str) -> Result<Header> {
287
304
Header :: from_encoded ( header)
288
305
}
289
306
290
- /// A builder style JWT decoder
291
- ///
292
- /// # Examples
293
- ///
294
- /// ```
295
- /// use jsonwebtoken::{JwtDecoder, HmacSecret};
296
- /// use serde::{Serialize, Deserialize};
307
+ /// Verify signature of a JWT, and return header object and raw payload
297
308
///
298
- /// #[derive(Debug, Serialize, Deserialize)]
299
- /// struct Claims {
300
- /// sub: String,
301
- /// company: String,
302
- /// exp: usize,
303
- /// }
304
- ///
305
- /// let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUiLCJleHAiOjI1MzI1MjQ4OTF9.9r56oF7ZliOBlOAyiOFperTGxBtPykRQiWNFxhDCW98";
306
- ///
307
- /// let hmac_secret = HmacSecret::from_secret(b"secret");
308
- ///
309
- /// let claims = JwtDecoder::hs_256(hmac_secret)
310
- /// .unwrap()
311
- /// .decode::<Claims>(&token)
312
- /// .unwrap();
313
- /// ```
314
- pub struct JwtDecoder {
309
+ /// If the token or its signature is invalid, it will return an error.
310
+ fn verify_signature < ' a > (
311
+ token : & ' a str ,
312
+ validation : & Validation ,
315
313
verifying_provider : Box < dyn JwtVerifier > ,
316
- validation : Validation ,
317
- }
318
-
319
- impl JwtDecoder {
320
- /// Create a new [`JwtDecoder`] with any `verifying_provider` that implements the [`JwtVerifier`] trait.
321
- pub fn from_verifier < V : JwtVerifier + ' static > ( verifying_provider : V ) -> Self {
322
- Self :: from_boxed_verifiyer ( Box :: new ( verifying_provider) )
314
+ ) -> Result < ( Header , & ' a str ) > {
315
+ if validation. validate_signature && validation. algorithms . is_empty ( ) {
316
+ return Err ( new_error ( ErrorKind :: MissingAlgorithm ) ) ;
323
317
}
324
318
325
- /// Create a new [`JwtDecoder`] with any `verifying_provider` implements the [`JwtVerifier`] trait.
326
- pub fn from_boxed_verifiyer ( verifying_provider : Box < dyn JwtVerifier > ) -> Self {
327
- let validation = Validation :: new ( verifying_provider. algorithm ( ) ) ;
319
+ // Todo: This behaviour is currently not captured anywhere.
320
+ // if validation.validate_signature {
321
+ // for alg in &validation.algorithms {
322
+ // if key.family != alg.family() {
323
+ // return Err(new_error(ErrorKind::InvalidAlgorithm));
324
+ // }
325
+ // }
326
+ // }
328
327
329
- Self { verifying_provider, validation }
330
- }
331
-
332
- /// Provide custom a custom validation configuration.
333
- ///
334
- /// # Examples
335
- ///
336
- /// ```
337
- /// use jsonwebtoken::{JwtDecoder, HmacSecret, Validation, Algorithm};
338
- ///
339
- /// let hmac_secret = HmacSecret::from_secret(b"secret");
340
- /// let mut validation = Validation::new(Algorithm::HS256);
341
- /// validation.leeway = 5;
342
- ///
343
- /// let jwt_decoder = JwtDecoder::hs_256(hmac_secret)
344
- /// .unwrap()
345
- /// .with_validation(&validation)
346
- /// .unwrap();
347
- /// ```
348
- pub fn with_validation ( mut self , validation : & Validation ) -> Result < Self > {
349
- // Check that the validation contains the correct algorithm
350
- if validation. validate_signature
351
- && !validation. algorithms . contains ( & self . verifying_provider . algorithm ( ) )
352
- {
353
- return Err ( new_error ( crate :: errors:: ErrorKind :: InvalidAlgorithm ) ) ;
354
- }
355
-
356
- self . validation = validation. clone ( ) ;
357
- Ok ( self )
358
- }
359
-
360
- /// Decode and verify a JWT `token` using the `verifying_provider` and `validation` of the [`JwtDecoder`]
361
- pub fn decode < T : DeserializeOwned > ( & self , token : & str ) -> Result < TokenData < T > > {
362
- let ( header, claims) = self . verify_signature ( token) ?;
363
-
364
- let decoded_claims = DecodedJwtPartClaims :: from_jwt_part_claims ( claims) ?;
365
- let claims = decoded_claims. deserialize ( ) ?;
366
- validate ( decoded_claims. deserialize ( ) ?, & self . validation ) ?;
367
-
368
- Ok ( TokenData { header, claims } )
369
- }
370
-
371
- /// Verify signature of a JWT, and return header object and raw payload
372
- ///
373
- /// If the token or its signature is invalid, it will return an error.
374
- fn verify_signature < ' a > ( & self , token : & ' a str ) -> Result < ( Header , & ' a str ) > {
375
- if self . validation . validate_signature && self . validation . algorithms . is_empty ( ) {
376
- return Err ( new_error ( ErrorKind :: MissingAlgorithm ) ) ;
377
- }
328
+ let ( signature, message) = expect_two ! ( token. rsplitn( 2 , '.' ) ) ;
329
+ let ( payload, header) = expect_two ! ( message. rsplitn( 2 , '.' ) ) ;
330
+ let header = Header :: from_encoded ( header) ?;
378
331
379
- // Todo: This behaviour is currently not captured anywhere.
380
- // if validation.validate_signature {
381
- // for alg in &validation.algorithms {
382
- // if key.family != alg.family() {
383
- // return Err(new_error(ErrorKind::InvalidAlgorithm));
384
- // }
385
- // }
386
- // }
387
-
388
- let ( signature, message) = expect_two ! ( token. rsplitn( 2 , '.' ) ) ;
389
- let ( payload, header) = expect_two ! ( message. rsplitn( 2 , '.' ) ) ;
390
- let header = Header :: from_encoded ( header) ?;
391
-
392
- if self . validation . validate_signature && !self . validation . algorithms . contains ( & header. alg ) {
393
- return Err ( new_error ( ErrorKind :: InvalidAlgorithm ) ) ;
394
- }
395
-
396
- if self . validation . validate_signature
397
- && self . verifying_provider . verify ( message. as_bytes ( ) , & b64_decode ( signature) ?) . is_err ( )
398
- {
399
- return Err ( new_error ( ErrorKind :: InvalidSignature ) ) ;
400
- }
401
-
402
- Ok ( ( header, payload) )
403
- }
404
-
405
- /// Create new [`JwtDecoder`] with the `HS256` algorithm.
406
- pub fn hs_256 ( secret : HmacSecret ) -> Result < JwtDecoder > {
407
- let verifying_provider = Box :: new ( Hs256 :: new ( secret) ?) ;
408
-
409
- Ok ( JwtDecoder :: from_boxed_verifiyer ( verifying_provider) )
332
+ if validation. validate_signature && !validation. algorithms . contains ( & header. alg ) {
333
+ return Err ( new_error ( ErrorKind :: InvalidAlgorithm ) ) ;
410
334
}
411
335
412
- /// Create new [`JwtDecoder`] with the `HS384` algorithm.
413
- pub fn hs_384 ( secret : HmacSecret ) -> Result < JwtDecoder > {
414
- let verifying_provider = Box :: new ( Hs384 :: new ( secret) ?) ;
415
-
416
- Ok ( JwtDecoder :: from_boxed_verifiyer ( verifying_provider) )
336
+ if validation. validate_signature
337
+ && verifying_provider. verify ( message. as_bytes ( ) , & b64_decode ( signature) ?) . is_err ( )
338
+ {
339
+ return Err ( new_error ( ErrorKind :: InvalidSignature ) ) ;
417
340
}
418
341
419
- /// Create new [`JwtDecoder`] with the `HS512` algorithm.
420
- pub fn hs_512 ( secret : HmacSecret ) -> Result < JwtDecoder > {
421
- let verifying_provider = Box :: new ( Hs512 :: new ( secret) ?) ;
422
-
423
- Ok ( JwtDecoder :: from_boxed_verifiyer ( verifying_provider) )
424
- }
342
+ Ok ( ( header, payload) )
425
343
}
0 commit comments