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

KeyError when generating IDTokenCredentials with bad permissions #1249

Closed
charlielito opened this issue Mar 14, 2023 · 0 comments · Fixed by #1315
Closed

KeyError when generating IDTokenCredentials with bad permissions #1249

charlielito opened this issue Mar 14, 2023 · 0 comments · Fixed by #1315

Comments

@charlielito
Copy link

charlielito commented Mar 14, 2023

When trying to generate a IDTokenCredentials token, if for some reason there is some sort of permissions error, the library crashes with a KeyError instead of providing useful info to fix the error.

Environment details

  • OS: Ubuntu 22.04
  • Python version: 3.10.6
  • pip version: 23.0
  • google-auth version: 2.16.0

Steps to reproduce

import google.auth
import google.auth.transport.requests
import google.oauth2.id_token
from google.auth import impersonated_credentials

target_principal = "[email protected]"
target_audience = "https://some_url.appspot.com"

credentials, project = google.auth.default()

target_credentials = impersonated_credentials.Credentials(
    source_credentials=credentials,
    target_principal=target_principal,
    target_scopes=["https://www.googleapis.com/auth/cloud-platform"],
    lifetime=120,
)

credentials = impersonated_credentials.IDTokenCredentials(
    target_credentials, target_audience=target_audience
)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
print(credentials.token)

And the traceback is

Traceback (most recent call last):
  File "/workspace/test.py", line 23, in <module>
    credentials.refresh(request)
  File "/usr/local/lib/python3.10/dist-packages/google/auth/impersonated_credentials.py", line 445, in refresh
    id_token = response.json()["token"]
KeyError: 'token'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment