Skip to content

Commit c746d1a

Browse files
fix: raise RefreshError for missing token in impersonated credentials
Instead of crashing with a KeyError when the ID token is missing from the response (even if the status code is 200), raise a proper RefreshError. Fixes #1167
1 parent 1123f87 commit c746d1a

File tree

2 files changed

+22
-77
lines changed

2 files changed

+22
-77
lines changed

tests/test_impersonated_credentials.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,28 @@ def test_refresh_failure(self):
761761

762762
assert excinfo.match("Error getting ID token")
763763

764+
def test_refresh_failure_missing_token_in_200_response(self):
765+
credentials = self.make_credentials(lifetime=None)
766+
credentials.expiry = None
767+
credentials.token = "token"
768+
id_creds = impersonated_credentials.IDTokenCredentials(
769+
credentials, target_audience="audience"
770+
)
771+
772+
# Response has 200 OK status but is missing the "token" field
773+
response = mock.create_autospec(transport.Response, instance=False)
774+
response.status_code = http_client.OK
775+
response.json = mock.Mock(return_value={"not_token": "something"})
776+
777+
with mock.patch(
778+
"google.auth.transport.requests.AuthorizedSession.post",
779+
return_value=response,
780+
):
781+
with pytest.raises(exceptions.RefreshError) as excinfo:
782+
id_creds.refresh(None)
783+
784+
assert excinfo.match("No ID token in response")
785+
764786
def test_refresh_failure_http_error(self, mock_donor_credentials):
765787
credentials = self.make_credentials(lifetime=None)
766788

tests/test_issue_1167.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)