salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 1 | # Copyright 2018 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 | """Google Cloud Impersonated credentials. |
| 16 | |
| 17 | This module provides authentication for applications where local credentials |
| 18 | impersonates a remote service account using `IAM Credentials API`_. |
| 19 | |
| 20 | This class can be used to impersonate a service account as long as the original |
| 21 | Credential object has the "Service Account Token Creator" role on the target |
| 22 | service account. |
| 23 | |
| 24 | .. _IAM Credentials API: |
| 25 | https://cloud.google.com/iam/credentials/reference/rest/ |
| 26 | """ |
| 27 | |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 28 | import base64 |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 29 | import copy |
| 30 | from datetime import datetime |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 31 | import http.client |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 32 | import json |
| 33 | |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 34 | from google.auth import _helpers |
| 35 | from google.auth import credentials |
| 36 | from google.auth import exceptions |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 37 | from google.auth import jwt |
| 38 | from google.auth.transport.requests import AuthorizedSession |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 39 | |
| 40 | _DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds |
| 41 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 42 | _IAM_SCOPE = ["https://www.googleapis.com/auth/iam"] |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 43 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 44 | _IAM_ENDPOINT = ( |
| 45 | "https://iamcredentials.googleapis.com/v1/projects/-" |
| 46 | + "/serviceAccounts/{}:generateAccessToken" |
| 47 | ) |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 48 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 49 | _IAM_SIGN_ENDPOINT = ( |
| 50 | "https://iamcredentials.googleapis.com/v1/projects/-" |
| 51 | + "/serviceAccounts/{}:signBlob" |
| 52 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 53 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 54 | _IAM_IDTOKEN_ENDPOINT = ( |
| 55 | "https://iamcredentials.googleapis.com/v1/" |
| 56 | + "projects/-/serviceAccounts/{}:generateIdToken" |
| 57 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 58 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 59 | _REFRESH_ERROR = "Unable to acquire impersonated credentials" |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 60 | |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 61 | _DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds |
| 62 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 63 | _DEFAULT_TOKEN_URI = "https://oauth2.googleapis.com/token" |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 64 | |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 65 | |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 66 | def _make_iam_token_request( |
| 67 | request, principal, headers, body, iam_endpoint_override=None |
| 68 | ): |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 69 | """Makes a request to the Google Cloud IAM service for an access token. |
| 70 | Args: |
| 71 | request (Request): The Request object to use. |
| 72 | principal (str): The principal to request an access token for. |
| 73 | headers (Mapping[str, str]): Map of headers to transmit. |
| 74 | body (Mapping[str, str]): JSON Payload body for the iamcredentials |
| 75 | API call. |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 76 | iam_endpoint_override (Optiona[str]): The full IAM endpoint override |
| 77 | with the target_principal embedded. This is useful when supporting |
| 78 | impersonation with regional endpoints. |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 79 | |
| 80 | Raises: |
arithmetic1728 | 9d5a9a9 | 2020-06-03 10:47:36 -0700 | [diff] [blame] | 81 | google.auth.exceptions.TransportError: Raised if there is an underlying |
| 82 | HTTP connection error |
| 83 | google.auth.exceptions.RefreshError: Raised if the impersonated |
| 84 | credentials are not available. Common reasons are |
| 85 | `iamcredentials.googleapis.com` is not enabled or the |
| 86 | `Service Account Token Creator` is not assigned |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 87 | """ |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 88 | iam_endpoint = iam_endpoint_override or _IAM_ENDPOINT.format(principal) |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 89 | |
Bu Sun Kim | a57a770 | 2020-01-10 13:17:34 -0800 | [diff] [blame] | 90 | body = json.dumps(body).encode("utf-8") |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 91 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 92 | response = request(url=iam_endpoint, method="POST", headers=headers, body=body) |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 93 | |
arithmetic1728 | 9b7228e | 2020-05-06 17:11:01 -0700 | [diff] [blame] | 94 | # support both string and bytes type response.data |
arithmetic1728 | e115bae | 2020-05-06 16:00:17 -0700 | [diff] [blame] | 95 | response_body = ( |
| 96 | response.data.decode("utf-8") |
| 97 | if hasattr(response.data, "decode") |
| 98 | else response.data |
| 99 | ) |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 100 | |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 101 | if response.status != http.client.OK: |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 102 | exceptions.RefreshError(_REFRESH_ERROR, response_body) |
| 103 | |
| 104 | try: |
arithmetic1728 | e115bae | 2020-05-06 16:00:17 -0700 | [diff] [blame] | 105 | token_response = json.loads(response_body) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 106 | token = token_response["accessToken"] |
| 107 | expiry = datetime.strptime(token_response["expireTime"], "%Y-%m-%dT%H:%M:%SZ") |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 108 | |
| 109 | return token, expiry |
| 110 | |
| 111 | except (KeyError, ValueError) as caught_exc: |
| 112 | new_exc = exceptions.RefreshError( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 113 | "{}: No access token or invalid expiration in response.".format( |
| 114 | _REFRESH_ERROR |
| 115 | ), |
| 116 | response_body, |
| 117 | ) |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 118 | raise new_exc from caught_exc |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 119 | |
| 120 | |
Bu Sun Kim | 41599ae | 2020-09-02 12:55:42 -0600 | [diff] [blame] | 121 | class Credentials(credentials.CredentialsWithQuotaProject, credentials.Signing): |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 122 | """This module defines impersonated credentials which are essentially |
| 123 | impersonated identities. |
| 124 | |
| 125 | Impersonated Credentials allows credentials issued to a user or |
| 126 | service account to impersonate another. The target service account must |
| 127 | grant the originating credential principal the |
| 128 | `Service Account Token Creator`_ IAM role: |
| 129 | |
| 130 | For more information about Token Creator IAM role and |
| 131 | IAMCredentials API, see |
| 132 | `Creating Short-Lived Service Account Credentials`_. |
| 133 | |
| 134 | .. _Service Account Token Creator: |
| 135 | https://cloud.google.com/iam/docs/service-accounts#the_service_account_token_creator_role |
| 136 | |
| 137 | .. _Creating Short-Lived Service Account Credentials: |
| 138 | https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials |
| 139 | |
| 140 | Usage: |
| 141 | |
| 142 | First grant source_credentials the `Service Account Token Creator` |
| 143 | role on the target account to impersonate. In this example, the |
| 144 | service account represented by svc_account.json has the |
| 145 | token creator role on |
| 146 | `impersonated-account@_project_.iam.gserviceaccount.com`. |
| 147 | |
salrashid123 | b29f262 | 2018-11-12 09:49:16 -0800 | [diff] [blame] | 148 | Enable the IAMCredentials API on the source project: |
| 149 | `gcloud services enable iamcredentials.googleapis.com`. |
| 150 | |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 151 | Initialize a source credential which does not have access to |
| 152 | list bucket:: |
| 153 | |
Bu Sun Kim | 3319ea8 | 2020-12-07 11:00:03 -0700 | [diff] [blame] | 154 | from google.oauth2 import service_account |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 155 | |
| 156 | target_scopes = [ |
| 157 | 'https://www.googleapis.com/auth/devstorage.read_only'] |
| 158 | |
| 159 | source_credentials = ( |
| 160 | service_account.Credentials.from_service_account_file( |
| 161 | '/path/to/svc_account.json', |
| 162 | scopes=target_scopes)) |
| 163 | |
| 164 | Now use the source credentials to acquire credentials to impersonate |
| 165 | another service account:: |
| 166 | |
| 167 | from google.auth import impersonated_credentials |
| 168 | |
| 169 | target_credentials = impersonated_credentials.Credentials( |
| 170 | source_credentials=source_credentials, |
| 171 | target_principal='impersonated-account@_project_.iam.gserviceaccount.com', |
| 172 | target_scopes = target_scopes, |
| 173 | lifetime=500) |
| 174 | |
| 175 | Resource access is granted:: |
| 176 | |
| 177 | client = storage.Client(credentials=target_credentials) |
| 178 | buckets = client.list_buckets(project='your_project') |
| 179 | for bucket in buckets: |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 180 | print(bucket.name) |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 181 | """ |
| 182 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 183 | def __init__( |
| 184 | self, |
| 185 | source_credentials, |
| 186 | target_principal, |
| 187 | target_scopes, |
| 188 | delegates=None, |
| 189 | lifetime=_DEFAULT_TOKEN_LIFETIME_SECS, |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 190 | quota_project_id=None, |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 191 | iam_endpoint_override=None, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 192 | ): |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 193 | """ |
| 194 | Args: |
| 195 | source_credentials (google.auth.Credentials): The source credential |
| 196 | used as to acquire the impersonated credentials. |
| 197 | target_principal (str): The service account to impersonate. |
| 198 | target_scopes (Sequence[str]): Scopes to request during the |
| 199 | authorization grant. |
| 200 | delegates (Sequence[str]): The chained list of delegates required |
| 201 | to grant the final access_token. If set, the sequence of |
| 202 | identities must have "Service Account Token Creator" capability |
| 203 | granted to the prceeding identity. For example, if set to |
| 204 | [serviceAccountB, serviceAccountC], the source_credential |
| 205 | must have the Token Creator role on serviceAccountB. |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 206 | serviceAccountB must have the Token Creator on |
| 207 | serviceAccountC. |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 208 | Finally, C must have Token Creator on target_principal. |
| 209 | If left unset, source_credential must have that role on |
| 210 | target_principal. |
| 211 | lifetime (int): Number of seconds the delegated credential should |
salrashid123 | b29f262 | 2018-11-12 09:49:16 -0800 | [diff] [blame] | 212 | be valid for (upto 3600). |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 213 | quota_project_id (Optional[str]): The project ID used for quota and billing. |
| 214 | This project may be different from the project used to |
| 215 | create the credentials. |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 216 | iam_endpoint_override (Optiona[str]): The full IAM endpoint override |
| 217 | with the target_principal embedded. This is useful when supporting |
| 218 | impersonation with regional endpoints. |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 219 | """ |
| 220 | |
| 221 | super(Credentials, self).__init__() |
| 222 | |
| 223 | self._source_credentials = copy.copy(source_credentials) |
Bu Sun Kim | 82e224b | 2020-03-13 13:21:18 -0700 | [diff] [blame] | 224 | # Service account source credentials must have the _IAM_SCOPE |
| 225 | # added to refresh correctly. User credentials cannot have |
| 226 | # their original scopes modified. |
| 227 | if isinstance(self._source_credentials, credentials.Scoped): |
| 228 | self._source_credentials = self._source_credentials.with_scopes(_IAM_SCOPE) |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 229 | self._target_principal = target_principal |
| 230 | self._target_scopes = target_scopes |
| 231 | self._delegates = delegates |
| 232 | self._lifetime = lifetime |
| 233 | self.token = None |
| 234 | self.expiry = _helpers.utcnow() |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 235 | self._quota_project_id = quota_project_id |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 236 | self._iam_endpoint_override = iam_endpoint_override |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 237 | |
| 238 | @_helpers.copy_docstring(credentials.Credentials) |
| 239 | def refresh(self, request): |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 240 | self._update_token(request) |
| 241 | |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 242 | def _update_token(self, request): |
| 243 | """Updates credentials with a new access_token representing |
| 244 | the impersonated account. |
| 245 | |
| 246 | Args: |
| 247 | request (google.auth.transport.requests.Request): Request object |
| 248 | to use for refreshing credentials. |
| 249 | """ |
| 250 | |
arithmetic1728 | eb7be3f | 2020-05-28 11:01:24 -0700 | [diff] [blame] | 251 | # Refresh our source credentials if it is not valid. |
| 252 | if not self._source_credentials.valid: |
| 253 | self._source_credentials.refresh(request) |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 254 | |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 255 | body = { |
| 256 | "delegates": self._delegates, |
| 257 | "scope": self._target_scopes, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 258 | "lifetime": str(self._lifetime) + "s", |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 261 | headers = {"Content-Type": "application/json"} |
salrashid123 | 1fbc679 | 2018-11-09 11:05:34 -0800 | [diff] [blame] | 262 | |
| 263 | # Apply the source credentials authentication info. |
| 264 | self._source_credentials.apply(headers) |
| 265 | |
| 266 | self.token, self.expiry = _make_iam_token_request( |
| 267 | request=request, |
| 268 | principal=self._target_principal, |
| 269 | headers=headers, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 270 | body=body, |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 271 | iam_endpoint_override=self._iam_endpoint_override, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 272 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 273 | |
| 274 | def sign_bytes(self, message): |
| 275 | |
| 276 | iam_sign_endpoint = _IAM_SIGN_ENDPOINT.format(self._target_principal) |
| 277 | |
Aniruddha Maru | ca8d98a | 2020-05-15 14:52:50 -0700 | [diff] [blame] | 278 | body = { |
| 279 | "payload": base64.b64encode(message).decode("utf-8"), |
| 280 | "delegates": self._delegates, |
| 281 | } |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 282 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 283 | headers = {"Content-Type": "application/json"} |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 284 | |
| 285 | authed_session = AuthorizedSession(self._source_credentials) |
| 286 | |
| 287 | response = authed_session.post( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 288 | url=iam_sign_endpoint, headers=headers, json=body |
| 289 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 290 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 291 | return base64.b64decode(response.json()["signedBlob"]) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 292 | |
| 293 | @property |
| 294 | def signer_email(self): |
| 295 | return self._target_principal |
| 296 | |
| 297 | @property |
| 298 | def service_account_email(self): |
| 299 | return self._target_principal |
| 300 | |
| 301 | @property |
| 302 | def signer(self): |
| 303 | return self |
| 304 | |
Bu Sun Kim | 41599ae | 2020-09-02 12:55:42 -0600 | [diff] [blame] | 305 | @_helpers.copy_docstring(credentials.CredentialsWithQuotaProject) |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 306 | def with_quota_project(self, quota_project_id): |
| 307 | return self.__class__( |
| 308 | self._source_credentials, |
| 309 | target_principal=self._target_principal, |
| 310 | target_scopes=self._target_scopes, |
| 311 | delegates=self._delegates, |
| 312 | lifetime=self._lifetime, |
| 313 | quota_project_id=quota_project_id, |
bojeil-google | d4d7f38 | 2021-02-16 12:33:20 -0800 | [diff] [blame] | 314 | iam_endpoint_override=self._iam_endpoint_override, |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 315 | ) |
| 316 | |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 317 | |
Bu Sun Kim | 41599ae | 2020-09-02 12:55:42 -0600 | [diff] [blame] | 318 | class IDTokenCredentials(credentials.CredentialsWithQuotaProject): |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 319 | """Open ID Connect ID Token-based service account credentials. |
| 320 | |
| 321 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 322 | |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 323 | def __init__( |
| 324 | self, |
| 325 | target_credentials, |
| 326 | target_audience=None, |
| 327 | include_email=False, |
| 328 | quota_project_id=None, |
| 329 | ): |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 330 | """ |
| 331 | Args: |
| 332 | target_credentials (google.auth.Credentials): The target |
| 333 | credential used as to acquire the id tokens for. |
| 334 | target_audience (string): Audience to issue the token for. |
| 335 | include_email (bool): Include email in IdToken |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 336 | quota_project_id (Optional[str]): The project ID used for |
| 337 | quota and billing. |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 338 | """ |
| 339 | super(IDTokenCredentials, self).__init__() |
| 340 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 341 | if not isinstance(target_credentials, Credentials): |
| 342 | raise exceptions.GoogleAuthError( |
| 343 | "Provided Credential must be " "impersonated_credentials" |
| 344 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 345 | self._target_credentials = target_credentials |
| 346 | self._target_audience = target_audience |
| 347 | self._include_email = include_email |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 348 | self._quota_project_id = quota_project_id |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 349 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 350 | def from_credentials(self, target_credentials, target_audience=None): |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 351 | return self.__class__( |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 352 | target_credentials=self._target_credentials, |
| 353 | target_audience=target_audience, |
Pietro De Nicolao | fd9b5b1 | 2020-12-11 20:00:30 +0100 | [diff] [blame] | 354 | include_email=self._include_email, |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 355 | quota_project_id=self._quota_project_id, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 356 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 357 | |
| 358 | def with_target_audience(self, target_audience): |
| 359 | return self.__class__( |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 360 | target_credentials=self._target_credentials, |
| 361 | target_audience=target_audience, |
Pietro De Nicolao | fd9b5b1 | 2020-12-11 20:00:30 +0100 | [diff] [blame] | 362 | include_email=self._include_email, |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 363 | quota_project_id=self._quota_project_id, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 364 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 365 | |
| 366 | def with_include_email(self, include_email): |
| 367 | return self.__class__( |
| 368 | target_credentials=self._target_credentials, |
| 369 | target_audience=self._target_audience, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 370 | include_email=include_email, |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 371 | quota_project_id=self._quota_project_id, |
| 372 | ) |
| 373 | |
Bu Sun Kim | 41599ae | 2020-09-02 12:55:42 -0600 | [diff] [blame] | 374 | @_helpers.copy_docstring(credentials.CredentialsWithQuotaProject) |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 375 | def with_quota_project(self, quota_project_id): |
| 376 | return self.__class__( |
| 377 | target_credentials=self._target_credentials, |
| 378 | target_audience=self._target_audience, |
| 379 | include_email=self._include_email, |
| 380 | quota_project_id=quota_project_id, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 381 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 382 | |
| 383 | @_helpers.copy_docstring(credentials.Credentials) |
| 384 | def refresh(self, request): |
| 385 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 386 | iam_sign_endpoint = _IAM_IDTOKEN_ENDPOINT.format( |
| 387 | self._target_credentials.signer_email |
| 388 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 389 | |
| 390 | body = { |
| 391 | "audience": self._target_audience, |
| 392 | "delegates": self._target_credentials._delegates, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 393 | "includeEmail": self._include_email, |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 396 | headers = {"Content-Type": "application/json"} |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 397 | |
arithmetic1728 | eb7be3f | 2020-05-28 11:01:24 -0700 | [diff] [blame] | 398 | authed_session = AuthorizedSession( |
| 399 | self._target_credentials._source_credentials, auth_request=request |
| 400 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 401 | |
| 402 | response = authed_session.post( |
| 403 | url=iam_sign_endpoint, |
| 404 | headers=headers, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 405 | data=json.dumps(body).encode("utf-8"), |
| 406 | ) |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 407 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 408 | id_token = response.json()["token"] |
salrashid123 | 7a8641a | 2019-08-07 14:31:33 -0700 | [diff] [blame] | 409 | self.token = id_token |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 410 | self.expiry = datetime.fromtimestamp(jwt.decode(id_token, verify=False)["exp"]) |