Skip to content

Commit

Permalink
Merge pull request #1 from Flat-Irons/dev-timezones
Browse files Browse the repository at this point in the history
Added ability to use datetime objects that are timezone aware for tim…
  • Loading branch information
Flat-Irons authored Jun 27, 2023
2 parents 41fb08a + 2a4f84a commit f59d562
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion jwcrypto/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import time
import uuid
from datetime import datetime

from deprecated import deprecated

Expand Down Expand Up @@ -172,7 +173,9 @@ def __init__(self, header=None, claims=None, jwt=None, key=None,
"""Creates a JWT object.
:param header: A dict or a JSON string with the JWT Header data.
:param claims: A dict or a string with the JWT Claims data.
:param claims: A dict or a string with the JWT Claims data. If
the 'exp' or 'nbf' are datetime objects, they are automatically
converted to integer unix timestamps. Otherwise, they are left as is.
:param jwt: a 'raw' JWT token
:param key: A (:class:`jwcrypto.jwk.JWK`) key to deserialize
the token. A (:class:`jwcrypto.jwk.JWKSet`) can also be used.
Expand Down Expand Up @@ -225,6 +228,14 @@ def __init__(self, header=None, claims=None, jwt=None, key=None,
self._check_claims = check_claims

if claims is not None:
# Check for datetime objects
if 'exp' in claims and isinstance(claims['exp'], datetime):
# Check if timezone aware
claims['exp'] = self._check_and_convert_dt(claims['exp'], 'exp')
if 'nbf' in claims and isinstance(claims['nbf'], datetime):
# Check if timezone aware
claims['nbf'] = self._check_and_convert_dt(claims['nbf'], 'nbf')

self.claims = claims

if jwt is not None:
Expand Down Expand Up @@ -388,6 +399,18 @@ def expected_type(self, v):
else:
raise ValueError("Invalid value, must be 'JWS' or 'JWE'")

def _check_and_convert_dt(self, dt, claim_prop):
if (
dt.tzinfo is not None
and dt.tzinfo.utcoffset(dt) is not None
):
dt_timestamp = int(dt.timestamp())
return dt_timestamp
else:
raise ValueError(
f"'{claim_prop}' datetime object must be timezone aware"
)

def _add_optional_claim(self, name, claims):
if name in claims:
return
Expand Down

0 comments on commit f59d562

Please sign in to comment.