Skip to content

Commit e24dd7e

Browse files
committed
[Identity] Enable brokered auth in DAC
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
1 parent 7423f0e commit e24dd7e

File tree

1 file changed

+32
-1
lines changed
  • sdk/identity/azure-identity/azure/identity/_credentials

1 file changed

+32
-1
lines changed

sdk/identity/azure-identity/azure/identity/_credentials/default.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# ------------------------------------
55
import logging
66
import os
7-
from typing import List, Any, Optional, cast
7+
import sys
8+
from typing import List, Any, Optional, cast, TYPE_CHECKING
89

10+
import msal
911
from azure.core.credentials import AccessToken, AccessTokenInfo, TokenRequestOptions, SupportsTokenInfo, TokenCredential
1012
from .._constants import EnvironmentVariables
1113
from .._internal import get_default_authority, normalize_authority, within_dac
@@ -20,6 +22,9 @@
2022
from .vscode import VisualStudioCodeCredential
2123
from .workload_identity import WorkloadIdentityCredential
2224

25+
if TYPE_CHECKING:
26+
from azure.identity.broker import InteractiveBrowserBrokerCredential
27+
2328
_LOGGER = logging.getLogger(__name__)
2429

2530

@@ -42,6 +47,8 @@ class DefaultAzureCredential(ChainedTokenCredential):
4247
5. The identity currently logged in to the Azure CLI.
4348
6. The identity currently logged in to Azure PowerShell.
4449
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.
4552
4653
This default behavior is configurable with keyword arguments.
4754
@@ -192,6 +199,20 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
192199
)
193200
else:
194201
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+
195216
within_dac.set(False)
196217
super(DefaultAzureCredential, self).__init__(*credentials)
197218

@@ -256,3 +277,13 @@ def get_token_info(self, *scopes: str, options: Optional[TokenRequestOptions] =
256277
token_info = cast(SupportsTokenInfo, super()).get_token_info(*scopes, options=options)
257278
within_dac.set(False)
258279
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

Comments
 (0)