@@ -505,10 +505,17 @@ def __init__(
505
505
isinstance (self , ConfidentialClientApplication ) or self .client_credential )
506
506
if is_confidential_app and allow_broker :
507
507
raise ValueError ("allow_broker=True is only supported in PublicClientApplication" )
508
- self ._enable_broker = bool (
509
- allow_broker and not is_confidential_app
510
- and sys .platform == "win32"
511
- and not self .authority .is_adfs and not self .authority ._is_b2c )
508
+ self ._enable_broker = False
509
+ if (allow_broker and not is_confidential_app
510
+ and sys .platform == "win32"
511
+ and not self .authority .is_adfs and not self .authority ._is_b2c ):
512
+ try :
513
+ from . import broker # Trigger Broker's initialization
514
+ self ._enable_broker = True
515
+ except RuntimeError :
516
+ logger .exception (
517
+ "Broker is unavailable on this platform. "
518
+ "We will fallback to non-broker." )
512
519
logger .debug ("Broker enabled? %s" , self ._enable_broker )
513
520
514
521
self .token_cache = token_cache or TokenCache ()
@@ -1072,14 +1079,10 @@ def remove_account(self, account):
1072
1079
"""Sign me out and forget me from token cache"""
1073
1080
self ._forget_me (account )
1074
1081
if self ._enable_broker :
1075
- try :
1076
- from .broker import _signout_silently
1077
- except RuntimeError : # TODO: TBD
1078
- logger .debug ("Broker is unavailable on this platform. Fallback to non-broker." )
1079
- else :
1080
- error = _signout_silently (self .client_id , account ["local_account_id" ])
1081
- if error :
1082
- logger .debug ("_signout_silently() returns error: %s" , error )
1082
+ from .broker import _signout_silently
1083
+ error = _signout_silently (self .client_id , account ["local_account_id" ])
1084
+ if error :
1085
+ logger .debug ("_signout_silently() returns error: %s" , error )
1083
1086
1084
1087
def _sign_out (self , home_account ):
1085
1088
# Remove all relevant RTs and ATs from token cache
@@ -1312,22 +1315,18 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
1312
1315
return self ._acquire_token_by_cloud_shell (scopes , data = data )
1313
1316
1314
1317
if self ._enable_broker and account is not None and data .get ("token_type" ) != "ssh-cert" :
1315
- try :
1316
- from .broker import _acquire_token_silently
1317
- except RuntimeError : # TODO: TBD
1318
- logger .debug ("Broker is unavailable on this platform. Fallback to non-broker." )
1319
- else :
1320
- response = _acquire_token_silently (
1321
- "https://{}/{}" .format (self .authority .instance , self .authority .tenant ),
1322
- self .client_id ,
1323
- account ["local_account_id" ],
1324
- scopes ,
1325
- claims = _merge_claims_challenge_and_capabilities (
1326
- self ._client_capabilities , claims_challenge ),
1327
- correlation_id = correlation_id ,
1328
- ** data )
1329
- if response : # The broker provided a decisive outcome, so we use it
1330
- return self ._process_broker_response (response , scopes , data )
1318
+ from .broker import _acquire_token_silently
1319
+ response = _acquire_token_silently (
1320
+ "https://{}/{}" .format (self .authority .instance , self .authority .tenant ),
1321
+ self .client_id ,
1322
+ account ["local_account_id" ],
1323
+ scopes ,
1324
+ claims = _merge_claims_challenge_and_capabilities (
1325
+ self ._client_capabilities , claims_challenge ),
1326
+ correlation_id = correlation_id ,
1327
+ ** data )
1328
+ if response : # The broker provided a decisive outcome, so we use it
1329
+ return self ._process_broker_response (response , scopes , data )
1331
1330
1332
1331
result = _clean_up (self ._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family (
1333
1332
authority , self ._decorate_scope (scopes ), account ,
@@ -1533,24 +1532,20 @@ def acquire_token_by_username_password(
1533
1532
claims = _merge_claims_challenge_and_capabilities (
1534
1533
self ._client_capabilities , claims_challenge )
1535
1534
if self ._enable_broker :
1536
- try :
1537
- from .broker import _signin_silently
1538
- except RuntimeError : # TODO: TBD
1539
- logger .debug ("Broker is unavailable on this platform. Fallback to non-broker." )
1540
- else :
1541
- response = _signin_silently (
1542
- "https://{}/{}" .format (self .authority .instance , self .authority .tenant ),
1543
- self .client_id ,
1544
- scopes , # Decorated scopes won't work due to offline_access
1545
- MSALRuntime_Username = username ,
1546
- MSALRuntime_Password = password ,
1547
- validateAuthority = "no"
1548
- if self .authority ._validate_authority is False
1549
- or self .authority .is_adfs or self .authority ._is_b2c
1550
- else None ,
1551
- claims = claims ,
1552
- )
1553
- return self ._process_broker_response (response , scopes , kwargs .get ("data" , {}))
1535
+ from .broker import _signin_silently
1536
+ response = _signin_silently (
1537
+ "https://{}/{}" .format (self .authority .instance , self .authority .tenant ),
1538
+ self .client_id ,
1539
+ scopes , # Decorated scopes won't work due to offline_access
1540
+ MSALRuntime_Username = username ,
1541
+ MSALRuntime_Password = password ,
1542
+ validateAuthority = "no"
1543
+ if self .authority ._validate_authority is False
1544
+ or self .authority .is_adfs or self .authority ._is_b2c
1545
+ else None ,
1546
+ claims = claims ,
1547
+ )
1548
+ return self ._process_broker_response (response , scopes , kwargs .get ("data" , {}))
1554
1549
1555
1550
scopes = self ._decorate_scope (scopes )
1556
1551
telemetry_context = self ._build_telemetry_context (
@@ -1759,20 +1754,17 @@ def acquire_token_interactive(
1759
1754
"04f0c124-f2bc-4f59-8241-bf6df9866bbd" , # Visual Studio
1760
1755
] and data .get ("token_type" ) != "ssh-cert" # Work around a known issue as of PyMsalRuntime 0.8
1761
1756
)
1762
- try :
1763
- return self ._acquire_token_interactive_via_broker (
1764
- scopes ,
1765
- parent_window_handle ,
1766
- enable_msa_passthrough ,
1767
- claims ,
1768
- data ,
1769
- on_before_launching_ui ,
1770
- prompt = prompt ,
1771
- login_hint = login_hint ,
1772
- max_age = max_age ,
1773
- )
1774
- except RuntimeError : # TODO: TBD
1775
- logger .debug ("Broker is unavailable on this platform. Fallback to non-broker." )
1757
+ return self ._acquire_token_interactive_via_broker (
1758
+ scopes ,
1759
+ parent_window_handle ,
1760
+ enable_msa_passthrough ,
1761
+ claims ,
1762
+ data ,
1763
+ on_before_launching_ui ,
1764
+ prompt = prompt ,
1765
+ login_hint = login_hint ,
1766
+ max_age = max_age ,
1767
+ )
1776
1768
1777
1769
on_before_launching_ui (ui = "browser" )
1778
1770
telemetry_context = self ._build_telemetry_context (
0 commit comments