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

Add decode timezone support #516

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ API Reference
.. deprecated:: 1.2.0
Use ``verify_exp`` instead

* ``timezone`` change the timezone used for decoding and validation, expects tzinfo object

:param iterable audience: optional, the value for ``verify_aud`` check
:param str issuer: optional, the value for ``verify_iss`` check
Expand Down
5 changes: 3 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 .algorithms import Algorithm, get_default_algorithms # NOQA
from .api_jws import PyJWS
Expand Down Expand Up @@ -36,6 +36,7 @@ def _get_default_options():
"verify_iat": True,
"verify_aud": True,
"verify_iss": True,
"timezone": timezone.utc, # type: tzinfo
"require": [],
}

Expand Down Expand Up @@ -132,7 +133,7 @@ def _validate_claims(

self._validate_required_claims(payload, options)

now = timegm(datetime.utcnow().utctimetuple())
now = timegm(datetime.now(tz=options.get("timezone")).utctimetuple())

if "iat" in payload and options.get("verify_iat"):
self._validate_iat(payload, now, leeway)
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tests =
pytest>=6.0.0,<7.0.0
coverage[toml]==5.0.4
requests-mock>=1.7.0,<2.0.0
pytz >= 2020.1
dev =
sphinx
sphinx-rtd-theme
Expand Down
24 changes: 24 additions & 0 deletions tests/test_api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from decimal import Decimal

import pytest
import pytz

from jwt.api_jwt import PyJWT
from jwt.exceptions import (
Expand Down Expand Up @@ -588,6 +589,29 @@ def test_decode_with_verify_exp_option(self, jwt, payload):
options={"verify_exp": True},
)

def test_decode_with_timezone_and_verify_exp_options(self, jwt, payload):
timezone = pytz.timezone('US/Pacific')
current_time = timegm(datetime.now(tz=timezone).utctimetuple())
payload["iat"] = current_time
payload["exp"] = current_time - 1
secret = "secret"
jwt_message = jwt.encode(payload, secret)

jwt.decode(
jwt_message,
secret,
algorithms=["HS256"],
options={"timezone": timezone, "verify_exp": False},
)

with pytest.raises(ExpiredSignatureError):
jwt.decode(
jwt_message,
secret,
algorithms=["HS256"],
options={"timezone": timezone, "verify_exp": True,},
)

def test_decode_with_optional_algorithms(self, jwt, payload):
secret = "secret"
jwt_message = jwt.encode(payload, secret)
Expand Down