diff --git a/sdk/identity/azure-identity/azure/identity/_credentials/default.py b/sdk/identity/azure-identity/azure/identity/_credentials/default.py index d5d4298b81cf..7ac2676f4df1 100644 --- a/sdk/identity/azure-identity/azure/identity/_credentials/default.py +++ b/sdk/identity/azure-identity/azure/identity/_credentials/default.py @@ -14,6 +14,15 @@ from .managed_identity import ManagedIdentityCredential from .shared_cache import SharedTokenCacheCredential +try: + from typing import TYPE_CHECKING +except ImportError: + TYPE_CHECKING = False + +if TYPE_CHECKING: + from typing import Any + from azure.core.credentials import AccessToken + _LOGGER = logging.getLogger(__name__) @@ -83,12 +92,21 @@ def __init__(self, **kwargs): super(DefaultAzureCredential, self).__init__(*credentials) def get_token(self, *scopes, **kwargs): + # type: (*str, **Any) -> AccessToken + """Request an access token for `scopes`. + + .. note:: This method is called by Azure SDK clients. It isn't intended for use in application code. + + :param str scopes: desired scopes for the token + :raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The exception has a + `message` attribute listing each authentication attempt and its error message. + """ try: return super(DefaultAzureCredential, self).get_token(*scopes, **kwargs) except ClientAuthenticationError as e: raise ClientAuthenticationError( message=""" -{}\n\nPlease visit the Azure identity Python SDK docs at +{}\n\nPlease visit the documentation at https://aka.ms/python-sdk-identity#defaultazurecredential to learn what options DefaultAzureCredential supports""".format( e.message diff --git a/sdk/identity/azure-identity/azure/identity/aio/_credentials/default.py b/sdk/identity/azure-identity/azure/identity/aio/_credentials/default.py index 8a543ba2f72f..bcf81c4cc338 100644 --- a/sdk/identity/azure-identity/azure/identity/aio/_credentials/default.py +++ b/sdk/identity/azure-identity/azure/identity/aio/_credentials/default.py @@ -4,6 +4,9 @@ # ------------------------------------ import logging import os +from typing import TYPE_CHECKING + +from azure.core.exceptions import ClientAuthenticationError from ..._constants import EnvironmentVariables, KnownAuthorities from .chained import ChainedTokenCredential @@ -11,6 +14,9 @@ from .managed_identity import ManagedIdentityCredential from .shared_cache import SharedTokenCacheCredential +if TYPE_CHECKING: + from typing import Any + _LOGGER = logging.getLogger(__name__) @@ -73,3 +79,24 @@ def __init__(self, **kwargs): _LOGGER.info("Shared token cache is unavailable: '%s'", ex) super().__init__(*credentials) + + async def get_token(self, *scopes: str, **kwargs: "Any"): + """Asynchronously request an access token for `scopes`. + + .. note:: This method is called by Azure SDK clients. It isn't intended for use in application code. + + :param str scopes: desired scopes for the token + :raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The exception has a + `message` attribute listing each authentication attempt and its error message. + """ + try: + return await super(DefaultAzureCredential, self).get_token(*scopes, **kwargs) + except ClientAuthenticationError as e: + raise ClientAuthenticationError( + message=""" +{}\n\nPlease visit the documentation at +https://aka.ms/python-sdk-identity#defaultazurecredential +to learn what options DefaultAzureCredential supports""".format( + e.message + ) + )