Skip to content

Commit 9e6ae24

Browse files
committed
fix: update datetime usage for compatibility with Python versions
Refactor datetime imports and usage to ensure compatibility with Python 3.12 and 3.13. Replace deprecated datetime.utcnow() with datetime.now(UTC) and handle ImportError for older versions.
1 parent 15f9ea6 commit 9e6ae24

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

jose/jwt.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
except ImportError:
77
from collections import Mapping
88

9-
from datetime import UTC, datetime, timedelta
9+
try:
10+
from datetime import UTC, datetime, timedelta
11+
utc_now = datetime.now(UTC) # Preferred in Python 3.13+
12+
except ImportError:
13+
from datetime import datetime, timedelta, timezone
14+
utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
15+
UTC = timezone.utc
1016

1117
from jose import jws
1218

@@ -386,7 +392,7 @@ def _validate_sub(claims, subject=None):
386392
"sub" value is a case-sensitive string containing a StringOrURI
387393
value. Use of this claim is OPTIONAL.
388394
389-
Args:
395+
Arg
390396
claims (dict): The claims dictionary to validate.
391397
subject (str): The subject of the token.
392398
"""

tests/test_jwt.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import base64
22
import json
3-
from datetime import UTC, datetime, timedelta
3+
4+
try:
5+
from datetime import UTC, datetime, timedelta
6+
utc_now = datetime.now(UTC) # Preferred in Python 3.13+
7+
except ImportError:
8+
from datetime import datetime, timedelta, timezone
9+
utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
10+
UTC = timezone.utc
11+
412

513
import pytest
614

@@ -84,7 +92,9 @@ def return_encoded_array(token, key, algorithms, verify=True):
8492

8593
jws.verify = return_encoded_array
8694

87-
with pytest.raises(JWTError, match="Invalid payload string: must be a json object"):
95+
with pytest.raises(
96+
JWTError, match="Invalid payload string: must be a json object"
97+
):
8898
jwt.decode(token, "secret", ["HS256"])
8999
finally:
90100
jws.verify = old_jws_verify
@@ -144,20 +154,35 @@ def test_deterministic_headers(self):
144154

145155
# manually decode header to compare it to known good
146156
decoded_headers1 = base64url_decode(encoded_headers1.encode("utf-8"))
147-
assert decoded_headers1 == b"""{"alg":"HS256","another_key":"another_value","kid":"my-key-id","typ":"JWT"}"""
157+
assert (
158+
decoded_headers1
159+
== b"""{"alg":"HS256","another_key":"another_value","kid":"my-key-id","typ":"JWT"}"""
160+
)
148161

149162
def test_encode(self, claims, key):
150163
expected = (
151-
("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" ".eyJhIjoiYiJ9" ".xNtk2S0CNbCBZX_f67pFgGRugaP1xi2ICfet3nwOSxw"),
152-
("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ".eyJhIjoiYiJ9" ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"),
164+
(
165+
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"
166+
".eyJhIjoiYiJ9"
167+
".xNtk2S0CNbCBZX_f67pFgGRugaP1xi2ICfet3nwOSxw"
168+
),
169+
(
170+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
171+
".eyJhIjoiYiJ9"
172+
".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"
173+
),
153174
)
154175

155176
encoded = jwt.encode(claims, key)
156177

157178
assert encoded in expected
158179

159180
def test_decode(self, claims, key):
160-
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ".eyJhIjoiYiJ9" ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"
181+
token = (
182+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
183+
".eyJhIjoiYiJ9"
184+
".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"
185+
)
161186

162187
decoded = jwt.decode(token, key)
163188

@@ -504,8 +529,8 @@ def test_unverified_claims_object(self, claims, key):
504529
[
505530
("aud", "aud"),
506531
("ait", "ait"),
507-
("exp", datetime.now(UTC) + timedelta(seconds=3600)),
508-
("nbf", datetime.now(UTC) - timedelta(seconds=5)),
532+
("exp", utc_now + timedelta(seconds=3600)),
533+
("nbf", utc_now - timedelta(seconds=5)),
509534
("iss", "iss"),
510535
("sub", "sub"),
511536
("jti", "jti"),

0 commit comments

Comments
 (0)