|
44 | 44 | import google.api_core.client_options
|
45 | 45 | from google.auth.transport import mtls
|
46 | 46 | from google.auth.exceptions import MutualTLSChannelError
|
| 47 | +from google.oauth2 import service_account |
47 | 48 |
|
48 | 49 | try:
|
49 | 50 | import google_auth_httplib2
|
@@ -188,6 +189,7 @@ def build(
|
188 | 189 | adc_key_path=None,
|
189 | 190 | num_retries=1,
|
190 | 191 | static_discovery=None,
|
| 192 | + always_use_jwt_access=True, |
191 | 193 | ):
|
192 | 194 | """Construct a Resource for interacting with an API.
|
193 | 195 |
|
@@ -246,6 +248,9 @@ def build(
|
246 | 248 | on the value of `discoveryServiceUrl`. `static_discovery` will default to
|
247 | 249 | `True` when `discoveryServiceUrl` is also not provided, otherwise it will
|
248 | 250 | default to `False`.
|
| 251 | + always_use_jwt_access: Boolean, whether always use self signed JWT for service |
| 252 | + account credentials. This only applies to |
| 253 | + google.oauth2.service_account.Credentials. |
249 | 254 |
|
250 | 255 | Returns:
|
251 | 256 | A Resource object with methods for interacting with the service.
|
@@ -301,6 +306,7 @@ def build(
|
301 | 306 | client_options=client_options,
|
302 | 307 | adc_cert_path=adc_cert_path,
|
303 | 308 | adc_key_path=adc_key_path,
|
| 309 | + always_use_jwt_access=always_use_jwt_access, |
304 | 310 | )
|
305 | 311 | break # exit if a service was created
|
306 | 312 | except HttpError as e:
|
@@ -441,6 +447,7 @@ def build_from_document(
|
441 | 447 | client_options=None,
|
442 | 448 | adc_cert_path=None,
|
443 | 449 | adc_key_path=None,
|
| 450 | + always_use_jwt_access=True, |
444 | 451 | ):
|
445 | 452 | """Create a Resource for interacting with an API.
|
446 | 453 |
|
@@ -490,6 +497,9 @@ def build_from_document(
|
490 | 497 | `true` in order to use this field, otherwise this field doesn't nothing.
|
491 | 498 | More details on the environment variables are here:
|
492 | 499 | https://google.aip.dev/auth/4114
|
| 500 | + always_use_jwt_access: Boolean, whether always use self signed JWT for service |
| 501 | + account credentials. This only applies to |
| 502 | + google.oauth2.service_account.Credentials. |
493 | 503 |
|
494 | 504 | Returns:
|
495 | 505 | A Resource object with methods for interacting with the service.
|
@@ -530,6 +540,7 @@ def build_from_document(
|
530 | 540 |
|
531 | 541 | # If an API Endpoint is provided on client options, use that as the base URL
|
532 | 542 | base = urllib.parse.urljoin(service["rootUrl"], service["servicePath"])
|
| 543 | + audience_for_self_signed_jwt = base |
533 | 544 | if client_options.api_endpoint:
|
534 | 545 | base = client_options.api_endpoint
|
535 | 546 |
|
@@ -572,6 +583,17 @@ def build_from_document(
|
572 | 583 | if not client_options.scopes:
|
573 | 584 | credentials = _auth.with_scopes(credentials, scopes)
|
574 | 585 |
|
| 586 | + # For google-auth service account credentials, enable self signed JWT if |
| 587 | + # always_use_jwt_access is true. |
| 588 | + if ( |
| 589 | + credentials |
| 590 | + and isinstance(credentials, service_account.Credentials) |
| 591 | + and always_use_jwt_access |
| 592 | + and hasattr(service_account.Credentials, "with_always_use_jwt_access") |
| 593 | + ): |
| 594 | + credentials = credentials.with_always_use_jwt_access(always_use_jwt_access) |
| 595 | + credentials._create_self_signed_jwt(audience_for_self_signed_jwt) |
| 596 | + |
575 | 597 | # If credentials are provided, create an authorized http instance;
|
576 | 598 | # otherwise, skip authentication.
|
577 | 599 | if credentials:
|
|
0 commit comments