Skip to content

Commit 3d3d02f

Browse files
committed
Deprecate allow_broker, use enable_broker_on_windows
1 parent b3b2195 commit 3d3d02f

File tree

4 files changed

+100
-76
lines changed

4 files changed

+100
-76
lines changed

msal/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ def _main():
190190
option_renderer=lambda a: a["name"],
191191
header="Impersonate this app (or you can type in the client_id of your own app)",
192192
accept_nonempty_string=True)
193-
allow_broker = _input_boolean("Allow broker?")
193+
enable_broker = _input_boolean("Enable broker? It will error out later if your app has not registered some redirect URI")
194194
enable_debug_log = _input_boolean("Enable MSAL Python's DEBUG log?")
195-
enable_pii_log = _input_boolean("Enable PII in broker's log?") if allow_broker and enable_debug_log else False
195+
enable_pii_log = _input_boolean("Enable PII in broker's log?") if enable_broker and enable_debug_log else False
196196
app = msal.PublicClientApplication(
197197
chosen_app["client_id"] if isinstance(chosen_app, dict) else chosen_app,
198198
authority=_select_options([
@@ -205,7 +205,7 @@ def _main():
205205
header="Input authority (Note that MSA-PT apps would NOT use the /common authority)",
206206
accept_nonempty_string=True,
207207
),
208-
allow_broker=allow_broker,
208+
enable_broker_on_windows=enable_broker,
209209
enable_pii_log=enable_pii_log,
210210
)
211211
if enable_debug_log:

msal/application.py

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class ClientApplication(object):
181181
_TOKEN_SOURCE_CACHE = "cache"
182182
_TOKEN_SOURCE_BROKER = "broker"
183183

184+
_enable_broker = False
185+
184186
def __init__(
185187
self, client_id,
186188
client_credential=None, authority=None, validate_authority=True,
@@ -470,48 +472,7 @@ def __init__(
470472
New in version 1.19.0.
471473
472474
:param boolean allow_broker:
473-
This parameter is NOT applicable to :class:`ConfidentialClientApplication`.
474-
475-
A broker is a component installed on your device.
476-
Broker implicitly gives your device an identity. By using a broker,
477-
your device becomes a factor that can satisfy MFA (Multi-factor authentication).
478-
This factor would become mandatory
479-
if a tenant's admin enables a corresponding Conditional Access (CA) policy.
480-
The broker's presence allows Microsoft identity platform
481-
to have higher confidence that the tokens are being issued to your device,
482-
and that is more secure.
483-
484-
An additional benefit of broker is,
485-
it runs as a long-lived process with your device's OS,
486-
and maintains its own cache,
487-
so that your broker-enabled apps (even a CLI)
488-
could automatically SSO from a previously established signed-in session.
489-
490-
This parameter defaults to None, which means MSAL will not utilize a broker.
491-
If this parameter is set to True,
492-
MSAL will use the broker whenever possible,
493-
and automatically fall back to non-broker behavior.
494-
That also means your app does not need to enable broker conditionally,
495-
you can always set allow_broker to True,
496-
as long as your app meets the following prerequisite:
497-
498-
* Installed optional dependency, e.g. ``pip install msal[broker]>=1.20,<2``.
499-
(Note that broker is currently only available on Windows 10+)
500-
501-
* Register a new redirect_uri for your desktop app as:
502-
``ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id``
503-
504-
* Tested your app in following scenarios:
505-
506-
* Windows 10+
507-
508-
* PublicClientApplication's following methods::
509-
acquire_token_interactive(), acquire_token_by_username_password(),
510-
acquire_token_silent() (or acquire_token_silent_with_error()).
511-
512-
* AAD and MSA accounts (i.e. Non-ADFS, non-B2C)
513-
514-
New in version 1.20.0.
475+
Deprecated. Please use ``enable_broker_on_windows`` instead.
515476
516477
:param boolean enable_pii_log:
517478
When enabled, logs may include PII (Personal Identifiable Information).
@@ -584,34 +545,47 @@ def __init__(
584545
)
585546
else:
586547
raise
587-
is_confidential_app = bool(
588-
isinstance(self, ConfidentialClientApplication) or self.client_credential)
548+
549+
self._decide_broker(allow_broker, enable_pii_log)
550+
self.token_cache = token_cache or TokenCache()
551+
self._region_configured = azure_region
552+
self._region_detected = None
553+
self.client, self._regional_client = self._build_client(
554+
client_credential, self.authority)
555+
self.authority_groups = None
556+
self._telemetry_buffer = {}
557+
self._telemetry_lock = Lock()
558+
559+
def _decide_broker(self, allow_broker, enable_pii_log):
560+
is_confidential_app = self.client_credential or isinstance(
561+
self, ConfidentialClientApplication)
589562
if is_confidential_app and allow_broker:
590563
raise ValueError("allow_broker=True is only supported in PublicClientApplication")
591-
self._enable_broker = False
592-
if (allow_broker and not is_confidential_app
593-
and sys.platform == "win32"
564+
# Historically, we chose to support ClientApplication("client_id", allow_broker=True)
565+
if allow_broker:
566+
warnings.warn(
567+
"allow_broker is deprecated. "
568+
"Please use PublicClientApplication(..., enable_broker_on_windows=True)",
569+
DeprecationWarning)
570+
self._enable_broker = self._enable_broker or (
571+
# When we started the broker project on Windows platform,
572+
# the allow_broker was meant to be cross-platform. Now we realize
573+
# that other platforms have different redirect_uri requirements,
574+
# so the old allow_broker is deprecated and will only for Windows.
575+
allow_broker and sys.platform == "win32")
576+
if (self._enable_broker and not is_confidential_app
594577
and not self.authority.is_adfs and not self.authority._is_b2c):
595578
try:
596579
from . import broker # Trigger Broker's initialization
597-
self._enable_broker = True
598580
if enable_pii_log:
599581
broker._enable_pii_log()
600582
except RuntimeError:
583+
self._enable_broker = False
601584
logger.exception(
602585
"Broker is unavailable on this platform. "
603586
"We will fallback to non-broker.")
604587
logger.debug("Broker enabled? %s", self._enable_broker)
605588

606-
self.token_cache = token_cache or TokenCache()
607-
self._region_configured = azure_region
608-
self._region_detected = None
609-
self.client, self._regional_client = self._build_client(
610-
client_credential, self.authority)
611-
self.authority_groups = None
612-
self._telemetry_buffer = {}
613-
self._telemetry_lock = Lock()
614-
615589
def _decorate_scope(
616590
self, scopes,
617591
reserved_scope=frozenset(['openid', 'profile', 'offline_access'])):
@@ -1746,9 +1720,53 @@ class PublicClientApplication(ClientApplication): # browser app or mobile app
17461720
def __init__(self, client_id, client_credential=None, **kwargs):
17471721
"""Same as :func:`ClientApplication.__init__`,
17481722
except that ``client_credential`` parameter shall remain ``None``.
1723+
1724+
.. note::
1725+
1726+
You may set enable_broker_on_windows to True.
1727+
1728+
What is a broker, and why use it?
1729+
1730+
A broker is a component installed on your device.
1731+
Broker implicitly gives your device an identity. By using a broker,
1732+
your device becomes a factor that can satisfy MFA (Multi-factor authentication).
1733+
This factor would become mandatory
1734+
if a tenant's admin enables a corresponding Conditional Access (CA) policy.
1735+
The broker's presence allows Microsoft identity platform
1736+
to have higher confidence that the tokens are being issued to your device,
1737+
and that is more secure.
1738+
1739+
An additional benefit of broker is,
1740+
it runs as a long-lived process with your device's OS,
1741+
and maintains its own cache,
1742+
so that your broker-enabled apps (even a CLI)
1743+
could automatically SSO from a previously established signed-in session.
1744+
1745+
ADFS and B2C do not support broker.
1746+
MSAL will automatically fallback to use browser.
1747+
1748+
You shall only enable broker when your app:
1749+
1750+
1. is running on supported platforms,
1751+
and already registered their corresponding redirect_uri
1752+
1753+
* ``ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id``
1754+
if your app is expected to run on Windows 10+
1755+
1756+
2. installed broker dependency,
1757+
e.g. ``pip install msal[broker]>=1.25,<2``.
1758+
1759+
3. tested with ``acquire_token_interactive()`` and ``acquire_token_silent()``.
1760+
1761+
:param boolean enable_broker_on_windows:
1762+
This setting is only effective if your app is running on Windows 10+.
1763+
This parameter defaults to None, which means MSAL will not utilize a broker.
17491764
"""
17501765
if client_credential is not None:
17511766
raise ValueError("Public Client should not possess credentials")
1767+
# Using kwargs notation for now. We will switch to keyword-only arguments.
1768+
enable_broker_on_windows = kwargs.pop("enable_broker_on_windows", False)
1769+
self._enable_broker = enable_broker_on_windows and sys.platform == "win32"
17521770
super(PublicClientApplication, self).__init__(
17531771
client_id, client_credential=None, **kwargs)
17541772

sample/interactive_sample.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
# Create a preferably long-lived app instance, to avoid the overhead of app creation
3838
global_app = msal.PublicClientApplication(
3939
config["client_id"], authority=config["authority"],
40-
#allow_broker=True, # If opted in, you will be guided to meet the prerequisites, when applicable
41-
# See also: https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-desktop-acquire-token-wam#wam-value-proposition
40+
#enable_broker_on_windows=True, # Opted in. You will be guided to meet the prerequisites, if your app hasn't already
41+
# See also: https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-desktop-acquire-token-wam#wam-value-proposition
4242
token_cache=global_token_cache, # Let this app (re)use an existing token cache.
4343
# If absent, ClientApplication will create its own empty token cache
4444
)

tests/test_e2e.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,27 @@ 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+
)
183189

184190
def _test_username_password(self,
185191
authority=None, client_id=None, username=None, password=None, scope=None,

0 commit comments

Comments
 (0)