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