Skip to content

Commit

Permalink
feat: check 'iss' in verify_oauth2_token (#500)
Browse files Browse the repository at this point in the history
Co-authored-by: Tianzi Cai <tianzi@google.com>
  • Loading branch information
busunkim96 and anguillanneuf committed Jun 29, 2020
1 parent 06d7f97 commit c05b8b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion google/auth/transport/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ def configure_mtls_channel(self, client_cert_callback=None):
six.raise_from(new_exc, caught_exc)

try:
self._is_mtls, cert, key = google.auth.transport._mtls_helper.get_client_cert_and_key(
(
self._is_mtls,
cert,
key,
) = google.auth.transport._mtls_helper.get_client_cert_and_key(
client_cert_callback
)

Expand Down
16 changes: 15 additions & 1 deletion google/oauth2/id_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
"/securetoken@system.gserviceaccount.com"
)

_GOOGLE_ISSUERS = ["accounts.google.com", "https://accounts.google.com"]


def _fetch_certs(request, certs_url):
"""Fetches certificates.
Expand Down Expand Up @@ -140,11 +142,23 @@ def verify_oauth2_token(id_token, request, audience=None):
Returns:
Mapping[str, Any]: The decoded token.
Raises:
exceptions.GoogleAuthError: If the issuer is invalid.
"""
return verify_token(
idinfo = verify_token(
id_token, request, audience=audience, certs_url=_GOOGLE_OAUTH2_CERTS_URL
)

if idinfo["iss"] not in _GOOGLE_ISSUERS:
raise exceptions.GoogleAuthError(
"Wrong issuer. 'iss' should be one of the following: {}".format(
_GOOGLE_ISSUERS
)
)

return idinfo


def verify_firebase_token(id_token, request, audience=None):
"""Verifies an ID Token issued by Firebase Authentication.
Expand Down
11 changes: 11 additions & 0 deletions tests/oauth2/test_id_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def test_verify_token_args(_fetch_certs, decode):

@mock.patch("google.oauth2.id_token.verify_token", autospec=True)
def test_verify_oauth2_token(verify_token):
verify_token.return_value = {"iss": "accounts.google.com"}
result = id_token.verify_oauth2_token(
mock.sentinel.token, mock.sentinel.request, audience=mock.sentinel.audience
)
Expand All @@ -108,6 +109,16 @@ def test_verify_oauth2_token(verify_token):
)


@mock.patch("google.oauth2.id_token.verify_token", autospec=True)
def test_verify_oauth2_token_invalid_iss(verify_token):
verify_token.return_value = {"iss": "invalid_issuer"}

with pytest.raises(exceptions.GoogleAuthError):
id_token.verify_oauth2_token(
mock.sentinel.token, mock.sentinel.request, audience=mock.sentinel.audience
)


@mock.patch("google.oauth2.id_token.verify_token", autospec=True)
def test_verify_firebase_token(verify_token):
result = id_token.verify_firebase_token(
Expand Down

0 comments on commit c05b8b5

Please sign in to comment.