Skip to content

Conversation

@joshtemple
Copy link

Currently, if the user is not authorized to make a request, there is no token in the response so the user receives a KeyError, which is less informative than the HTTPError returned from the API.

Raising the HTTP error solves this.

headers=headers,
data=json.dumps(body).encode("utf-8"),
)
response.raise_for_status()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write a test for this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clundin25, I've attempted to write a test. Let me know if you'd like me to approach it differently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refresh method normally throws RefreshError (https://github.com/googleapis/google-auth-library-python/blob/main/google/auth/exceptions.py#L35), could you maybe check the status code and raise a RefreshError instead?

chalmerlowe pushed a commit that referenced this pull request Dec 19, 2025
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
chalmerlowe pushed a commit that referenced this pull request Dec 19, 2025
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
@chalmerlowe
Copy link
Contributor

Closing in deference to a PR #1897 that addresses some minor issues with this approach.

chalmerlowe pushed a commit that referenced this pull request Dec 20, 2025
…#1897)

Previously, `IDTokenCredentials.refresh` would raise a `KeyError` if the
response from the IAM server was `200 OK` but did not contain the
expected "token" field. This change wraps the token extraction in a
`try...except` block to catch `KeyError` (and `ValueError` for malformed
JSON) and raises a `google.auth.exceptions.RefreshError` instead, which
is the expected behavior for credential refresh failures.

I've added a new test `tests/test_impersonated_credentials.py` to
verify:
1.  A `200 OK` response with a missing token now raises `RefreshError`.
2.  Non-200 responses (e.g., 403) still raise `RefreshError` as before.

---
This PR replaces the PR #1167 from 3 years ago that addressed this
issue, but was less robust/not fully correct.

*PR created automatically by Jules for task
[3520893520890582850](https://jules.google.com/task/3520893520890582850)
started by @chalmerlowe*

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants