Skip to content

Commit

Permalink
Resolve deprecation warnings related to datetime.utcnow in python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
brouberol committed Nov 2, 2023
1 parent 4b0701b commit a1a3062
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
6 changes: 3 additions & 3 deletions jose/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .constants import ALGORITHMS
from .exceptions import ExpiredSignatureError, JWSError, JWTClaimsError, JWTError
from .utils import calculate_at_hash, timedelta_total_seconds
from .utils import calculate_at_hash, timedelta_total_seconds, utcnow


def encode(claims, key, algorithm=ALGORITHMS.HS256, headers=None, access_token=None):
Expand Down Expand Up @@ -281,7 +281,7 @@ def _validate_nbf(claims, leeway=0):
except ValueError:
raise JWTClaimsError("Not Before claim (nbf) must be an integer.")

now = timegm(datetime.utcnow().utctimetuple())
now = timegm(utcnow().utctimetuple())

if nbf > (now + leeway):
raise JWTClaimsError("The token is not yet valid (nbf)")
Expand Down Expand Up @@ -311,7 +311,7 @@ def _validate_exp(claims, leeway=0):
except ValueError:
raise JWTClaimsError("Expiration Time claim (exp) must be an integer.")

now = timegm(datetime.utcnow().utctimetuple())
now = timegm(utcnow().utctimetuple())

if exp < (now - leeway):
raise ExpiredSignatureError("Signature has expired.")
Expand Down
13 changes: 13 additions & 0 deletions jose/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import datetime
import struct

# Piggyback of the backends implementation of the function that converts a long
Expand Down Expand Up @@ -105,3 +106,15 @@ def ensure_binary(s):
if isinstance(s, str):
return s.encode("utf-8", "strict")
raise TypeError(f"not expecting type '{type(s)}'")



def utcnow():
try:
from datetime import UTC
except ImportError:
# datetime.datetime.utcnow was deprecated in favor of
# datetime.datetime.now(datetime.UTC) in python 3.12
return datetime.datetime.utcnow()
else:
return datetime.datetime.now(datetime.UTC)
23 changes: 12 additions & 11 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from jose import jws, jwt
from jose.exceptions import JWTError
from jose.utils import utcnow


@pytest.fixture
Expand Down Expand Up @@ -180,7 +181,7 @@ def test_leeway_is_int(self):
pass

def test_leeway_is_timedelta(self, claims, key):
nbf = datetime.utcnow() + timedelta(seconds=5)
nbf = utcnow() + timedelta(seconds=5)
leeway = timedelta(seconds=10)

claims = {
Expand Down Expand Up @@ -209,15 +210,15 @@ def test_nbf_not_int(self, key):
jwt.decode(token, key)

def test_nbf_datetime(self, key):
nbf = datetime.utcnow() - timedelta(seconds=5)
nbf = utcnow() - timedelta(seconds=5)

claims = {"nbf": nbf}

token = jwt.encode(claims, key)
jwt.decode(token, key)

def test_nbf_with_leeway(self, key):
nbf = datetime.utcnow() + timedelta(seconds=5)
nbf = utcnow() + timedelta(seconds=5)

claims = {
"nbf": nbf,
Expand All @@ -229,7 +230,7 @@ def test_nbf_with_leeway(self, key):
jwt.decode(token, key, options=options)

def test_nbf_in_future(self, key):
nbf = datetime.utcnow() + timedelta(seconds=5)
nbf = utcnow() + timedelta(seconds=5)

claims = {"nbf": nbf}

Expand All @@ -239,7 +240,7 @@ def test_nbf_in_future(self, key):
jwt.decode(token, key)

def test_nbf_skip(self, key):
nbf = datetime.utcnow() + timedelta(seconds=5)
nbf = utcnow() + timedelta(seconds=5)

claims = {"nbf": nbf}

Expand All @@ -261,15 +262,15 @@ def test_exp_not_int(self, key):
jwt.decode(token, key)

def test_exp_datetime(self, key):
exp = datetime.utcnow() + timedelta(seconds=5)
exp = utcnow() + timedelta(seconds=5)

claims = {"exp": exp}

token = jwt.encode(claims, key)
jwt.decode(token, key)

def test_exp_with_leeway(self, key):
exp = datetime.utcnow() - timedelta(seconds=5)
exp = utcnow() - timedelta(seconds=5)

claims = {
"exp": exp,
Expand All @@ -281,7 +282,7 @@ def test_exp_with_leeway(self, key):
jwt.decode(token, key, options=options)

def test_exp_in_past(self, key):
exp = datetime.utcnow() - timedelta(seconds=5)
exp = utcnow() - timedelta(seconds=5)

claims = {"exp": exp}

Expand All @@ -291,7 +292,7 @@ def test_exp_in_past(self, key):
jwt.decode(token, key)

def test_exp_skip(self, key):
exp = datetime.utcnow() - timedelta(seconds=5)
exp = utcnow() - timedelta(seconds=5)

claims = {"exp": exp}

Expand Down Expand Up @@ -504,8 +505,8 @@ def test_unverified_claims_object(self, claims, key):
[
("aud", "aud"),
("ait", "ait"),
("exp", datetime.utcnow() + timedelta(seconds=3600)),
("nbf", datetime.utcnow() - timedelta(seconds=5)),
("exp", utcnow() + timedelta(seconds=3600)),
("nbf", utcnow() - timedelta(seconds=5)),
("iss", "iss"),
("sub", "sub"),
("jti", "jti"),
Expand Down

0 comments on commit a1a3062

Please sign in to comment.