Skip to content

Commit 98a5c1d

Browse files
JulianMaurinpre-commit-ci[bot]auvipy
authored
Update audience typing (#782)
* fix(api_jwt): update audience typing & type checking * doc(api): update decode.audience typing * feat(test_api_jwt): ensure audience as bytes raises error * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refacto(api_jwt): precise typing Co-authored-by: Julian Maurin <[email protected]> Update jwt/api_jwt.py Co-authored-by: Julian Maurin <[email protected]> fix(jwt/api_jwt.py): backport future annotations * fix: handle audience=0 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Asif Saif Uddin <[email protected]>
1 parent 0bef0fb commit 98a5c1d

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ API Reference
6262
if ``verify_exp``, ``verify_iat``, and ``verify_nbf`` respectively
6363
is set to ``True``).
6464

65-
:param Iterable audience: optional, the value for ``verify_aud`` check
65+
:param Union[str, Iterable] audience: optional, the value for ``verify_aud`` check
6666
:param str issuer: optional, the value for ``verify_iss`` check
6767
:param float leeway: a time margin in seconds for the expiration check
6868
:rtype: dict

jwt/api_jwt.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
import warnings
35
from calendar import timegm
@@ -76,7 +78,7 @@ def decode_complete(
7678
detached_payload: Optional[bytes] = None,
7779
# passthrough arguments to _validate_claims
7880
# consider putting in options
79-
audience: Optional[str] = None,
81+
audience: Optional[Union[str, Iterable[str]]] = None,
8082
issuer: Optional[str] = None,
8183
leeway: Union[int, float, timedelta] = 0,
8284
# kwargs
@@ -150,7 +152,7 @@ def decode(
150152
detached_payload: Optional[bytes] = None,
151153
# passthrough arguments to _validate_claims
152154
# consider putting in options
153-
audience: Optional[str] = None,
155+
audience: Optional[Union[str, Iterable[str]]] = None,
154156
issuer: Optional[str] = None,
155157
leeway: Union[int, float, timedelta] = 0,
156158
# kwargs
@@ -180,8 +182,8 @@ def _validate_claims(self, payload, options, audience=None, issuer=None, leeway=
180182
if isinstance(leeway, timedelta):
181183
leeway = leeway.total_seconds()
182184

183-
if not isinstance(audience, (bytes, str, type(None), Iterable)):
184-
raise TypeError("audience must be a string, iterable, or None")
185+
if audience is not None and not isinstance(audience, (str, Iterable)):
186+
raise TypeError("audience must be a string, iterable or None")
185187

186188
self._validate_required_claims(payload, options)
187189

tests/test_api_jwt.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_decode_with_invalid_audience_param_throws_exception(self, jwt):
119119
jwt.decode(example_jwt, secret, audience=1, algorithms=["HS256"])
120120

121121
exception = context.value
122-
assert str(exception) == "audience must be a string, iterable, or None"
122+
assert str(exception) == "audience must be a string, iterable or None"
123123

124124
def test_decode_with_nonlist_aud_claim_throws_exception(self, jwt):
125125
secret = "secret"
@@ -419,6 +419,14 @@ def test_raise_exception_invalid_audience(self, jwt):
419419
with pytest.raises(InvalidAudienceError):
420420
jwt.decode(token, "secret", audience="urn-me", algorithms=["HS256"])
421421

422+
def test_raise_exception_audience_as_bytes(self, jwt):
423+
payload = {"some": "payload", "aud": ["urn:me", "urn:someone-else"]}
424+
token = jwt.encode(payload, "secret")
425+
with pytest.raises(InvalidAudienceError):
426+
jwt.decode(
427+
token, "secret", audience="urn:me".encode(), algorithms=["HS256"]
428+
)
429+
422430
def test_raise_exception_invalid_audience_in_array(self, jwt):
423431
payload = {
424432
"some": "payload",

0 commit comments

Comments
 (0)