@@ -181,6 +181,8 @@ class ClientApplication(object):
181
181
_TOKEN_SOURCE_CACHE = "cache"
182
182
_TOKEN_SOURCE_BROKER = "broker"
183
183
184
+ _enable_broker = False
185
+
184
186
def __init__ (
185
187
self , client_id ,
186
188
client_credential = None , authority = None , validate_authority = True ,
@@ -470,48 +472,7 @@ def __init__(
470
472
New in version 1.19.0.
471
473
472
474
: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.
515
476
516
477
:param boolean enable_pii_log:
517
478
When enabled, logs may include PII (Personal Identifiable Information).
@@ -584,34 +545,47 @@ def __init__(
584
545
)
585
546
else :
586
547
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 )
589
562
if is_confidential_app and allow_broker :
590
563
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
594
577
and not self .authority .is_adfs and not self .authority ._is_b2c ):
595
578
try :
596
579
from . import broker # Trigger Broker's initialization
597
- self ._enable_broker = True
598
580
if enable_pii_log :
599
581
broker ._enable_pii_log ()
600
582
except RuntimeError :
583
+ self ._enable_broker = False
601
584
logger .exception (
602
585
"Broker is unavailable on this platform. "
603
586
"We will fallback to non-broker." )
604
587
logger .debug ("Broker enabled? %s" , self ._enable_broker )
605
588
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
-
615
589
def _decorate_scope (
616
590
self , scopes ,
617
591
reserved_scope = frozenset (['openid' , 'profile' , 'offline_access' ])):
@@ -1746,9 +1720,53 @@ class PublicClientApplication(ClientApplication): # browser app or mobile app
1746
1720
def __init__ (self , client_id , client_credential = None , ** kwargs ):
1747
1721
"""Same as :func:`ClientApplication.__init__`,
1748
1722
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.
1749
1764
"""
1750
1765
if client_credential is not None :
1751
1766
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"
1752
1770
super (PublicClientApplication , self ).__init__ (
1753
1771
client_id , client_credential = None , ** kwargs )
1754
1772
0 commit comments