Skip to content

Commit 85c93f8

Browse files
committed
Merge branch 'broker-refactor' into dev
2 parents 828b419 + 4ce6646 commit 85c93f8

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

msal/application.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
logger = logging.getLogger(__name__)
2727
_AUTHORITY_TYPE_CLOUDSHELL = "CLOUDSHELL"
2828

29+
def _init_broker(enable_pii_log): # Make it a function to allow mocking
30+
from . import broker # Trigger Broker's initialization, lazily
31+
if enable_pii_log:
32+
broker._enable_pii_log()
33+
2934
def extract_certs(public_cert_content):
3035
# Parses raw public certificate file contents and returns a list of strings
3136
# Usage: headers = {"x5c": extract_certs(open("my_cert.pem").read())}
@@ -216,8 +221,6 @@ class ClientApplication(object):
216221
"You can enable broker by following these instructions. "
217222
"https://msal-python.readthedocs.io/en/latest/#publicclientapplication")
218223

219-
_enable_broker = False
220-
221224
def __init__(
222225
self, client_id,
223226
client_credential=None, authority=None, validate_authority=True,
@@ -646,18 +649,24 @@ def _decide_broker(self, allow_broker, enable_pii_log):
646649
"enable_broker_on_windows=True, "
647650
"enable_broker_on_mac=...)",
648651
DeprecationWarning)
649-
self._enable_broker = self._enable_broker or (
652+
opted_in_for_broker = (
653+
self._enable_broker # True means Opted-in from PCA
654+
or (
650655
# When we started the broker project on Windows platform,
651656
# the allow_broker was meant to be cross-platform. Now we realize
652657
# that other platforms have different redirect_uri requirements,
653658
# so the old allow_broker is deprecated and will only for Windows.
654659
allow_broker and sys.platform == "win32")
655-
if (self._enable_broker and not is_confidential_app
656-
and not self.authority.is_adfs and not self.authority._is_b2c):
660+
)
661+
self._enable_broker = ( # This same variable will also store the state
662+
opted_in_for_broker
663+
and not is_confidential_app
664+
and not self.authority.is_adfs
665+
and not self.authority._is_b2c
666+
)
667+
if self._enable_broker:
657668
try:
658-
from . import broker # Trigger Broker's initialization
659-
if enable_pii_log:
660-
broker._enable_pii_log()
669+
_init_broker(enable_pii_log)
661670
except RuntimeError:
662671
self._enable_broker = False
663672
logger.exception(

tests/test_application.py

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Note: Since Aug 2019 we move all e2e tests into test_e2e.py,
22
# so this test_application file contains only unit tests without dependency.
3+
import json
4+
import logging
35
import sys
46
import time
5-
from msal.application import *
6-
from msal.application import _str2bytes
7+
from unittest.mock import patch, Mock
78
import msal
8-
from msal.application import _merge_claims_challenge_and_capabilities
9+
from msal.application import (
10+
extract_certs,
11+
ClientApplication, PublicClientApplication, ConfidentialClientApplication,
12+
_str2bytes, _merge_claims_challenge_and_capabilities,
13+
)
914
from tests import unittest
1015
from tests.test_token_cache import build_id_token, build_response
1116
from tests.http_client import MinimalHttpClient, MinimalResponse
@@ -722,3 +727,63 @@ def test_client_id_should_be_a_valid_scope(self):
722727
self._test_client_id_should_be_a_valid_scope("client_id", [])
723728
self._test_client_id_should_be_a_valid_scope("client_id", ["foo"])
724729

730+
731+
@patch("sys.platform", new="darwin") # Pretend running on Mac.
732+
@patch("msal.authority.tenant_discovery", new=Mock(return_value={
733+
"authorization_endpoint": "https://contoso.com/placeholder",
734+
"token_endpoint": "https://contoso.com/placeholder",
735+
}))
736+
@patch("msal.application._init_broker", new=Mock()) # Allow testing without pymsalruntime
737+
class TestBrokerFallback(unittest.TestCase):
738+
739+
def test_broker_should_be_disabled_by_default(self):
740+
app = msal.PublicClientApplication(
741+
"client_id",
742+
authority="https://login.microsoftonline.com/common",
743+
)
744+
self.assertFalse(app._enable_broker)
745+
746+
def test_broker_should_be_enabled_when_opted_in(self):
747+
app = msal.PublicClientApplication(
748+
"client_id",
749+
authority="https://login.microsoftonline.com/common",
750+
enable_broker_on_mac=True,
751+
)
752+
self.assertTrue(app._enable_broker)
753+
754+
def test_should_fallback_to_non_broker_when_using_adfs(self):
755+
app = msal.PublicClientApplication(
756+
"client_id",
757+
authority="https://contoso.com/adfs",
758+
#instance_discovery=False, # Automatically skipped when detected ADFS
759+
enable_broker_on_mac=True,
760+
)
761+
self.assertFalse(app._enable_broker)
762+
763+
def test_should_fallback_to_non_broker_when_using_b2c(self):
764+
app = msal.PublicClientApplication(
765+
"client_id",
766+
authority="https://contoso.b2clogin.com/contoso/policy",
767+
#instance_discovery=False, # Automatically skipped when detected B2C
768+
enable_broker_on_mac=True,
769+
)
770+
self.assertFalse(app._enable_broker)
771+
772+
def test_should_use_broker_when_disabling_instance_discovery(self):
773+
app = msal.PublicClientApplication(
774+
"client_id",
775+
authority="https://contoso.com/path",
776+
instance_discovery=False, # Need this for a generic authority url
777+
enable_broker_on_mac=True,
778+
)
779+
# TODO: Shall we bypass broker when opted out of instance discovery?
780+
self.assertTrue(app._enable_broker) # Current implementation enables broker
781+
782+
def test_should_fallback_to_non_broker_when_using_oidc_authority(self):
783+
app = msal.PublicClientApplication(
784+
"client_id",
785+
oidc_authority="https://contoso.com/path",
786+
enable_broker_on_mac=True,
787+
)
788+
self.assertFalse(app._enable_broker)
789+

0 commit comments

Comments
 (0)