|
| 1 | +# Copyright 2022 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [auth_cloud_idtoken_impersonated_credentials] |
| 16 | + |
| 17 | +import google |
| 18 | +from google.auth import impersonated_credentials |
| 19 | +import google.auth.transport.requests |
| 20 | + |
| 21 | + |
| 22 | +def idtoken_from_impersonated_credentials( |
| 23 | + impersonated_service_account: str, scope: str, target_audience: str): |
| 24 | + """ |
| 25 | + Use a service account (SA1) to impersonate as another service account (SA2) and obtain id token |
| 26 | + for the impersonated account. |
| 27 | + To obtain token for SA2, SA1 should have the "roles/iam.serviceAccountTokenCreator" permission |
| 28 | + on SA2. |
| 29 | +
|
| 30 | + Args: |
| 31 | + impersonated_service_account: The name of the privilege-bearing service account for whom the credential is created. |
| 32 | + Examples: name@project.service.gserviceaccount.com |
| 33 | +
|
| 34 | + scope: Provide the scopes that you might need to request to access Google APIs, |
| 35 | + depending on the level of access you need. |
| 36 | + For this example, we use the cloud-wide scope and use IAM to narrow the permissions. |
| 37 | + https://cloud.google.com/docs/authentication#authorization_for_services |
| 38 | + For more information, see: https://developers.google.com/identity/protocols/oauth2/scopes |
| 39 | +
|
| 40 | + target_audience: The service name for which the id token is requested. Service name refers to the |
| 41 | + logical identifier of an API service, such as "iap.googleapis.com". |
| 42 | + Examples: iap.googleapis.com |
| 43 | + """ |
| 44 | + |
| 45 | + # Construct the GoogleCredentials object which obtains the default configuration from your |
| 46 | + # working environment. |
| 47 | + credentials, project_id = google.auth.default() |
| 48 | + |
| 49 | + # Create the impersonated credential. |
| 50 | + target_credentials = impersonated_credentials.Credentials( |
| 51 | + source_credentials=credentials, |
| 52 | + target_principal=impersonated_service_account, |
| 53 | + # delegates: The chained list of delegates required to grant the final accessToken. |
| 54 | + # For more information, see: |
| 55 | + # https://cloud.google.com/iam/docs/create-short-lived-credentials-direct#sa-credentials-permissions |
| 56 | + # Delegate is NOT USED here. |
| 57 | + delegates=[], |
| 58 | + target_scopes=[scope], |
| 59 | + lifetime=300) |
| 60 | + |
| 61 | + # Set the impersonated credential, target audience and token options. |
| 62 | + id_creds = impersonated_credentials.IDTokenCredentials( |
| 63 | + target_credentials, |
| 64 | + target_audience=target_audience, |
| 65 | + include_email=True) |
| 66 | + |
| 67 | + # Get the ID token. |
| 68 | + # Once you've obtained the ID token, use it to make an authenticated call |
| 69 | + # to the target audience. |
| 70 | + request = google.auth.transport.requests.Request() |
| 71 | + id_creds.refresh(request) |
| 72 | + # token = id_creds.token |
| 73 | + print("Generated ID token.") |
| 74 | + |
| 75 | +# [auth_cloud_idtoken_impersonated_credentials] |
0 commit comments