Skip to content
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
6 changes: 5 additions & 1 deletion packages/google-auth/google/auth/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,11 @@ def decode(token, certs=None, verify=True, audience=None, clock_skew_in_seconds=
claim_audience = payload.get("aud")
if isinstance(audience, str):
audience = [audience]
if claim_audience not in audience:
# The "aud" claim may be a single string or a list of strings.
claim_audiences = (
claim_audience if isinstance(claim_audience, list) else [claim_audience]
)
if not any(aud in audience for aud in claim_audiences):
raise exceptions.InvalidValue(
"Token has wrong audience {}, expected one of {}".format(
claim_audience, audience
Expand Down
19 changes: 19 additions & 0 deletions packages/google-auth/tests/test_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ def test_decode_valid_with_audience_list(token_factory):
assert payload["metadata"]["meta"] == "data"


def test_decode_valid_with_audience_claim_list(token_factory):
token = token_factory(
claims={"aud": ["audience@example.com", "another_audience@example.com"]}
)
payload = jwt.decode(
token, certs=PUBLIC_CERT_BYTES, audience="audience@example.com"
)
assert payload["aud"] == ["audience@example.com", "another_audience@example.com"]


def test_decode_valid_unverified(token_factory):
payload = jwt.decode(token_factory(), certs=OTHER_CERT_BYTES, verify=False)
assert payload["aud"] == "audience@example.com"
Expand Down Expand Up @@ -328,6 +338,15 @@ def test_decode_bad_token_wrong_audience_list(token_factory):
assert excinfo.match(r"Token has wrong audience")


def test_decode_bad_token_wrong_audience_claim_list(token_factory):
token = token_factory(
claims={"aud": ["audience2@example.com", "audience3@example.com"]}
)
with pytest.raises(ValueError) as excinfo:
jwt.decode(token, PUBLIC_CERT_BYTES, audience="audience@example.com")
assert excinfo.match(r"Token has wrong audience")


def test_decode_wrong_cert(token_factory):
with pytest.raises(ValueError) as excinfo:
jwt.decode(token_factory(), OTHER_CERT_BYTES)
Expand Down
Loading