-
Notifications
You must be signed in to change notification settings - Fork 346
Raise HTTP error if encountered #1167
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
Conversation
| headers=headers, | ||
| data=json.dumps(body).encode("utf-8"), | ||
| ) | ||
| response.raise_for_status() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
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
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
|
Closing in deference to a PR #1897 that addresses some minor issues with this approach. |
…#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>
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.