Skip to content

Commit bf20396

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

File tree

1 file changed

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

1 file changed

+30
-1
lines changed

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

Lines changed: 30 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

@@ -192,6 +197,20 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
192197
)
193198
else:
194199
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+
195214
within_dac.set(False)
196215
super(DefaultAzureCredential, self).__init__(*credentials)
197216

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

Comments
 (0)