Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve deprecation warnings related to datetime.utcnow in python 3.12 #335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
brouberol marked this conversation as resolved.
Show resolved Hide resolved

# 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)
brouberol marked this conversation as resolved.
Show resolved Hide resolved
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