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

Use timezone package as Python 3.5+ is required #694

Merged
merged 2 commits into from
Oct 6, 2021
Merged
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
11 changes: 6 additions & 5 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ datetime, which will be converted into an int. For example:
.. code-block:: python

jwt.encode({"exp": 1371720939}, "secret")
jwt.encode({"exp": datetime.utcnow()}, "secret")
jwt.encode({"exp": datetime.now(tz=timezone.utc)}, "secret")

Expiration time is automatically verified in `jwt.decode()` and raises
`jwt.ExpiredSignatureError` if the expiration time is in the past:
Expand All @@ -133,7 +133,7 @@ Expiration time is automatically verified in `jwt.decode()` and raises
...

Expiration time will be compared to the current UTC time (as given by
`timegm(datetime.utcnow().utctimetuple())`), so be sure to use a UTC timestamp
`timegm(datetime.now(tz=timezone.utc).utctimetuple())`), so be sure to use a UTC timestamp
or datetime in encoding.

You can turn off expiration time verification with the `verify_exp` parameter in the options argument.
Expand All @@ -147,7 +147,8 @@ you can set a leeway of 10 seconds in order to have some margin:
.. code-block:: python

jwt_payload = jwt.encode(
{"exp": datetime.datetime.utcnow() + datetime.timedelta(seconds=30)}, "secret"
{"exp": datetime.datetime.now(tz=timezone.utc) + datetime.timedelta(seconds=30)},
"secret",
)

time.sleep(32)
Expand Down Expand Up @@ -181,7 +182,7 @@ The `nbf` claim works similarly to the `exp` claim above.
.. code-block:: python

jwt.encode({"nbf": 1371720939}, "secret")
jwt.encode({"nbf": datetime.utcnow()}, "secret")
jwt.encode({"nbf": datetime.now(tz=timezone.utc)}, "secret")

Issuer Claim (iss)
~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -259,7 +260,7 @@ Issued At Claim (iat)
.. code-block:: python

jwt.encode({"iat": 1371720939}, "secret")
jwt.encode({"iat": datetime.utcnow()}, "secret")
jwt.encode({"iat": datetime.now(tz=timezone.utc)}, "secret")

Requiring Presence of Claims
----------------------------
Expand Down
4 changes: 2 additions & 2 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from calendar import timegm
from collections.abc import Iterable, Mapping
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional, Type, Union

from . import api_jws
Expand Down Expand Up @@ -133,7 +133,7 @@ def _validate_claims(self, payload, options, audience, issuer, leeway):

self._validate_required_claims(payload, options)

now = timegm(datetime.utcnow().utctimetuple())
now = timegm(datetime.now(tz=timezone.utc).utctimetuple())

if "iat" in payload and options["verify_iat"]:
self._validate_iat(payload, now, leeway)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_api_jwt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import time
from calendar import timegm
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from decimal import Decimal

import pytest
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_decode_raises_exception_if_aud_is_none(self, jwt):

def test_encode_datetime(self, jwt):
secret = "secret"
current_datetime = datetime.utcnow()
current_datetime = datetime.now(tz=timezone.utc)
payload = {
"exp": current_datetime,
"iat": current_datetime,
Expand Down Expand Up @@ -499,7 +499,7 @@ def test_skip_check_audience(self, jwt):
def test_skip_check_exp(self, jwt):
payload = {
"some": "payload",
"exp": datetime.utcnow() - timedelta(days=1),
"exp": datetime.now(tz=timezone.utc) - timedelta(days=1),
}
token = jwt.encode(payload, "secret")
jwt.decode(
Expand Down Expand Up @@ -576,7 +576,7 @@ def test_skip_check_signature(self, jwt):
def test_skip_check_iat(self, jwt):
payload = {
"some": "payload",
"iat": datetime.utcnow() + timedelta(days=1),
"iat": datetime.now(tz=timezone.utc) + timedelta(days=1),
}
token = jwt.encode(payload, "secret")
jwt.decode(
Expand All @@ -589,7 +589,7 @@ def test_skip_check_iat(self, jwt):
def test_skip_check_nbf(self, jwt):
payload = {
"some": "payload",
"nbf": datetime.utcnow() + timedelta(days=1),
"nbf": datetime.now(tz=timezone.utc) + timedelta(days=1),
}
token = jwt.encode(payload, "secret")
jwt.decode(
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
from calendar import timegm
from datetime import datetime
from datetime import datetime, timezone

import pytest

from jwt.algorithms import has_crypto


def utc_timestamp():
return timegm(datetime.utcnow().utctimetuple())
return timegm(datetime.now(tz=timezone.utc).utctimetuple())


def key_path(key_name):
Expand Down