22# Copyright (c) Microsoft Corporation.
33# Licensed under the MIT License.
44# ------------------------------------
5+ import logging
56import os
67
78from azure .core .exceptions import ClientAuthenticationError
1213from .managed_identity import ManagedIdentityCredential
1314from .user import SharedTokenCacheCredential
1415
16+ _LOGGER = logging .getLogger (__name__ )
17+
1518
1619class DefaultAzureCredential (ChainedTokenCredential ):
1720 """A default credential capable of handling most Azure SDK authentication scenarios.
@@ -37,11 +40,14 @@ def __init__(self, **kwargs):
3740
3841 # SharedTokenCacheCredential is part of the default only on supported platforms.
3942 if SharedTokenCacheCredential .supported ():
40- credentials .append (
41- SharedTokenCacheCredential (
42- username = os .environ .get (EnvironmentVariables .AZURE_USERNAME ), authority = authority , ** kwargs
43- )
44- )
43+ try :
44+ # username is only required to disambiguate, when the cache contains tokens for multiple identities
45+ username = os .environ .get (EnvironmentVariables .AZURE_USERNAME )
46+ shared_cache = SharedTokenCacheCredential (username = username , authority = authority , ** kwargs )
47+ credentials .append (shared_cache )
48+ except Exception as ex : # pylint:disable=broad-except
49+ # transitive dependency pywin32 doesn't support 3.8 (https://github.com/mhammond/pywin32/issues/1431)
50+ _LOGGER .info ("Shared token cache is unavailable: '%s'" , ex )
4551
4652 super (DefaultAzureCredential , self ).__init__ (* credentials )
4753
0 commit comments