google-auth 2.59.0__tar.gz → 2.60.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {google_auth-2.59.0/google_auth.egg-info → google_auth-2.60.0}/PKG-INFO +1 -1
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aio/transport/sessions.py +117 -8
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/requests.py +100 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/version.py +1 -1
- {google_auth-2.59.0 → google_auth-2.60.0/google_auth.egg-info}/PKG-INFO +1 -1
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/aio/test_sessions.py +257 -4
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test_requests.py +341 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/LICENSE +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/MANIFEST.in +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/README.rst +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_agent_identity_utils.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_cache.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_cloud_sdk.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_credentials_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_credentials_base.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_default.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_default_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_exponential_backoff.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_helpers.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_jwt_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_oauth2client.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_refresh_worker.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_regional_access_boundary_utils.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/_service_account_info.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aio/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aio/_helpers.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aio/credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aio/transport/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aio/transport/aiohttp.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aio/transport/mtls.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/api_key.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/app_engine.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/aws.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/compute_engine/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/compute_engine/_metadata.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/compute_engine/_mtls.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/compute_engine/credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/_cryptography_rsa.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/_helpers.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/_python_rsa.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/base.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/es.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/es256.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/crypt/rsa.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/downscoped.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/environment_vars.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/exceptions.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/external_account.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/external_account_authorized_user.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/iam.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/identity_pool.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/impersonated_credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/jwt.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/metrics.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/pluggable.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/py.typed +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/_aiohttp_requests.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/_custom_tls_signer.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/_http_client.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/_mtls_helper.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/_requests_base.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/grpc.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/mtls.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/auth/transport/urllib3.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/_client.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/_client_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/_credentials_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/_id_token_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/_reauth_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/_service_account_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/challenges.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/gdch_credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/id_token.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/py.typed +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/reauth.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/service_account.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/sts.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/utils.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/webauthn_handler.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/webauthn_handler_factory.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google/oauth2/webauthn_types.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google_auth.egg-info/SOURCES.txt +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google_auth.egg-info/dependency_links.txt +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google_auth.egg-info/requires.txt +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/google_auth.egg-info/top_level.txt +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/setup.cfg +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/setup.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/aio/test__helpers.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/compute_engine/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/compute_engine/data/smbios_product_name +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/compute_engine/data/smbios_product_name_non_google +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/compute_engine/test__metadata.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/compute_engine/test__mtls.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/compute_engine/test_credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/conftest.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/crypt/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/crypt/test__cryptography_rsa.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/crypt/test__python_rsa.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/crypt/test_crypt.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/crypt/test_es.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/crypt/test_es256.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/crypt/test_rsa.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/authorized_user.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/authorized_user_cloud_sdk.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/authorized_user_cloud_sdk_with_quota_project_id.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/authorized_user_with_rapt_token.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/client_secrets.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/context_aware_metadata.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/enterprise_cert_invalid.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/enterprise_cert_valid.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/enterprise_cert_valid_provider.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es256_privatekey.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es256_public_cert.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es256_publickey.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es256_service_account.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es384_privatekey.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es384_public_cert.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es384_publickey.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/es384_service_account.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/external_account_authorized_user.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/external_account_authorized_user_non_gdu.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/external_subject_token.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/external_subject_token.txt +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/gdch_service_account.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/impersonated_service_account_authorized_user_source.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/impersonated_service_account_external_account_authorized_user_source.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/impersonated_service_account_service_account_source.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/impersonated_service_account_with_quota_project.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/old_oauth_credentials_py3.pickle +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/other_cert.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/pem_from_pkcs12.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/privatekey.p12 +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/privatekey.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/privatekey.pub +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/public_cert.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/service_account.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/service_account_non_gdu.json +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/trust_chain_with_leaf.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/trust_chain_without_leaf.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/data/trust_chain_wrong_order.pem +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test__client.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_challenges.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_gdch_credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_id_token.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_reauth.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_service_account.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_sts.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_utils.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_webauthn_handler.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_webauthn_handler_factory.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/oauth2/test_webauthn_types.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__cache.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__cloud_sdk.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__default.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__exponential_backoff.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__helpers.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__oauth2client.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__refresh_worker.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__regional_access_boundary_utils.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test__service_account_info.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_agent_identity_utils.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_api_key.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_app_engine.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_aws.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_credentials_async.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_downscoped.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_exceptions.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_external_account.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_external_account_authorized_user.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_iam.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_identity_pool.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_impersonated_credentials.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_jwt.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_metrics.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_packaging.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/test_pluggable.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/__init__.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/aio/conftest.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/aio/test_aiohttp.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/aio/test_mtls.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/aio/test_sessions_mtls.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/aioresponses_compat.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/compliance.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test__custom_tls_signer.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test__http_client.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test__mtls_helper.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test_aio_mtls_helper.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test_grpc.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test_mtls.py +0 -0
- {google_auth-2.59.0 → google_auth-2.60.0}/tests/transport/test_urllib3.py +0 -0
|
@@ -24,8 +24,9 @@ import warnings
|
|
|
24
24
|
from contextlib import asynccontextmanager
|
|
25
25
|
from typing import TYPE_CHECKING, Mapping, Optional, Union
|
|
26
26
|
|
|
27
|
+
import google.auth.credentials
|
|
27
28
|
import google.auth.transport._mtls_helper
|
|
28
|
-
from google.auth import _exponential_backoff, exceptions
|
|
29
|
+
from google.auth import _exponential_backoff, _helpers, exceptions
|
|
29
30
|
from google.auth.aio import transport
|
|
30
31
|
from google.auth.aio.credentials import Credentials
|
|
31
32
|
from google.auth.aio.transport import mtls
|
|
@@ -104,6 +105,106 @@ async def timeout_guard(timeout):
|
|
|
104
105
|
_remaining_time()
|
|
105
106
|
|
|
106
107
|
|
|
108
|
+
class _SyncCredentialsAdapter(Credentials):
|
|
109
|
+
"""Adapts synchronous credentials to the asynchronous credentials interface.
|
|
110
|
+
|
|
111
|
+
:class:`AsyncAuthorizedSession` wraps :class:`google.auth.credentials.Credentials`
|
|
112
|
+
(e.g. application default credentials) with this adapter so that they can be
|
|
113
|
+
used with an asynchronous transport. Calls are delegated to the wrapped
|
|
114
|
+
credentials using a synchronous transport, and blocking calls such as
|
|
115
|
+
refreshing the access token run in a worker thread so that the event loop
|
|
116
|
+
is not blocked.
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
credentials (google.auth.credentials.Credentials): The synchronous
|
|
120
|
+
credentials to adapt.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
def __init__(self, credentials: google.auth.credentials.Credentials):
|
|
124
|
+
self._credentials = credentials
|
|
125
|
+
# Synchronous credentials cannot use the asynchronous transport of the
|
|
126
|
+
# session, so they are called with a synchronous transport instead.
|
|
127
|
+
self._sync_request_instance = None
|
|
128
|
+
# Synchronous credentials are not safe to refresh concurrently, which
|
|
129
|
+
# concurrent requests would otherwise do from multiple worker threads.
|
|
130
|
+
# Instead, at most one refresh is in flight and concurrent callers share it.
|
|
131
|
+
self._pending_refresh: Optional["asyncio.Task[None]"] = None
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def _sync_request(self):
|
|
135
|
+
if self._sync_request_instance is None:
|
|
136
|
+
# Imported here because `requests` is an optional dependency of
|
|
137
|
+
# google-auth. It is installed alongside `aiohttp` by the `aiohttp` extra.
|
|
138
|
+
from google.auth.transport import requests as sync_requests
|
|
139
|
+
|
|
140
|
+
self._sync_request_instance = sync_requests.Request()
|
|
141
|
+
return self._sync_request_instance
|
|
142
|
+
|
|
143
|
+
def close(self):
|
|
144
|
+
if (
|
|
145
|
+
self._sync_request_instance is not None
|
|
146
|
+
and hasattr(self._sync_request_instance, "session")
|
|
147
|
+
and self._sync_request_instance.session is not None
|
|
148
|
+
):
|
|
149
|
+
self._sync_request_instance.session.close()
|
|
150
|
+
|
|
151
|
+
@property
|
|
152
|
+
def token(self):
|
|
153
|
+
"""Optional[str]: The bearer token that can be used in HTTP headers to make
|
|
154
|
+
authenticated requests."""
|
|
155
|
+
return self._credentials.token
|
|
156
|
+
|
|
157
|
+
@property
|
|
158
|
+
def expiry(self):
|
|
159
|
+
"""Optional[datetime]: When the token expires and is no longer valid.
|
|
160
|
+
If this is None, the token is assumed to never expire."""
|
|
161
|
+
return self._credentials.expiry
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
@_helpers.copy_docstring(google.auth.credentials.Credentials)
|
|
165
|
+
def valid(self):
|
|
166
|
+
return self._credentials.valid
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
@_helpers.copy_docstring(google.auth.credentials.Credentials)
|
|
170
|
+
def expired(self):
|
|
171
|
+
return self._credentials.expired
|
|
172
|
+
|
|
173
|
+
async def _refresh_shared(self):
|
|
174
|
+
"""Refreshes the wrapped credentials, joining a refresh already in flight.
|
|
175
|
+
|
|
176
|
+
The refresh is shielded from cancellation: a caller that is cancelled
|
|
177
|
+
while waiting (e.g. because of a timeout) stops waiting, but the refresh
|
|
178
|
+
completes so that the next caller joins it rather than starting a
|
|
179
|
+
second, concurrent refresh.
|
|
180
|
+
"""
|
|
181
|
+
if self._pending_refresh is None or self._pending_refresh.done():
|
|
182
|
+
self._pending_refresh = asyncio.create_task(
|
|
183
|
+
asyncio.to_thread(self._credentials.refresh, self._sync_request)
|
|
184
|
+
)
|
|
185
|
+
await asyncio.shield(self._pending_refresh)
|
|
186
|
+
|
|
187
|
+
@_helpers.copy_docstring(Credentials)
|
|
188
|
+
async def apply(self, headers, token=None):
|
|
189
|
+
self._credentials.apply(headers, token=token)
|
|
190
|
+
|
|
191
|
+
@_helpers.copy_docstring(Credentials)
|
|
192
|
+
async def refresh(self, request):
|
|
193
|
+
await self._refresh_shared()
|
|
194
|
+
|
|
195
|
+
@_helpers.copy_docstring(Credentials)
|
|
196
|
+
async def before_request(self, request, method, url, headers):
|
|
197
|
+
if not self._credentials.valid:
|
|
198
|
+
await self._refresh_shared()
|
|
199
|
+
await asyncio.to_thread(
|
|
200
|
+
self._credentials.before_request,
|
|
201
|
+
self._sync_request,
|
|
202
|
+
method,
|
|
203
|
+
url,
|
|
204
|
+
headers,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
|
|
107
208
|
class AsyncAuthorizedSession:
|
|
108
209
|
"""This is an asynchronous implementation of :class:`google.auth.requests.AuthorizedSession` class.
|
|
109
210
|
We utilize an instance of a class that implements :class:`google.auth.aio.transport.Request` configured
|
|
@@ -126,8 +227,9 @@ class AsyncAuthorizedSession:
|
|
|
126
227
|
credentials' headers to the request and refreshing credentials as needed.
|
|
127
228
|
|
|
128
229
|
Args:
|
|
129
|
-
credentials (google.auth.aio.credentials.Credentials):
|
|
130
|
-
The credentials to add to the request.
|
|
230
|
+
credentials (Union[google.auth.aio.credentials.Credentials, google.auth.credentials.Credentials]):
|
|
231
|
+
The credentials to add to the request. Synchronous credentials
|
|
232
|
+
(e.g. application default credentials) are also supported.
|
|
131
233
|
auth_request (Optional[google.auth.aio.transport.Request]):
|
|
132
234
|
An instance of a class that implements
|
|
133
235
|
:class:`~google.auth.aio.transport.Request` used to make requests
|
|
@@ -139,17 +241,22 @@ class AsyncAuthorizedSession:
|
|
|
139
241
|
- google.auth.exceptions.TransportError: If `auth_request` is `None`
|
|
140
242
|
and the external package `aiohttp` is not installed.
|
|
141
243
|
- google.auth.exceptions.InvalidType: If the provided credentials are
|
|
142
|
-
not of type `google.auth.aio.credentials.Credentials
|
|
244
|
+
not of type `google.auth.aio.credentials.Credentials` or
|
|
245
|
+
`google.auth.credentials.Credentials`.
|
|
143
246
|
"""
|
|
144
247
|
|
|
145
248
|
def __init__(
|
|
146
|
-
self,
|
|
249
|
+
self,
|
|
250
|
+
credentials: Union[Credentials, google.auth.credentials.Credentials],
|
|
251
|
+
auth_request: Optional[transport.Request] = None,
|
|
147
252
|
):
|
|
148
|
-
if
|
|
253
|
+
if isinstance(credentials, google.auth.credentials.Credentials):
|
|
254
|
+
credentials = _SyncCredentialsAdapter(credentials)
|
|
255
|
+
elif not isinstance(credentials, Credentials):
|
|
149
256
|
raise exceptions.InvalidType(
|
|
150
|
-
f"The configured credentials of type {type(credentials)} are invalid and must be of type `google.auth.aio.credentials.Credentials`"
|
|
257
|
+
f"The configured credentials of type {type(credentials)} are invalid and must be of type `google.auth.aio.credentials.Credentials` or `google.auth.credentials.Credentials`"
|
|
151
258
|
)
|
|
152
|
-
self._credentials = credentials
|
|
259
|
+
self._credentials: Credentials = credentials
|
|
153
260
|
_auth_request = auth_request
|
|
154
261
|
if not _auth_request and AIOHTTP_INSTALLED:
|
|
155
262
|
_auth_request = AiohttpRequest()
|
|
@@ -846,6 +953,8 @@ class AsyncAuthorizedSession:
|
|
|
846
953
|
if inspect.isawaitable(res):
|
|
847
954
|
await res
|
|
848
955
|
finally:
|
|
956
|
+
if hasattr(self._credentials, "close"):
|
|
957
|
+
self._credentials.close()
|
|
849
958
|
for old_request in self._old_auth_requests:
|
|
850
959
|
try:
|
|
851
960
|
if hasattr(old_request, "close"):
|
|
@@ -16,11 +16,15 @@
|
|
|
16
16
|
|
|
17
17
|
from __future__ import absolute_import
|
|
18
18
|
|
|
19
|
+
import collections
|
|
20
|
+
import copy
|
|
19
21
|
import functools
|
|
20
22
|
import http.client as http_client
|
|
21
23
|
import logging
|
|
22
24
|
import numbers
|
|
25
|
+
import threading
|
|
23
26
|
import time
|
|
27
|
+
import urllib.parse as urllib_parse
|
|
24
28
|
from typing import Optional
|
|
25
29
|
|
|
26
30
|
try:
|
|
@@ -136,11 +140,28 @@ class Request(transport.Request):
|
|
|
136
140
|
.. automethod:: __call__
|
|
137
141
|
"""
|
|
138
142
|
|
|
143
|
+
_mtls_adapter = None
|
|
144
|
+
_mtls_unavailable = False
|
|
145
|
+
|
|
139
146
|
def __init__(self, session: Optional[requests.Session] = None) -> None:
|
|
140
147
|
if not session:
|
|
141
148
|
session = requests.Session()
|
|
142
149
|
|
|
143
150
|
self.session = session
|
|
151
|
+
self._mtls_adapter = None
|
|
152
|
+
self._mtls_unavailable = False
|
|
153
|
+
self._mtls_lock = threading.Lock()
|
|
154
|
+
|
|
155
|
+
def __getstate__(self):
|
|
156
|
+
state = self.__dict__.copy()
|
|
157
|
+
state["_mtls_lock"] = None
|
|
158
|
+
return state
|
|
159
|
+
|
|
160
|
+
def __setstate__(self, state):
|
|
161
|
+
self.__dict__.update(state)
|
|
162
|
+
self._mtls_lock = threading.Lock()
|
|
163
|
+
if not isinstance(self._mtls_adapter, _MutualTlsAdapter):
|
|
164
|
+
self._mtls_adapter = None
|
|
144
165
|
|
|
145
166
|
def __del__(self):
|
|
146
167
|
try:
|
|
@@ -152,6 +173,59 @@ class Request(transport.Request):
|
|
|
152
173
|
# TypeError.
|
|
153
174
|
pass
|
|
154
175
|
|
|
176
|
+
def _configure_mtls_if_needed(self, url):
|
|
177
|
+
"""Lazily mounts a mutual TLS adapter onto the session for mTLS endpoints.
|
|
178
|
+
|
|
179
|
+
Args:
|
|
180
|
+
url (str): The target request URL.
|
|
181
|
+
"""
|
|
182
|
+
if not _mtls_helper.is_mtls_endpoint(url):
|
|
183
|
+
return
|
|
184
|
+
|
|
185
|
+
# Mount on the specific origin rather than globally on "https://" so client
|
|
186
|
+
# certificates are not sent to other hosts sharing this session. The trailing
|
|
187
|
+
# slash prevents requests' startswith() matching from matching lookalike domains.
|
|
188
|
+
parsed = urllib_parse.urlparse(url)
|
|
189
|
+
prefix = f"{parsed.scheme}://{parsed.netloc.lower()}/"
|
|
190
|
+
with self._mtls_lock:
|
|
191
|
+
session_adapter = self.session.get_adapter(url if parsed.path else prefix)
|
|
192
|
+
# Skip if _MutualTlsAdapter is already mounted or the caller mounted a custom adapter.
|
|
193
|
+
if (
|
|
194
|
+
type(session_adapter) is not requests.adapters.HTTPAdapter
|
|
195
|
+
or self.session.get_adapter(prefix) is not session_adapter
|
|
196
|
+
):
|
|
197
|
+
return
|
|
198
|
+
|
|
199
|
+
if self._mtls_adapter is None:
|
|
200
|
+
if self._mtls_unavailable or not _mtls_helper.check_use_client_cert():
|
|
201
|
+
return
|
|
202
|
+
has_cert, cert, key = _mtls_helper.get_client_cert_and_key()
|
|
203
|
+
if not has_cert:
|
|
204
|
+
self._mtls_unavailable = True
|
|
205
|
+
return
|
|
206
|
+
|
|
207
|
+
self._mtls_adapter = _MutualTlsAdapter(
|
|
208
|
+
cert,
|
|
209
|
+
key,
|
|
210
|
+
max_retries=session_adapter.max_retries,
|
|
211
|
+
pool_connections=session_adapter._pool_connections,
|
|
212
|
+
pool_maxsize=session_adapter._pool_maxsize,
|
|
213
|
+
pool_block=session_adapter._pool_block,
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# Replace session.adapters atomically instead of calling session.mount(),
|
|
217
|
+
# which mutates the OrderedDict in place and can raise RuntimeError if
|
|
218
|
+
# another thread is iterating over session.adapters in get_adapter().
|
|
219
|
+
new_adapters = self.session.adapters.copy()
|
|
220
|
+
new_adapters[prefix] = self._mtls_adapter
|
|
221
|
+
self.session.adapters = collections.OrderedDict(
|
|
222
|
+
sorted(
|
|
223
|
+
new_adapters.items(),
|
|
224
|
+
key=lambda item: len(item[0]),
|
|
225
|
+
reverse=True,
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
|
|
155
229
|
def __call__(
|
|
156
230
|
self,
|
|
157
231
|
url,
|
|
@@ -181,6 +255,19 @@ class Request(transport.Request):
|
|
|
181
255
|
Raises:
|
|
182
256
|
google.auth.exceptions.TransportError: If any exception occurred.
|
|
183
257
|
"""
|
|
258
|
+
if not kwargs.get("cert"):
|
|
259
|
+
try:
|
|
260
|
+
self._configure_mtls_if_needed(url)
|
|
261
|
+
except (
|
|
262
|
+
exceptions.MutualTLSChannelError,
|
|
263
|
+
exceptions.ClientCertError,
|
|
264
|
+
OSError,
|
|
265
|
+
) as caught_exc:
|
|
266
|
+
new_exc = exceptions.TransportError(caught_exc)
|
|
267
|
+
raise new_exc from caught_exc
|
|
268
|
+
|
|
269
|
+
# Missing CA bundles or cert paths raised by requests must bubble up
|
|
270
|
+
# unwrapped to fail immediately rather than triggering client retries.
|
|
184
271
|
try:
|
|
185
272
|
_helpers.request_log(_LOGGER, method, url, body, headers)
|
|
186
273
|
response = self.session.request(
|
|
@@ -212,9 +299,11 @@ class _MutualTlsAdapter(requests.adapters.HTTPAdapter):
|
|
|
212
299
|
import certifi
|
|
213
300
|
|
|
214
301
|
ctx_poolmanager = create_urllib3_context()
|
|
302
|
+
ctx_poolmanager.check_hostname = False
|
|
215
303
|
ctx_poolmanager.load_verify_locations(cafile=certifi.where())
|
|
216
304
|
|
|
217
305
|
ctx_proxymanager = create_urllib3_context()
|
|
306
|
+
ctx_proxymanager.check_hostname = False
|
|
218
307
|
ctx_proxymanager.load_verify_locations(cafile=certifi.where())
|
|
219
308
|
|
|
220
309
|
try:
|
|
@@ -251,6 +340,17 @@ class _MutualTlsAdapter(requests.adapters.HTTPAdapter):
|
|
|
251
340
|
|
|
252
341
|
super(_MutualTlsAdapter, self).__init__(**kwargs)
|
|
253
342
|
|
|
343
|
+
def __deepcopy__(self, memo):
|
|
344
|
+
new_adapter = object.__new__(type(self))
|
|
345
|
+
memo[id(self)] = new_adapter
|
|
346
|
+
new_adapter._ctx_poolmanager = self._ctx_poolmanager
|
|
347
|
+
new_adapter._ctx_proxymanager = self._ctx_proxymanager
|
|
348
|
+
new_adapter.__setstate__(copy.deepcopy(self.__getstate__(), memo))
|
|
349
|
+
return new_adapter
|
|
350
|
+
|
|
351
|
+
def __reduce_ex__(self, protocol):
|
|
352
|
+
return (requests.adapters.HTTPAdapter, (), self.__getstate__())
|
|
353
|
+
|
|
254
354
|
def init_poolmanager(self, *args, **kwargs):
|
|
255
355
|
kwargs["ssl_context"] = self._ctx_poolmanager
|
|
256
356
|
super(_MutualTlsAdapter, self).init_poolmanager(*args, **kwargs)
|
|
@@ -13,13 +13,17 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
import asyncio
|
|
16
|
+
import http.client as http_client
|
|
17
|
+
import threading
|
|
16
18
|
from typing import AsyncGenerator
|
|
17
|
-
from unittest.mock import Mock, patch
|
|
19
|
+
from unittest.mock import AsyncMock, Mock, patch
|
|
18
20
|
|
|
19
21
|
import pytest # type: ignore
|
|
20
22
|
from aioresponses import aioresponses # type: ignore
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
import google.auth.credentials
|
|
25
|
+
import google.auth.transport.requests
|
|
26
|
+
from google.auth.aio.credentials import AnonymousCredentials, Credentials
|
|
23
27
|
from google.auth.aio.transport import (
|
|
24
28
|
_DEFAULT_TIMEOUT_SECONDS,
|
|
25
29
|
DEFAULT_MAX_RETRY_ATTEMPTS,
|
|
@@ -28,7 +32,12 @@ from google.auth.aio.transport import (
|
|
|
28
32
|
Response,
|
|
29
33
|
sessions,
|
|
30
34
|
)
|
|
31
|
-
from google.auth.exceptions import
|
|
35
|
+
from google.auth.exceptions import (
|
|
36
|
+
InvalidType,
|
|
37
|
+
RefreshError,
|
|
38
|
+
TimeoutError,
|
|
39
|
+
TransportError,
|
|
40
|
+
)
|
|
32
41
|
|
|
33
42
|
|
|
34
43
|
@pytest.fixture
|
|
@@ -203,9 +212,77 @@ class TestAsyncAuthorizedSession(object):
|
|
|
203
212
|
sessions.AsyncAuthorizedSession(credentials)
|
|
204
213
|
|
|
205
214
|
exc.match(
|
|
206
|
-
f"The configured credentials of type {type(credentials)} are invalid and must be of type `google.auth.aio.credentials.Credentials`"
|
|
215
|
+
f"The configured credentials of type {type(credentials)} are invalid and must be of type `google.auth.aio.credentials.Credentials` or `google.auth.credentials.Credentials`"
|
|
207
216
|
)
|
|
208
217
|
|
|
218
|
+
@pytest.mark.asyncio
|
|
219
|
+
async def test_constructor_with_sync_credentials(self):
|
|
220
|
+
sync_credentials = google.auth.credentials.AnonymousCredentials()
|
|
221
|
+
authed_session = sessions.AsyncAuthorizedSession(
|
|
222
|
+
sync_credentials, auth_request=MockRequest()
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# Synchronous credentials are adapted to the asynchronous credentials interface.
|
|
226
|
+
assert isinstance(authed_session._credentials, Credentials)
|
|
227
|
+
assert isinstance(authed_session._credentials, sessions._SyncCredentialsAdapter)
|
|
228
|
+
assert authed_session._credentials._credentials is sync_credentials
|
|
229
|
+
with patch.object(
|
|
230
|
+
authed_session._credentials,
|
|
231
|
+
"close",
|
|
232
|
+
wraps=authed_session._credentials.close,
|
|
233
|
+
) as mock_close:
|
|
234
|
+
await authed_session.close()
|
|
235
|
+
mock_close.assert_called_once()
|
|
236
|
+
|
|
237
|
+
@pytest.mark.asyncio
|
|
238
|
+
async def test_request_with_sync_credentials_success(self, mocked_content):
|
|
239
|
+
sync_credentials = Mock(spec=google.auth.credentials.Credentials)
|
|
240
|
+
mocked_response = MockResponse(
|
|
241
|
+
status_code=http_client.OK,
|
|
242
|
+
headers={"Content-Type": "application/json"},
|
|
243
|
+
content=mocked_content,
|
|
244
|
+
)
|
|
245
|
+
auth_request = MockRequest(mocked_response)
|
|
246
|
+
authed_session = sessions.AsyncAuthorizedSession(sync_credentials, auth_request)
|
|
247
|
+
|
|
248
|
+
response = await authed_session.request(
|
|
249
|
+
"GET", self.TEST_URL, headers={"x-test": "value"}
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
assert response.status_code == http_client.OK
|
|
253
|
+
assert await response.read() == b"Cavefish have no sight."
|
|
254
|
+
# The synchronous credentials are invoked with a synchronous transport rather
|
|
255
|
+
# than the asynchronous transport of the session.
|
|
256
|
+
sync_credentials.before_request.assert_called_once()
|
|
257
|
+
request, method, url, headers = sync_credentials.before_request.call_args.args
|
|
258
|
+
assert isinstance(request, google.auth.transport.requests.Request)
|
|
259
|
+
assert method == "GET"
|
|
260
|
+
assert url == self.TEST_URL
|
|
261
|
+
assert headers == {"x-test": "value"}
|
|
262
|
+
await authed_session.close()
|
|
263
|
+
|
|
264
|
+
@pytest.mark.asyncio
|
|
265
|
+
async def test_request_with_sync_credentials_refreshes_on_unauthorized(self):
|
|
266
|
+
sync_credentials = Mock(spec=google.auth.credentials.Credentials)
|
|
267
|
+
unauthorized_response = MockResponse(status_code=http_client.UNAUTHORIZED)
|
|
268
|
+
ok_response = MockResponse(status_code=http_client.OK)
|
|
269
|
+
auth_request = AsyncMock(side_effect=[unauthorized_response, ok_response])
|
|
270
|
+
authed_session = sessions.AsyncAuthorizedSession(
|
|
271
|
+
sync_credentials, auth_request=auth_request
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
response = await authed_session.request("GET", self.TEST_URL)
|
|
275
|
+
|
|
276
|
+
assert response is ok_response
|
|
277
|
+
assert auth_request.call_count == 2
|
|
278
|
+
assert unauthorized_response._close
|
|
279
|
+
sync_credentials.refresh.assert_called_once()
|
|
280
|
+
(refresh_request,) = sync_credentials.refresh.call_args.args
|
|
281
|
+
assert isinstance(refresh_request, google.auth.transport.requests.Request)
|
|
282
|
+
# The same synchronous transport is reused for every call to the credentials.
|
|
283
|
+
assert refresh_request is sync_credentials.before_request.call_args.args[0]
|
|
284
|
+
await authed_session.close()
|
|
285
|
+
|
|
209
286
|
@pytest.mark.asyncio
|
|
210
287
|
async def test_request_default_auth_request_success(self):
|
|
211
288
|
with aioresponses() as m:
|
|
@@ -368,3 +445,179 @@ def test_mock_request_clone():
|
|
|
368
445
|
request = MockRequest()
|
|
369
446
|
cloned = request._clone()
|
|
370
447
|
assert cloned is request
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
class BlockingRefreshCredentials(google.auth.credentials.Credentials):
|
|
451
|
+
"""Synchronous credentials whose refresh blocks until released by the test."""
|
|
452
|
+
|
|
453
|
+
def __init__(self):
|
|
454
|
+
super().__init__()
|
|
455
|
+
self.refresh_calls = 0
|
|
456
|
+
self.in_flight_refreshes = 0
|
|
457
|
+
self.max_in_flight_refreshes = 0
|
|
458
|
+
self.refresh_started = threading.Event()
|
|
459
|
+
self.release_refresh = threading.Event()
|
|
460
|
+
|
|
461
|
+
def refresh(self, request):
|
|
462
|
+
self.refresh_calls += 1
|
|
463
|
+
self.in_flight_refreshes += 1
|
|
464
|
+
self.max_in_flight_refreshes = max(
|
|
465
|
+
self.max_in_flight_refreshes, self.in_flight_refreshes
|
|
466
|
+
)
|
|
467
|
+
self.refresh_started.set()
|
|
468
|
+
self.release_refresh.wait(timeout=5)
|
|
469
|
+
self.in_flight_refreshes -= 1
|
|
470
|
+
self.token = "token"
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
class TestSyncCredentialsAdapter(object):
|
|
474
|
+
TEST_URL = "http://example.com/"
|
|
475
|
+
|
|
476
|
+
@pytest.mark.asyncio
|
|
477
|
+
async def test_delegates_to_sync_credentials(self):
|
|
478
|
+
sync_credentials = Mock(spec=google.auth.credentials.Credentials)
|
|
479
|
+
sync_credentials.token = "sync-token"
|
|
480
|
+
sync_credentials.expiry = Mock()
|
|
481
|
+
sync_credentials.valid = True
|
|
482
|
+
sync_credentials.expired = False
|
|
483
|
+
adapter = sessions._SyncCredentialsAdapter(sync_credentials)
|
|
484
|
+
headers = {}
|
|
485
|
+
|
|
486
|
+
await adapter.before_request(Mock(), "GET", self.TEST_URL, headers)
|
|
487
|
+
await adapter.refresh(Mock())
|
|
488
|
+
await adapter.apply(headers, token="token")
|
|
489
|
+
|
|
490
|
+
assert adapter.token == "sync-token"
|
|
491
|
+
assert adapter.expiry is sync_credentials.expiry
|
|
492
|
+
assert adapter.valid is True
|
|
493
|
+
assert adapter.expired is False
|
|
494
|
+
|
|
495
|
+
# The same synchronous transport is used for every call.
|
|
496
|
+
sync_request = sync_credentials.before_request.call_args.args[0]
|
|
497
|
+
assert isinstance(sync_request, google.auth.transport.requests.Request)
|
|
498
|
+
sync_credentials.before_request.assert_called_once_with(
|
|
499
|
+
sync_request, "GET", self.TEST_URL, headers
|
|
500
|
+
)
|
|
501
|
+
sync_credentials.refresh.assert_called_once_with(sync_request)
|
|
502
|
+
sync_credentials.apply.assert_called_once_with(headers, token="token")
|
|
503
|
+
with patch.object(sync_request.session, "close") as mock_close:
|
|
504
|
+
adapter.close()
|
|
505
|
+
mock_close.assert_called_once()
|
|
506
|
+
|
|
507
|
+
@pytest.mark.asyncio
|
|
508
|
+
async def test_blocking_calls_run_off_the_event_loop_thread(self):
|
|
509
|
+
sync_credentials = Mock(spec=google.auth.credentials.Credentials)
|
|
510
|
+
thread_ids = []
|
|
511
|
+
sync_credentials.before_request.side_effect = lambda *args: thread_ids.append(
|
|
512
|
+
threading.get_ident()
|
|
513
|
+
)
|
|
514
|
+
sync_credentials.refresh.side_effect = lambda *args: thread_ids.append(
|
|
515
|
+
threading.get_ident()
|
|
516
|
+
)
|
|
517
|
+
adapter = sessions._SyncCredentialsAdapter(sync_credentials)
|
|
518
|
+
|
|
519
|
+
await adapter.before_request(Mock(), "GET", self.TEST_URL, {})
|
|
520
|
+
await adapter.refresh(Mock())
|
|
521
|
+
|
|
522
|
+
assert len(thread_ids) == 2
|
|
523
|
+
assert threading.get_ident() not in thread_ids
|
|
524
|
+
|
|
525
|
+
@pytest.mark.asyncio
|
|
526
|
+
async def test_concurrent_before_request_refreshes_once(self):
|
|
527
|
+
sync_credentials = BlockingRefreshCredentials()
|
|
528
|
+
adapter = sessions._SyncCredentialsAdapter(sync_credentials)
|
|
529
|
+
headers = [{} for _ in range(5)]
|
|
530
|
+
|
|
531
|
+
tasks = [
|
|
532
|
+
asyncio.create_task(adapter.before_request(Mock(), "GET", self.TEST_URL, h))
|
|
533
|
+
for h in headers
|
|
534
|
+
]
|
|
535
|
+
await asyncio.to_thread(sync_credentials.refresh_started.wait, 5)
|
|
536
|
+
# Yield to the event loop so all tasks run until they await the refresh.
|
|
537
|
+
await asyncio.sleep(0)
|
|
538
|
+
sync_credentials.release_refresh.set()
|
|
539
|
+
await asyncio.gather(*tasks)
|
|
540
|
+
|
|
541
|
+
assert sync_credentials.refresh_calls == 1
|
|
542
|
+
assert all(h["authorization"] == "Bearer token" for h in headers)
|
|
543
|
+
|
|
544
|
+
@pytest.mark.asyncio
|
|
545
|
+
async def test_refresh_and_before_request_share_one_refresh(self):
|
|
546
|
+
sync_credentials = BlockingRefreshCredentials()
|
|
547
|
+
adapter = sessions._SyncCredentialsAdapter(sync_credentials)
|
|
548
|
+
headers = {}
|
|
549
|
+
|
|
550
|
+
refresh_task = asyncio.create_task(adapter.refresh(Mock()))
|
|
551
|
+
await asyncio.to_thread(sync_credentials.refresh_started.wait, 5)
|
|
552
|
+
before_request_task = asyncio.create_task(
|
|
553
|
+
adapter.before_request(Mock(), "GET", self.TEST_URL, headers)
|
|
554
|
+
)
|
|
555
|
+
# Yield to the event loop so before_request_task reaches the refresh.
|
|
556
|
+
await asyncio.sleep(0)
|
|
557
|
+
sync_credentials.release_refresh.set()
|
|
558
|
+
await asyncio.gather(refresh_task, before_request_task)
|
|
559
|
+
|
|
560
|
+
assert sync_credentials.max_in_flight_refreshes == 1
|
|
561
|
+
assert sync_credentials.refresh_calls == 1
|
|
562
|
+
assert headers["authorization"] == "Bearer token"
|
|
563
|
+
|
|
564
|
+
@pytest.mark.asyncio
|
|
565
|
+
async def test_cancelled_caller_does_not_abandon_refresh(self):
|
|
566
|
+
sync_credentials = BlockingRefreshCredentials()
|
|
567
|
+
adapter = sessions._SyncCredentialsAdapter(sync_credentials)
|
|
568
|
+
|
|
569
|
+
cancelled_task = asyncio.create_task(
|
|
570
|
+
adapter.before_request(Mock(), "GET", self.TEST_URL, {})
|
|
571
|
+
)
|
|
572
|
+
await asyncio.to_thread(sync_credentials.refresh_started.wait, 5)
|
|
573
|
+
cancelled_task.cancel()
|
|
574
|
+
with pytest.raises(asyncio.CancelledError):
|
|
575
|
+
await cancelled_task
|
|
576
|
+
|
|
577
|
+
# The refresh started on behalf of the cancelled request is still running:
|
|
578
|
+
# a new request must wait for it rather than start a second refresh.
|
|
579
|
+
headers = {}
|
|
580
|
+
waiting_task = asyncio.create_task(
|
|
581
|
+
adapter.before_request(Mock(), "GET", self.TEST_URL, headers)
|
|
582
|
+
)
|
|
583
|
+
await asyncio.sleep(0)
|
|
584
|
+
assert not waiting_task.done()
|
|
585
|
+
sync_credentials.release_refresh.set()
|
|
586
|
+
await waiting_task
|
|
587
|
+
|
|
588
|
+
assert sync_credentials.refresh_calls == 1
|
|
589
|
+
assert sync_credentials.max_in_flight_refreshes == 1
|
|
590
|
+
assert headers["authorization"] == "Bearer token"
|
|
591
|
+
|
|
592
|
+
@pytest.mark.asyncio
|
|
593
|
+
async def test_failed_refresh_is_not_reused(self):
|
|
594
|
+
sync_credentials = Mock(spec=google.auth.credentials.Credentials)
|
|
595
|
+
sync_credentials.refresh.side_effect = [RefreshError("refresh failed"), None]
|
|
596
|
+
adapter = sessions._SyncCredentialsAdapter(sync_credentials)
|
|
597
|
+
|
|
598
|
+
with pytest.raises(RefreshError):
|
|
599
|
+
await adapter.refresh(Mock())
|
|
600
|
+
await adapter.refresh(Mock())
|
|
601
|
+
|
|
602
|
+
assert sync_credentials.refresh.call_count == 2
|
|
603
|
+
|
|
604
|
+
@pytest.mark.asyncio
|
|
605
|
+
async def test_before_request_with_valid_credentials_does_not_wait_for_refresh(
|
|
606
|
+
self,
|
|
607
|
+
):
|
|
608
|
+
sync_credentials = BlockingRefreshCredentials()
|
|
609
|
+
sync_credentials.token = "token"
|
|
610
|
+
adapter = sessions._SyncCredentialsAdapter(sync_credentials)
|
|
611
|
+
headers = {}
|
|
612
|
+
|
|
613
|
+
refresh_task = asyncio.create_task(adapter.refresh(Mock()))
|
|
614
|
+
await asyncio.to_thread(sync_credentials.refresh_started.wait, 5)
|
|
615
|
+
# Requests that already have a valid token must not wait for the refresh.
|
|
616
|
+
await asyncio.wait_for(
|
|
617
|
+
adapter.before_request(Mock(), "GET", self.TEST_URL, headers), timeout=5
|
|
618
|
+
)
|
|
619
|
+
assert headers["authorization"] == "Bearer token"
|
|
620
|
+
|
|
621
|
+
sync_credentials.release_refresh.set()
|
|
622
|
+
await refresh_task
|
|
623
|
+
assert sync_credentials.refresh_calls == 1
|