|
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 |
|
@@ -192,6 +197,20 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
|
192 | 197 | )
|
193 | 198 | else:
|
194 | 199 | credentials.append(InteractiveBrowserCredential(tenant_id=interactive_browser_tenant_id, **kwargs))
|
| 200 | + broker_credential_class = _get_broker_credential() |
| 201 | + if broker_credential_class and sys.platform.startswith("win"): |
| 202 | + # The silent auth flow for brokered auth is only available on Windows. |
| 203 | + broker_credential_args = { |
| 204 | + "tenant_id": interactive_browser_tenant_id, |
| 205 | + "parent_window_handle": msal.PublicClientApplication.CONSOLE_WINDOW_HANDLE, |
| 206 | + "use_default_broker_account": True, |
| 207 | + **kwargs, |
| 208 | + } |
| 209 | + if interactive_browser_client_id: |
| 210 | + broker_credential_args["client_id"] = interactive_browser_client_id |
| 211 | + |
| 212 | + credentials.append(broker_credential_class(**broker_credential_args)) |
| 213 | + |
195 | 214 | within_dac.set(False)
|
196 | 215 | super(DefaultAzureCredential, self).__init__(*credentials)
|
197 | 216 |
|
@@ -256,3 +275,13 @@ def get_token_info(self, *scopes: str, options: Optional[TokenRequestOptions] =
|
256 | 275 | token_info = cast(SupportsTokenInfo, super()).get_token_info(*scopes, options=options)
|
257 | 276 | within_dac.set(False)
|
258 | 277 | return token_info
|
| 278 | + |
| 279 | + |
| 280 | +def _get_broker_credential() -> Optional["InteractiveBrowserBrokerCredential"]: |
| 281 | + # Get the broker credential if available |
| 282 | + try: |
| 283 | + from azure.identity.broker import InteractiveBrowserBrokerCredential |
| 284 | + |
| 285 | + return InteractiveBrowserBrokerCredential |
| 286 | + except ImportError: |
| 287 | + return None |
0 commit comments