Skip to content

Commit 36e0ee8

Browse files
committed
Use 2 flags, one per supported platform
1 parent 51b6ec1 commit 36e0ee8

File tree

4 files changed

+93
-87
lines changed

4 files changed

+93
-87
lines changed

msal/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _main():
191191
option_renderer=lambda a: a["name"],
192192
header="Impersonate this app (or you can type in the client_id of your own app)",
193193
accept_nonempty_string=True)
194-
enable_broker = _input_boolean("Enable broker?")
194+
enable_broker = _input_boolean("Enable broker?") # It will error out later if your app has not registered some redirect URI
195195
enable_debug_log = _input_boolean("Enable MSAL Python's DEBUG log?")
196196
enable_pii_log = _input_boolean("Enable PII in broker's log?") if enable_broker and enable_debug_log else False
197197
app = msal.PublicClientApplication(
@@ -206,7 +206,8 @@ def _main():
206206
header="Input authority (Note that MSA-PT apps would NOT use the /common authority)",
207207
accept_nonempty_string=True,
208208
),
209-
enable_broker=enable_broker,
209+
enable_broker_on_windows=enable_broker,
210+
enable_broker_on_mac=enable_broker,
210211
enable_pii_log=enable_pii_log,
211212
)
212213
if enable_debug_log:

msal/application.py

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ class ClientApplication(object):
177177

178178
ATTEMPT_REGION_DISCOVERY = True # "TryAutoDetect"
179179

180+
_enable_broker = False
181+
180182
def __init__(
181183
self, client_id,
182184
client_credential=None, authority=None, validate_authority=True,
@@ -194,7 +196,6 @@ def __init__(
194196
instance_discovery=None,
195197
allow_broker=None,
196198
enable_pii_log=None,
197-
enable_broker=None,
198199
):
199200
"""Create an instance of application.
200201
@@ -467,51 +468,7 @@ def __init__(
467468
New in version 1.19.0.
468469
469470
:param boolean allow_broker:
470-
Deprecated. It will only work on Windows platform.
471-
Please use ``enable_broker`` instead.
472-
473-
:param boolean enable_broker:
474-
This parameter is NOT applicable to :class:`ConfidentialClientApplication`.
475-
476-
This setting is platform-dependent.
477-
We currently support Windows 10+ and Mac.
478-
You shall only enable broker when your app:
479-
480-
1. is running on supported platforms,
481-
and already registered their corresponding redirect_uri
482-
483-
* ``ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id``
484-
if your app is expected to run on Windows 10+
485-
* ``msauth.com.msauth.unsignedapp://auth``
486-
if your app is expected to run on Mac
487-
488-
2. installed broker dependency,
489-
e.g. ``pip install msal[broker]>=1.24,<2``.
490-
491-
3. tested with ``acquire_token_interactive()`` at least.
492-
493-
This parameter defaults to None,
494-
which means MSAL will not utilize a broker.
495-
496-
What is a broker, and why use it?
497-
498-
A broker is a component installed on your device.
499-
Broker implicitly gives your device an identity. By using a broker,
500-
your device becomes a factor that can satisfy MFA (Multi-factor authentication).
501-
This factor would become mandatory
502-
if a tenant's admin enables a corresponding Conditional Access (CA) policy.
503-
The broker's presence allows Microsoft identity platform
504-
to have higher confidence that the tokens are being issued to your device,
505-
and that is more secure.
506-
507-
An additional benefit of broker is,
508-
it runs as a long-lived process with your device's OS,
509-
and maintains its own cache,
510-
so that your broker-enabled apps (even a CLI)
511-
could automatically SSO from a previously established signed-in session.
512-
513-
Note: ADFS and B2C do not support broker.
514-
MSAL will automatically fallback to use browser.
471+
Deprecated. Please use ``enable_broker_on_windows`` instead.
515472
516473
:param boolean enable_pii_log:
517474
When enabled, logs may include PII (Personal Identifiable Information).
@@ -585,10 +542,7 @@ def __init__(
585542
else:
586543
raise
587544

588-
self._enable_broker = self._decide_broker(
589-
enable_broker=enable_broker, allow_broker=allow_broker,
590-
enable_pii_log=enable_pii_log,
591-
)
545+
self._decide_broker(allow_broker, enable_pii_log)
592546
self.token_cache = token_cache or TokenCache()
593547
self._region_configured = azure_region
594548
self._region_detected = None
@@ -598,45 +552,35 @@ def __init__(
598552
self._telemetry_buffer = {}
599553
self._telemetry_lock = Lock()
600554

601-
def _decide_broker(
602-
self,
603-
enable_broker=None,
604-
allow_broker=None,
605-
enable_pii_log=None,
606-
):
607-
_enable_broker = False
608-
is_confidential_app = bool(
609-
isinstance(self, ConfidentialClientApplication) or self.client_credential)
555+
def _decide_broker(self, allow_broker, enable_pii_log):
556+
is_confidential_app = self.client_credential or isinstance(
557+
self, ConfidentialClientApplication)
610558
if is_confidential_app and allow_broker:
611559
raise ValueError("allow_broker=True is only supported in PublicClientApplication")
560+
# Historically, we chose to support ClientApplication("client_id", allow_broker=True)
612561
if allow_broker:
613562
warnings.warn(
614563
"allow_broker is deprecated. "
615-
"Please use enable_broker conditionally per platform.",
564+
"Please use enable_broker_on_windows and/or enable_broker_on_mac",
616565
DeprecationWarning)
617-
if is_confidential_app and enable_broker:
618-
raise ValueError("enable_broker=True is only supported in PublicClientApplication")
619-
if enable_broker and sys.platform not in ["win32", "darwin"]:
620-
raise ValueError("enable_broker can only run on Windows or Mac")
621-
opts_in = enable_broker or (
566+
self._enable_broker = self._enable_broker or (
622567
# When we started the broker project on Windows platform,
623568
# the allow_broker was meant to be cross-platform. Now we realize
624569
# that other platforms have different redirect_uri requirements,
625570
# so the old allow_broker is deprecated and will only for Windows.
626571
allow_broker and sys.platform == "win32")
627-
if (opts_in and not is_confidential_app
572+
if (self._enable_broker and not is_confidential_app
628573
and not self.authority.is_adfs and not self.authority._is_b2c):
629574
try:
630575
from . import broker # Trigger Broker's initialization
631576
if enable_pii_log:
632577
broker._enable_pii_log()
633-
_enable_broker = True
634578
except RuntimeError:
579+
self._enable_broker = False
635580
logger.exception(
636581
"Broker is unavailable on this platform. "
637582
"We will fallback to non-broker.")
638-
logger.debug("Broker enabled? %s", _enable_broker)
639-
return _enable_broker
583+
logger.debug("Broker enabled? %s", self._enable_broker)
640584

641585
def _decorate_scope(
642586
self, scopes,
@@ -1759,9 +1703,62 @@ class PublicClientApplication(ClientApplication): # browser app or mobile app
17591703
def __init__(self, client_id, client_credential=None, **kwargs):
17601704
"""Same as :func:`ClientApplication.__init__`,
17611705
except that ``client_credential`` parameter shall remain ``None``.
1706+
1707+
.. note::
1708+
1709+
You may set enable_broker_on_windows and/or enable_broker_on_mac to True.
1710+
1711+
What is a broker, and why use it?
1712+
1713+
A broker is a component installed on your device.
1714+
Broker implicitly gives your device an identity. By using a broker,
1715+
your device becomes a factor that can satisfy MFA (Multi-factor authentication).
1716+
This factor would become mandatory
1717+
if a tenant's admin enables a corresponding Conditional Access (CA) policy.
1718+
The broker's presence allows Microsoft identity platform
1719+
to have higher confidence that the tokens are being issued to your device,
1720+
and that is more secure.
1721+
1722+
An additional benefit of broker is,
1723+
it runs as a long-lived process with your device's OS,
1724+
and maintains its own cache,
1725+
so that your broker-enabled apps (even a CLI)
1726+
could automatically SSO from a previously established signed-in session.
1727+
1728+
ADFS and B2C do not support broker.
1729+
MSAL will automatically fallback to use browser.
1730+
1731+
You shall only enable broker when your app:
1732+
1733+
1. is running on supported platforms,
1734+
and already registered their corresponding redirect_uri
1735+
1736+
* ``ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id``
1737+
if your app is expected to run on Windows 10+
1738+
* ``msauth.com.msauth.unsignedapp://auth``
1739+
if your app is expected to run on Mac
1740+
1741+
2. installed broker dependency,
1742+
e.g. ``pip install msal[broker]>=1.25,<2``.
1743+
1744+
3. tested with ``acquire_token_interactive()`` at least.
1745+
1746+
:param boolean enable_broker_on_windows:
1747+
This setting is only effective if your app is running on Windows 10+.
1748+
This parameter defaults to None, which means MSAL will not utilize a broker.
1749+
1750+
:param boolean enable_broker_on_mac:
1751+
This setting is only effective if your app is running on Mac.
1752+
This parameter defaults to None, which means MSAL will not utilize a broker.
17621753
"""
17631754
if client_credential is not None:
17641755
raise ValueError("Public Client should not possess credentials")
1756+
# Using kwargs notation for now. We will switch to keyword-only arguments.
1757+
enable_broker_on_windows = kwargs.pop("enable_broker_on_windows", False)
1758+
enable_broker_on_mac = kwargs.pop("enable_broker_on_mac", False)
1759+
self._enable_broker = bool(
1760+
enable_broker_on_windows and sys.platform == "win32"
1761+
or enable_broker_on_mac and sys.platform == "darwin")
17651762
super(PublicClientApplication, self).__init__(
17661763
client_id, client_credential=None, **kwargs)
17671764

@@ -2073,7 +2070,7 @@ def acquire_token_by_device_flow(self, flow, claims_challenge=None, **kwargs):
20732070

20742071
class ConfidentialClientApplication(ClientApplication): # server-side web app
20752072
"""Same as :func:`ClientApplication.__init__`,
2076-
except that ``allow_broker`` and ``enable_broker`` parameters shall remain ``None``.
2073+
except that ``allow_broker`` parameter shall remain ``None``.
20772074
"""
20782075

20792076
def acquire_token_for_client(self, scopes, claims_challenge=None, **kwargs):

sample/interactive_sample.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
# Create a preferably long-lived app instance which maintains a token cache.
3434
app = msal.PublicClientApplication(
3535
config["client_id"], authority=config["authority"],
36-
#allow_broker=True, # If opted in, you will be guided to meet the prerequisites, when applicable
36+
#enable_broker_on_windows=True, # Opted in. You will be guided to meet the prerequisites, if your app hasn't already
37+
#enable_broker_on_mac=True, # Opted in. You will be guided to meet the prerequisites, if your app hasn't already
3738
# See also: https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-desktop-acquire-token-wam#wam-value-proposition
3839
# token_cache=... # Default cache is in memory only.
3940
# You can learn how to use SerializableTokenCache from

tests/test_e2e.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,28 @@ def _build_app(cls,
165165
http_client=None,
166166
azure_region=None,
167167
**kwargs):
168-
try:
169-
import pymsalruntime
170-
broker_available = True
171-
except ImportError:
172-
broker_available = False
173-
return (msal.ConfidentialClientApplication
174-
if client_credential else msal.PublicClientApplication)(
175-
client_id,
176-
client_credential=client_credential,
177-
authority=authority,
178-
azure_region=azure_region,
179-
http_client=http_client or MinimalHttpClient(),
180-
allow_broker=broker_available # This way, we reuse same test cases, by run them with and without broker
181-
and not client_credential,
168+
if client_credential:
169+
return msal.ConfidentialClientApplication(
170+
client_id,
171+
client_credential=client_credential,
172+
authority=authority,
173+
azure_region=azure_region,
174+
http_client=http_client or MinimalHttpClient(),
182175
)
176+
else:
177+
# Reuse same test cases, by run them with and without broker
178+
try:
179+
import pymsalruntime
180+
broker_available = True
181+
except ImportError:
182+
broker_available = False
183+
return msal.PublicClientApplication(
184+
client_id,
185+
authority=authority,
186+
http_client=http_client or MinimalHttpClient(),
187+
enable_broker_on_windows=broker_available,
188+
enable_broker_on_mac=broker_available,
189+
)
183190

184191
def _test_username_password(self,
185192
authority=None, client_id=None, username=None, password=None, scope=None,

0 commit comments

Comments
 (0)