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 1, 2023
1 parent 4b0701b commit 0a72b34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions jose/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Mapping
except ImportError:
from collections import Mapping
from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC

from jose import jws

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(datetime.now(UTC).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(datetime.now(UTC).utctimetuple())

if exp < (now - leeway):
raise ExpiredSignatureError("Signature has expired.")
Expand Down
24 changes: 12 additions & 12 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
import json
from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC

import pytest

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

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

claims = {
Expand Down Expand Up @@ -209,15 +209,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 = datetime.now(UTC) - 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 = datetime.now(UTC) + timedelta(seconds=5)

claims = {
"nbf": nbf,
Expand All @@ -229,7 +229,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 = datetime.now(UTC) + timedelta(seconds=5)

claims = {"nbf": nbf}

Expand All @@ -239,7 +239,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 = datetime.now(UTC) + timedelta(seconds=5)

claims = {"nbf": nbf}

Expand All @@ -261,15 +261,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 = datetime.now(UTC) + 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 = datetime.now(UTC) - timedelta(seconds=5)

claims = {
"exp": exp,
Expand All @@ -281,7 +281,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 = datetime.now(UTC) - timedelta(seconds=5)

claims = {"exp": exp}

Expand All @@ -291,7 +291,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 = datetime.now(UTC) - timedelta(seconds=5)

claims = {"exp": exp}

Expand Down Expand Up @@ -504,8 +504,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", datetime.now(UTC) + timedelta(seconds=3600)),
("nbf", datetime.now(UTC) - timedelta(seconds=5)),
("iss", "iss"),
("sub", "sub"),
("jti", "jti"),
Expand Down

0 comments on commit 0a72b34

Please sign in to comment.