Skip to content

Commit 81df908

Browse files
pvaneckCopilot
andauthored
[Identity Broker] Allow default account usage in WSL (#42005)
* [Identity Broker] Allow default account usage in WSL Signed-off-by: Paul Van Eck <[email protected]> * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> --------- Signed-off-by: Paul Van Eck <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent b926f21 commit 81df908

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

sdk/identity/azure-identity-broker/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This package extends the [Azure Identity][azure_identity] library by providing s
77
| Web Account Manager (WAM) on Windows 10+ | 1.0.0 |
88
| [Company Portal][company_portal] on macOS | 1.3.0b1 |
99
| Web Account Manager (WAM) on WSL 2.4.13+ | 1.3.0b2 |
10-
| Broker on Linux | 1.3.0b2 |
10+
| Microsoft Identity Broker on Linux | 1.3.0b2 |
1111

1212
[Source code][source_code] | [Package (PyPI)][azure_identity_broker] | [API reference documentation][ref_docs] | [Microsoft Entra ID documentation][entra_id]
1313

sdk/identity/azure-identity-broker/azure/identity/broker/_browser.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------
55
import socket
66
import sys
7-
from typing import Dict, Any, Mapping, Union
7+
from typing import Dict, Any, Mapping, Union, cast
88
import msal
99

1010
from azure.core.exceptions import ClientAuthenticationError
@@ -14,7 +14,7 @@
1414
) # pylint:disable=protected-access
1515
from azure.identity._exceptions import CredentialUnavailableError # pylint:disable=protected-access
1616
from azure.identity._internal.utils import within_dac # pylint:disable=protected-access
17-
from ._utils import wrap_exceptions, resolve_tenant
17+
from ._utils import wrap_exceptions, resolve_tenant, is_wsl
1818

1919

2020
class PopTokenRequestOptions(TokenRequestOptions):
@@ -50,6 +50,8 @@ class InteractiveBrowserBrokerCredential(_InteractiveBrowserCredential):
5050
unspecified, users will authenticate to an Azure development application.
5151
:keyword str login_hint: a username suggestion to pre-fill the login page's username/email address field. A user
5252
may still log in with a different username.
53+
:keyword cache_persistence_options: configuration for persistent token caching. If unspecified, the credential
54+
will cache tokens in memory.
5355
:paramtype cache_persistence_options: ~azure.identity.TokenCachePersistenceOptions
5456
:keyword int timeout: seconds to wait for the user to complete authentication. Defaults to 300 (5 minutes).
5557
:keyword int parent_window_handle: If your app is a GUI app running on Windows 10+ or macOS, you
@@ -80,21 +82,21 @@ def __init__(self, **kwargs: Any) -> None:
8082

8183
@wrap_exceptions
8284
def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
83-
scopes = list(scopes) # type: ignore
85+
scopes_list = list(scopes)
8486
claims = kwargs.get("claims")
8587
pop = kwargs.get("pop")
86-
app = self._get_app(**kwargs)
88+
app = cast(msal.PublicClientApplication, self._get_app(**kwargs))
8789
port = self._parsed_url.port if self._parsed_url else None
8890
auth_scheme = None
8991
if pop:
9092
auth_scheme = msal.PopAuthScheme(
9193
http_method=pop["resource_request_method"], url=pop["resource_request_url"], nonce=pop["nonce"]
9294
)
93-
if sys.platform.startswith("win"):
95+
if sys.platform.startswith("win") or is_wsl():
9496
if self._use_default_broker_account:
9597
try:
9698
result = app.acquire_token_interactive(
97-
scopes=scopes,
99+
scopes=scopes_list,
98100
login_hint=self._login_hint,
99101
claims_challenge=claims,
100102
timeout=self._timeout,
@@ -110,7 +112,7 @@ def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
110112
pass
111113
try:
112114
result = app.acquire_token_interactive(
113-
scopes=scopes,
115+
scopes=scopes_list,
114116
login_hint=self._login_hint,
115117
claims_challenge=claims,
116118
timeout=self._timeout,
@@ -133,7 +135,7 @@ def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
133135
else:
134136
try:
135137
result = app.acquire_token_interactive(
136-
scopes=scopes,
138+
scopes=scopes_list,
137139
login_hint=self._login_hint,
138140
claims_challenge=claims,
139141
timeout=self._timeout,
@@ -144,9 +146,9 @@ def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
144146
auth_scheme=auth_scheme,
145147
)
146148
except Exception: # pylint: disable=broad-except
147-
app = self._disable_broker_on_app(**kwargs)
149+
app = cast(msal.PublicClientApplication, self._disable_broker_on_app(**kwargs))
148150
result = app.acquire_token_interactive(
149-
scopes=scopes,
151+
scopes=scopes_list,
150152
login_hint=self._login_hint,
151153
claims_challenge=claims,
152154
timeout=self._timeout,

sdk/identity/azure-identity-broker/azure/identity/broker/_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55
from typing import List, Optional
6-
import os
76
import functools
87
import logging
8+
import os
9+
import platform
910
from azure.core.exceptions import ClientAuthenticationError
1011

1112

@@ -80,3 +81,11 @@ def resolve_tenant(
8081
'when creating the credential, or add "*" to additionally_allowed_tenants to allow '
8182
"acquiring tokens for any tenant.".format(tenant_id)
8283
)
84+
85+
86+
def is_wsl() -> bool:
87+
# This is how MSAL checks for WSL.
88+
uname = platform.uname()
89+
platform_name = getattr(uname, "system", uname[0]).lower()
90+
release = getattr(uname, "release", uname[2]).lower()
91+
return platform_name == "linux" and "microsoft" in release

0 commit comments

Comments
 (0)