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

Do not use utc_now on module level #372

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 3 additions & 6 deletions jose/jwt.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import json
from calendar import timegm
from datetime import datetime, timedelta

try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping

try:
from datetime import UTC, datetime, timedelta

utc_now = datetime.now(UTC) # Preferred in Python 3.13+
from datetime import UTC # Preferred in Python 3.13+
except ImportError:
from datetime import datetime, timedelta, timezone

utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
UTC = timezone.utc
UTC = timezone.utc # Preferred in Python 3.12 and below

from jose import jws

Expand Down
15 changes: 7 additions & 8 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import base64
import json
from datetime import datetime, timedelta

try:
from datetime import UTC, datetime, timedelta

utc_now = datetime.now(UTC) # Preferred in Python 3.13+
from datetime import UTC # Preferred in Python 3.13+
except ImportError:
from datetime import datetime, timedelta, timezone

utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
from datetime import timezone # Preferred in Python 3.12 and below
UTC = timezone.utc


Expand Down Expand Up @@ -514,14 +511,16 @@ def test_unverified_claims_object(self, claims, key):
[
("aud", "aud"),
("ait", "ait"),
("exp", utc_now + timedelta(seconds=3600)),
("nbf", utc_now - timedelta(seconds=5)),
("exp", lambda: datetime.now(UTC) + timedelta(seconds=3600)),
("nbf", lambda: datetime.now(UTC) - timedelta(seconds=5)),
("iss", "iss"),
("sub", "sub"),
("jti", "jti"),
],
)
def test_require(self, claims, key, claim, value):
if callable(value):
value = value()
options = {"require_" + claim: True, "verify_" + claim: False}

token = jwt.encode(claims, key)
Expand Down