Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check id token error response #1315

Merged
merged 1 commit into from
May 31, 2023
Merged
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
18 changes: 13 additions & 5 deletions google/auth/impersonated_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,19 @@ def refresh(self, request):
self._target_credentials._source_credentials, auth_request=request
)

response = authed_session.post(
url=iam_sign_endpoint,
headers=headers,
data=json.dumps(body).encode("utf-8"),
)
try:
response = authed_session.post(
url=iam_sign_endpoint,
headers=headers,
data=json.dumps(body).encode("utf-8"),
)
finally:
authed_session.close()

if response.status_code != http_client.OK:
raise exceptions.RefreshError(
"Error getting ID token: {}".format(response.json())
)

id_token = response.json()["token"]
self.token = id_token
Expand Down
Binary file modified system_tests/secrets.tar.enc
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/test_impersonated_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,27 @@ def test_refresh_failure_unauthorzed(self, mock_donor_credentials):
assert not credentials.valid
assert credentials.expired

def test_refresh_failure(self):
credentials = self.make_credentials(lifetime=None)
credentials.expiry = None
credentials.token = "token"
id_creds = impersonated_credentials.IDTokenCredentials(
credentials, target_audience="audience"
)

response = mock.create_autospec(transport.Response, instance=False)
response.status_code = http_client.UNAUTHORIZED
response.json = mock.Mock(return_value="failed to get ID token")

with mock.patch(
"google.auth.transport.requests.AuthorizedSession.post",
return_value=response,
):
with pytest.raises(exceptions.RefreshError) as excinfo:
id_creds.refresh(None)

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

def test_refresh_failure_http_error(self, mock_donor_credentials):
credentials = self.make_credentials(lifetime=None)

Expand Down