Skip to content

Commit 4eb7bd1

Browse files
DharshanBJrayluo
andauthored
Enable broker support on Linux for WSL (#766)
* Enable broker support on Linux * update version number * Update sample/interactive_sample.py Co-authored-by: Ray Luo <[email protected]> * Update msal/application.py Co-authored-by: Ray Luo <[email protected]> * Update tests/broker-test.py Co-authored-by: Ray Luo <[email protected]> * revert back release version bump * address comments * address comment * update approximate version hint * update * Update msal/application.py Co-authored-by: Ray Luo <[email protected]> * Address comments * Update * Add enable_broker_on_wsl flag * Address comments * Update msal/__main__.py Co-authored-by: Ray Luo <[email protected]> * Update tests/test_e2e.py Co-authored-by: Ray Luo <[email protected]> * Update msal/application.py Co-authored-by: Ray Luo <[email protected]> * Update msal/application.py Co-authored-by: Ray Luo <[email protected]> * Bump up msal py version to 1.33 * Update msal/application.py Co-authored-by: Ray Luo <[email protected]> --------- Co-authored-by: Ray Luo <[email protected]>
1 parent bcc54a9 commit 4eb7bd1

File tree

7 files changed

+53
-13
lines changed

7 files changed

+53
-13
lines changed

msal/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ def _main():
300300
instance_discovery=instance_discovery,
301301
enable_broker_on_windows=enable_broker,
302302
enable_broker_on_mac=enable_broker,
303+
enable_broker_on_linux=enable_broker,
304+
enable_broker_on_wsl=enable_broker,
303305
enable_pii_log=enable_pii_log,
304306
token_cache=global_cache,
305307
) if not is_cca else msal.ConfidentialClientApplication(

msal/application.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .throttled_http_client import ThrottledHttpClient
2222
from .cloudshell import _is_running_in_cloud_shell
2323
from .sku import SKU, __version__
24-
24+
from .oauth2cli.authcode import is_wsl
2525

2626

2727
logger = logging.getLogger(__name__)
@@ -164,6 +164,8 @@ def _preferred_browser():
164164
pass # We may still proceed
165165
return None
166166

167+
def _is_ssh_cert_or_pop_request(token_type, auth_scheme) -> bool:
168+
return token_type == "ssh-cert" or token_type == "pop" or isinstance(auth_scheme, msal.auth_scheme.PopAuthScheme)
167169

168170
class _ClientWithCcsRoutingInfo(Client):
169171

@@ -710,7 +712,7 @@ def _decide_broker(self, allow_broker, enable_pii_log):
710712

711713
def is_pop_supported(self):
712714
"""Returns True if this client supports Proof-of-Possession Access Token."""
713-
return self._enable_broker
715+
return self._enable_broker and sys.platform in ("win32", "darwin")
714716

715717
def _decorate_scope(
716718
self, scopes,
@@ -1582,10 +1584,12 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
15821584
raise ValueError("auth_scheme is not supported in Cloud Shell")
15831585
return self._acquire_token_by_cloud_shell(scopes, data=data)
15841586

1587+
is_ssh_cert_or_pop_request = _is_ssh_cert_or_pop_request(data.get("token_type"), auth_scheme)
1588+
15851589
if self._enable_broker and account and account.get("account_source") in (
15861590
_GRANT_TYPE_BROKER, # Broker successfully established this account previously.
15871591
None, # Unknown data from older MSAL. Broker might still work.
1588-
):
1592+
) and (sys.platform in ("win32", "darwin") or not is_ssh_cert_or_pop_request):
15891593
from .broker import _acquire_token_silently
15901594
response = _acquire_token_silently(
15911595
"https://{}/{}".format(self.authority.instance, self.authority.tenant),
@@ -1832,7 +1836,7 @@ def acquire_token_by_username_password(
18321836
"""
18331837
claims = _merge_claims_challenge_and_capabilities(
18341838
self._client_capabilities, claims_challenge)
1835-
if self._enable_broker:
1839+
if self._enable_broker and sys.platform in ("win32", "darwin"):
18361840
from .broker import _signin_silently
18371841
response = _signin_silently(
18381842
"https://{}/{}".format(self.authority.instance, self.authority.tenant),
@@ -1929,13 +1933,15 @@ def __init__(
19291933
*,
19301934
enable_broker_on_windows=None,
19311935
enable_broker_on_mac=None,
1936+
enable_broker_on_linux=None,
1937+
enable_broker_on_wsl=None,
19321938
**kwargs):
19331939
"""Same as :func:`ClientApplication.__init__`,
19341940
except that ``client_credential`` parameter shall remain ``None``.
19351941
19361942
.. note::
19371943
1938-
You may set enable_broker_on_windows and/or enable_broker_on_mac to True.
1944+
You may set enable_broker_on_windows and/or enable_broker_on_mac and/or enable_broker_on_linux and/or enable_broker_on_wsl to True.
19391945
19401946
**What is a broker, and why use it?**
19411947
@@ -1963,9 +1969,11 @@ def __init__(
19631969
if your app is expected to run on Windows 10+
19641970
* ``msauth.com.msauth.unsignedapp://auth``
19651971
if your app is expected to run on Mac
1972+
* ``ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id``
1973+
if your app is expected to run on Linux, especially WSL
19661974
19671975
2. installed broker dependency,
1968-
e.g. ``pip install msal[broker]>=1.31,<2``.
1976+
e.g. ``pip install msal[broker]>=1.33,<2``.
19691977
19701978
3. tested with ``acquire_token_interactive()`` and ``acquire_token_silent()``.
19711979
@@ -2003,12 +2011,29 @@ def __init__(
20032011
This parameter defaults to None, which means MSAL will not utilize a broker.
20042012
20052013
New in MSAL Python 1.31.0.
2014+
2015+
:param boolean enable_broker_on_linux:
2016+
This setting is only effective if your app is running on Linux, including WSL.
2017+
This parameter defaults to None, which means MSAL will not utilize a broker.
2018+
2019+
New in MSAL Python 1.33.0.
2020+
2021+
:param boolean enable_broker_on_wsl:
2022+
This setting is only effective if your app is running on WSL.
2023+
This parameter defaults to None, which means MSAL will not utilize a broker.
2024+
2025+
New in MSAL Python 1.33.0.
20062026
"""
20072027
if client_credential is not None:
20082028
raise ValueError("Public Client should not possess credentials")
2029+
20092030
self._enable_broker = bool(
20102031
enable_broker_on_windows and sys.platform == "win32"
2011-
or enable_broker_on_mac and sys.platform == "darwin")
2032+
or enable_broker_on_mac and sys.platform == "darwin"
2033+
or enable_broker_on_linux and sys.platform == "linux"
2034+
or enable_broker_on_wsl and is_wsl()
2035+
)
2036+
20122037
super(PublicClientApplication, self).__init__(
20132038
client_id, client_credential=None, **kwargs)
20142039

@@ -2137,6 +2162,8 @@ def acquire_token_interactive(
21372162
False
21382163
) and data.get("token_type") != "ssh-cert" # Work around a known issue as of PyMsalRuntime 0.8
21392164
self._validate_ssh_cert_input_data(data)
2165+
is_ssh_cert_or_pop_request = _is_ssh_cert_or_pop_request(data.get("token_type"), auth_scheme)
2166+
21402167
if not on_before_launching_ui:
21412168
on_before_launching_ui = lambda **kwargs: None
21422169
if _is_running_in_cloud_shell() and prompt == "none":
@@ -2145,7 +2172,7 @@ def acquire_token_interactive(
21452172
return self._acquire_token_by_cloud_shell(scopes, data=data)
21462173
claims = _merge_claims_challenge_and_capabilities(
21472174
self._client_capabilities, claims_challenge)
2148-
if self._enable_broker:
2175+
if self._enable_broker and (sys.platform in ("win32", "darwin") or not is_ssh_cert_or_pop_request):
21492176
if parent_window_handle is None:
21502177
raise ValueError(
21512178
"parent_window_handle is required when you opted into using broker. "
@@ -2170,7 +2197,9 @@ def acquire_token_interactive(
21702197
)
21712198
return self._process_broker_response(response, scopes, data)
21722199

2173-
if auth_scheme:
2200+
if isinstance(auth_scheme, msal.auth_scheme.PopAuthScheme) and sys.platform == "linux":
2201+
raise ValueError("POP is not supported on Linux")
2202+
elif auth_scheme:
21742203
raise ValueError(self._AUTH_SCHEME_UNSUPPORTED)
21752204
on_before_launching_ui(ui="browser")
21762205
telemetry_context = self._build_telemetry_context(

msal/broker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
min_ver = {
2828
"win32": "1.20",
2929
"darwin": "1.31",
30+
"linux": "1.33",
3031
}.get(sys.platform)
3132
if min_ver:
3233
raise ImportError(

sample/interactive_sample.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
oidc_authority=os.getenv('OIDC_AUTHORITY'), # For External ID with custom domain
4848
#enable_broker_on_windows=True, # Opted in. You will be guided to meet the prerequisites, if your app hasn't already
4949
#enable_broker_on_mac=True, # Opted in. You will be guided to meet the prerequisites, if your app hasn't already
50-
50+
#enable_broker_on_linux=True, # Opted in. You will be guided to meet the prerequisites, if your app hasn't already
51+
#enable_broker_on_wsl=True, # Opted in. You will be guided to meet the prerequisites, if your app hasn't already
5152
token_cache=global_token_cache, # Let this app (re)use an existing token cache.
5253
# If absent, ClientApplication will create its own empty token cache
5354
)

setup.cfg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ broker =
6262
# most existing MSAL Python apps do not have the redirect_uri needed by broker.
6363
#
6464
# We need pymsalruntime.CallbackData introduced in PyMsalRuntime 0.14
65-
pymsalruntime>=0.14,<0.18; python_version>='3.6' and platform_system=='Windows'
65+
pymsalruntime>=0.14,<0.19; python_version>='3.6' and platform_system=='Windows'
6666
# On Mac, PyMsalRuntime 0.17+ is expected to support SSH cert and ROPC
67-
pymsalruntime>=0.17,<0.18; python_version>='3.8' and platform_system=='Darwin'
67+
pymsalruntime>=0.17,<0.19; python_version>='3.8' and platform_system=='Darwin'
68+
# PyMsalRuntime 0.18+ is expected to support broker on Linux
69+
pymsalruntime>=0.18,<0.19; python_version>='3.8' and platform_system=='Linux'
6870

6971
[options.packages.find]
7072
exclude =

tests/broker-test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
_AZURE_CLI,
4040
authority="https://login.microsoftonline.com/organizations",
4141
enable_broker_on_mac=True,
42-
enable_broker_on_windows=True)
42+
enable_broker_on_windows=True,
43+
enable_broker_on_linux=True,
44+
enable_broker_on_wsl=True,
45+
)
4346

4447
def interactive_and_silent(scopes, auth_scheme, data, expected_token_type):
4548
print("An account picker shall be pop up, possibly behind this console. Continue from there.")

tests/test_e2e.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def _build_app(cls,
193193
http_client=http_client or MinimalHttpClient(),
194194
enable_broker_on_windows=_PYMSALRUNTIME_INSTALLED,
195195
enable_broker_on_mac=_PYMSALRUNTIME_INSTALLED,
196+
enable_broker_on_linux=_PYMSALRUNTIME_INSTALLED,
197+
enable_broker_on_wsl=_PYMSALRUNTIME_INSTALLED,
196198
)
197199

198200
def _test_username_password(self,

0 commit comments

Comments
 (0)