diff --git a/packages/google-auth/google/auth/jwt.py b/packages/google-auth/google/auth/jwt.py index 827d893bd14f..ea2fcf7a91d1 100644 --- a/packages/google-auth/google/auth/jwt.py +++ b/packages/google-auth/google/auth/jwt.py @@ -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 diff --git a/packages/google-auth/tests/test_jwt.py b/packages/google-auth/tests/test_jwt.py index 7347dab47818..69f96d10311a 100644 --- a/packages/google-auth/tests/test_jwt.py +++ b/packages/google-auth/tests/test_jwt.py @@ -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" @@ -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)