4
4
# ------------------------------------
5
5
import logging
6
6
import os
7
- from typing import List , Any , Optional , cast
7
+ import sys
8
+ from typing import List , Any , Optional , cast , TYPE_CHECKING
8
9
10
+ import msal
9
11
from azure .core .credentials import AccessToken , AccessTokenInfo , TokenRequestOptions , SupportsTokenInfo , TokenCredential
10
12
from .._constants import EnvironmentVariables
11
13
from .._internal import get_default_authority , normalize_authority , within_dac
20
22
from .vscode import VisualStudioCodeCredential
21
23
from .workload_identity import WorkloadIdentityCredential
22
24
25
+ if TYPE_CHECKING :
26
+ from azure .identity .broker import InteractiveBrowserBrokerCredential
27
+
23
28
_LOGGER = logging .getLogger (__name__ )
24
29
25
30
@@ -42,6 +47,8 @@ class DefaultAzureCredential(ChainedTokenCredential):
42
47
5. The identity currently logged in to the Azure CLI.
43
48
6. The identity currently logged in to Azure PowerShell.
44
49
7. The identity currently logged in to the Azure Developer CLI.
50
+ 8. On Windows only: The currently logged in Windows account. This requires the `azure-identity-broker` package to
51
+ be installed.
45
52
46
53
This default behavior is configurable with keyword arguments.
47
54
@@ -192,6 +199,20 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
192
199
)
193
200
else :
194
201
credentials .append (InteractiveBrowserCredential (tenant_id = interactive_browser_tenant_id , ** kwargs ))
202
+ broker_credential_class = _get_broker_credential ()
203
+ if broker_credential_class and sys .platform .startswith ("win" ):
204
+ # The silent auth flow for brokered auth is only available on Windows.
205
+ broker_credential_args = {
206
+ "tenant_id" : interactive_browser_tenant_id ,
207
+ "parent_window_handle" : msal .PublicClientApplication .CONSOLE_WINDOW_HANDLE ,
208
+ "use_default_broker_account" : True ,
209
+ ** kwargs ,
210
+ }
211
+ if interactive_browser_client_id :
212
+ broker_credential_args ["client_id" ] = interactive_browser_client_id
213
+
214
+ credentials .append (broker_credential_class (** broker_credential_args ))
215
+
195
216
within_dac .set (False )
196
217
super (DefaultAzureCredential , self ).__init__ (* credentials )
197
218
@@ -256,3 +277,13 @@ def get_token_info(self, *scopes: str, options: Optional[TokenRequestOptions] =
256
277
token_info = cast (SupportsTokenInfo , super ()).get_token_info (* scopes , options = options )
257
278
within_dac .set (False )
258
279
return token_info
280
+
281
+
282
+ def _get_broker_credential () -> Optional ["InteractiveBrowserBrokerCredential" ]:
283
+ # Get the broker credential if available
284
+ try :
285
+ from azure .identity .broker import InteractiveBrowserBrokerCredential
286
+
287
+ return InteractiveBrowserBrokerCredential
288
+ except ImportError :
289
+ return None
0 commit comments