@@ -7,7 +7,6 @@ use jsonwebtoken::{
7
7
decode, errors:: ErrorKind , get_current_timestamp, Algorithm , DecodingKey , Validation ,
8
8
} ;
9
9
use rand:: Rng ;
10
- use serde:: { Deserialize , Serialize } ;
11
10
#[ cfg( feature = "std" ) ]
12
11
use std:: {
13
12
fs, io,
@@ -117,7 +116,8 @@ const JWT_SIGNATURE_ALGO: Algorithm = Algorithm::HS256;
117
116
///
118
117
/// The Engine API spec requires that just the `iat` (issued-at) claim is provided.
119
118
/// It ignores claims that are optional or additional for this specification.
120
- #[ derive( Copy , Clone , Debug , Serialize , Deserialize ) ]
119
+ #[ derive( Copy , Clone , Debug ) ]
120
+ #[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
121
121
pub struct Claims {
122
122
/// The "iat" value MUST be a number containing a NumericDate value.
123
123
/// According to the RFC A NumericDate represents the number of seconds since
@@ -215,6 +215,7 @@ impl JwtSecret {
215
215
/// - The JWT `exp` (expiration time) claim is validated by default if defined.
216
216
///
217
217
/// See also: [JWT Claims - Engine API specs](https://github.com/ethereum/execution-apis/blob/main/src/engine/authentication.md#jwt-claims)
218
+ #[ cfg( feature = "serde" ) ]
218
219
pub fn validate ( & self , jwt : & str ) -> Result < ( ) , JwtError > {
219
220
// Create a new validation object with the required signature algorithm
220
221
// and ensure that the `iat` claim is present. The `exp` claim is validated if defined.
@@ -250,6 +251,7 @@ impl JwtSecret {
250
251
251
252
/// Encode the header and claims given and sign the payload using the algorithm from the header
252
253
/// and the key.
254
+ #[ cfg( feature = "serde" ) ]
253
255
pub fn encode ( & self , claims : & Claims ) -> Result < String , jsonwebtoken:: errors:: Error > {
254
256
let bytes = & self . 0 ;
255
257
let key = jsonwebtoken:: EncodingKey :: from_secret ( bytes) ;
@@ -331,6 +333,7 @@ mod tests {
331
333
}
332
334
333
335
#[ test]
336
+ #[ cfg( feature = "serde" ) ]
334
337
fn validation_ok ( ) {
335
338
let secret = JwtSecret :: random ( ) ;
336
339
let claims = Claims { iat : get_current_timestamp ( ) , exp : Some ( 10000000000 ) } ;
@@ -342,6 +345,7 @@ mod tests {
342
345
}
343
346
344
347
#[ test]
348
+ #[ cfg( feature = "serde" ) ]
345
349
fn validation_with_current_time_ok ( ) {
346
350
let secret = JwtSecret :: random ( ) ;
347
351
let claims = Claims :: default ( ) ;
@@ -353,7 +357,7 @@ mod tests {
353
357
}
354
358
355
359
#[ test]
356
- #[ cfg( feature = "std" ) ]
360
+ #[ cfg( all ( feature = "std" , feature = "serde" ) ) ]
357
361
fn validation_error_iat_out_of_window ( ) {
358
362
let secret = JwtSecret :: random ( ) ;
359
363
@@ -379,6 +383,7 @@ mod tests {
379
383
}
380
384
381
385
#[ test]
386
+ #[ cfg( feature = "serde" ) ]
382
387
fn validation_error_exp_expired ( ) {
383
388
let secret = JwtSecret :: random ( ) ;
384
389
let claims = Claims { iat : get_current_timestamp ( ) , exp : Some ( 1 ) } ;
@@ -390,6 +395,7 @@ mod tests {
390
395
}
391
396
392
397
#[ test]
398
+ #[ cfg( feature = "serde" ) ]
393
399
fn validation_error_wrong_signature ( ) {
394
400
let secret_1 = JwtSecret :: random ( ) ;
395
401
let claims = Claims { iat : get_current_timestamp ( ) , exp : Some ( 10000000000 ) } ;
@@ -402,6 +408,7 @@ mod tests {
402
408
}
403
409
404
410
#[ test]
411
+ #[ cfg( feature = "serde" ) ]
405
412
fn validation_error_unsupported_algorithm ( ) {
406
413
let secret = JwtSecret :: random ( ) ;
407
414
let bytes = & secret. 0 ;
@@ -417,6 +424,7 @@ mod tests {
417
424
}
418
425
419
426
#[ test]
427
+ #[ cfg( feature = "serde" ) ]
420
428
fn valid_without_exp_claim ( ) {
421
429
let secret = JwtSecret :: random ( ) ;
422
430
0 commit comments