@@ -63,7 +63,17 @@ def encode(claims, key, algorithm=ALGORITHMS.HS256, headers=None, access_token=N
63
63
return jws .sign (claims , key , headers = headers , algorithm = algorithm )
64
64
65
65
66
- def decode (token , key , algorithms = None , options = None , audience = None , issuer = None , subject = None , access_token = None ):
66
+ def decode (
67
+ token ,
68
+ key ,
69
+ algorithms = None ,
70
+ options = None ,
71
+ audience = None ,
72
+ issuer = None ,
73
+ subject = None ,
74
+ access_token = None ,
75
+ now = None ,
76
+ ):
67
77
"""Verifies a JWT string's signature and validates reserved claims.
68
78
69
79
Args:
@@ -91,6 +101,7 @@ def decode(token, key, algorithms=None, options=None, audience=None, issuer=None
91
101
claim set, then the access_token must be included, and it must match
92
102
the "at_hash" claim.
93
103
options (dict): A dictionary of options for skipping validation steps.
104
+ now (datetime): Current time. If not set, defaults to current system time.
94
105
95
106
defaults = {
96
107
'verify_signature': True,
@@ -179,6 +190,7 @@ def decode(token, key, algorithms=None, options=None, audience=None, issuer=None
179
190
algorithm = algorithm ,
180
191
access_token = access_token ,
181
192
options = defaults ,
193
+ now = now ,
182
194
)
183
195
184
196
return claims
@@ -271,7 +283,7 @@ def _validate_iat(claims):
271
283
raise JWTClaimsError ("Issued At claim (iat) must be an integer." )
272
284
273
285
274
- def _validate_nbf (claims , leeway = 0 ):
286
+ def _validate_nbf (now , claims , leeway = 0 ):
275
287
"""Validates that the 'nbf' claim is valid.
276
288
277
289
The "nbf" (not before) claim identifies the time before which the JWT
@@ -283,6 +295,7 @@ def _validate_nbf(claims, leeway=0):
283
295
NumericDate value. Use of this claim is OPTIONAL.
284
296
285
297
Args:
298
+ now (datetime): Current time.
286
299
claims (dict): The claims dictionary to validate.
287
300
leeway (int): The number of seconds of skew that is allowed.
288
301
"""
@@ -295,13 +308,13 @@ def _validate_nbf(claims, leeway=0):
295
308
except ValueError :
296
309
raise JWTClaimsError ("Not Before claim (nbf) must be an integer." )
297
310
298
- now = timegm (datetime . now ( UTC ) .utctimetuple ())
311
+ now = timegm (now .utctimetuple ())
299
312
300
313
if nbf > (now + leeway ):
301
314
raise JWTClaimsError ("The token is not yet valid (nbf)" )
302
315
303
316
304
- def _validate_exp (claims , leeway = 0 ):
317
+ def _validate_exp (now , claims , leeway = 0 ):
305
318
"""Validates that the 'exp' claim is valid.
306
319
307
320
The "exp" (expiration time) claim identifies the expiration time on
@@ -313,6 +326,7 @@ def _validate_exp(claims, leeway=0):
313
326
containing a NumericDate value. Use of this claim is OPTIONAL.
314
327
315
328
Args:
329
+ now (datetime): Current time.
316
330
claims (dict): The claims dictionary to validate.
317
331
leeway (int): The number of seconds of skew that is allowed.
318
332
"""
@@ -325,7 +339,7 @@ def _validate_exp(claims, leeway=0):
325
339
except ValueError :
326
340
raise JWTClaimsError ("Expiration Time claim (exp) must be an integer." )
327
341
328
- now = timegm (datetime . now ( UTC ) .utctimetuple ())
342
+ now = timegm (now .utctimetuple ())
329
343
330
344
if exp < (now - leeway ):
331
345
raise ExpiredSignatureError ("Signature has expired." )
@@ -472,7 +486,17 @@ def _validate_at_hash(claims, access_token, algorithm):
472
486
raise JWTClaimsError ("at_hash claim does not match access_token." )
473
487
474
488
475
- def _validate_claims (claims , audience = None , issuer = None , subject = None , algorithm = None , access_token = None , options = None ):
489
+ def _validate_claims (
490
+ claims ,
491
+ audience = None ,
492
+ issuer = None ,
493
+ subject = None ,
494
+ algorithm = None ,
495
+ access_token = None ,
496
+ options = None ,
497
+ now = None ,
498
+ ):
499
+
476
500
leeway = options .get ("leeway" , 0 )
477
501
478
502
if isinstance (leeway , timedelta ):
@@ -491,10 +515,12 @@ def _validate_claims(claims, audience=None, issuer=None, subject=None, algorithm
491
515
_validate_iat (claims )
492
516
493
517
if options .get ("verify_nbf" ):
494
- _validate_nbf (claims , leeway = leeway )
518
+ now = now or datetime .now (UTC )
519
+ _validate_nbf (now , claims , leeway = leeway )
495
520
496
521
if options .get ("verify_exp" ):
497
- _validate_exp (claims , leeway = leeway )
522
+ now = now or datetime .now (UTC )
523
+ _validate_exp (now , claims , leeway = leeway )
498
524
499
525
if options .get ("verify_aud" ):
500
526
_validate_aud (claims , audience = audience )
0 commit comments