Skip to content

Commit 457f20b

Browse files
committed
Adopting PyMsalRuntime 0.7
1 parent 5582982 commit 457f20b

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

msal/application.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ def acquire_token_interactive(
16601660
self._client_capabilities, claims_challenge)
16611661
if self._enable_broker:
16621662
try:
1663-
from .broker import _signin_interactively
1663+
from .broker import _signin_interactively, _signin_silently
16641664
except RuntimeError: # TODO: TBD
16651665
logger.debug("Broker is unavailable on this platform. Fallback to non-broker.")
16661666
else:
@@ -1670,20 +1670,29 @@ def acquire_token_interactive(
16701670
"which is not supported on current platform")
16711671
if "welcome_template" in kwargs:
16721672
logger.debug(kwargs["welcome_template"]) # Experimental
1673-
response = _signin_interactively(
1674-
"https://{}/{}".format(self.authority.instance, self.authority.tenant),
1675-
self.client_id,
1676-
scopes,
1677-
validateAuthority="no"
1678-
if self.authority._validate_authority is False
1679-
or self.authority.is_adfs or self.authority._is_b2c
1680-
else None,
1681-
login_hint=login_hint,
1682-
prompt=prompt,
1683-
claims=claims,
1684-
max_age=max_age, # Broker may choose to trust the auth_time returned by AAD
1685-
window=window,
1686-
)
1673+
authority = "https://{}/{}".format(
1674+
self.authority.instance, self.authority.tenant)
1675+
validate_authority = ("no"
1676+
if self.authority._validate_authority is False
1677+
or self.authority.is_adfs or self.authority._is_b2c
1678+
else None)
1679+
if (prompt and prompt != "none") or login_hint:
1680+
response = _signin_interactively(
1681+
authority, self.client_id, scopes,
1682+
validateAuthority=validate_authority,
1683+
login_hint=login_hint,
1684+
prompt=prompt,
1685+
claims=claims,
1686+
max_age=max_age, # Broker may choose to trust the auth_time returned by AAD
1687+
window=window,
1688+
)
1689+
else:
1690+
response = _signin_silently(
1691+
authority, self.client_id, scopes,
1692+
validateAuthority=validate_authority,
1693+
claims=claims,
1694+
max_age=max_age, # Broker may choose to trust the auth_time returned by AAD
1695+
)
16871696
return self._process_broker_response(response, scopes, kwargs.get("data", {}))
16881697

16891698
self._validate_ssh_cert_input_data(kwargs.get("data", {}))

msal/broker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
try:
1212
import pymsalruntime # ImportError would be raised on unsupported platforms such as Windows 8
1313
# Its API description is available in site-packages/pymsalruntime/PyMsalRuntime.pyi
14-
pymsalruntime.set_logging_callback(lambda message, level: { # New in pymsalruntime 0.5.0
14+
pymsalruntime.register_logging_callback(lambda message, level: { # New in pymsalruntime 0.7
1515
pymsalruntime.LogLevel.TRACE: logger.debug, # Python has no TRACE level
1616
pymsalruntime.LogLevel.DEBUG: logger.debug,
1717
# Let broker's excess info, warning and error logs map into default DEBUG, for now
@@ -99,9 +99,11 @@ def _get_new_correlation_id():
9999
return str(uuid.uuid4())
100100

101101

102-
def _signin_silently(authority, client_id, scopes, correlation_id=None, **kwargs):
102+
def _signin_silently(authority, client_id, scopes, correlation_id=None, claims=None, **kwargs):
103103
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
104104
params.set_requested_scopes(scopes)
105+
if claims:
106+
params.set_decoded_claims(claims)
105107
callback_data = _CallbackData()
106108
for k, v in kwargs.items(): # This can be used to support domain_hint, max_age, etc.
107109
if v is not None:
@@ -136,8 +138,6 @@ def _signin_interactively(
136138
# https://identitydivision.visualstudio.com/Engineering/_workitems/edit/1744492
137139
login_hint = None # Mimicing the AAD behavior
138140
logger.warning("Using both select_account and login_hint is ambiguous. Ignoring login_hint.")
139-
params.set_select_account_option(
140-
pymsalruntime.SelectAccountOption.SHOWLOCALACCOUNTSCONTROL)
141141
else:
142142
logger.warning("prompt=%s is not supported by this module", prompt)
143143
for k, v in kwargs.items(): # This can be used to support domain_hint, max_age, etc.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
# The broker is defined as optional dependency,
9292
# so that downstream apps can opt in. The opt-in is needed, partially because
9393
# most existing MSAL Python apps do not have the redirect_uri needed by broker.
94-
"pymsalruntime>=0.5,<0.6;python_version>='3.6' and platform_system=='Windows'",
94+
"pymsalruntime>=0.7,<0.8;python_version>='3.6' and platform_system=='Windows'",
9595
],
9696
},
9797
)

tests/test_broker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
if not sys.platform.startswith("win"):
66
raise unittest.SkipTest("Currently, our broker supports Windows")
77
from msal.broker import ( # Import them after the platform check
8-
_signin_interactively, _acquire_token_silently, RedirectUriError)
8+
_signin_silently, _signin_interactively, _acquire_token_silently, RedirectUriError)
99

1010

1111
logging.basicConfig(level=logging.DEBUG)
@@ -20,7 +20,7 @@ class BrokerTestCase(unittest.TestCase):
2020
_authority = "https://login.microsoftonline.com/common"
2121
_scopes = ["https://graph.microsoft.com/.default"]
2222

23-
def test_interactive_then_silent(self):
23+
def test_signin_interactive_then_acquire_token_silent(self):
2424
result = _signin_interactively(self._authority, self._client_id, self._scopes)
2525
self.assertIsNotNone(result.get("access_token"), result)
2626

@@ -45,3 +45,7 @@ def test_signin_interactively_and_select_account(self):
4545
result["access_token"] = "********"
4646
import pprint; pprint.pprint(result)
4747

48+
def test_signin_silently(self):
49+
result = _signin_silently(self._authority, self._client_id, self._scopes)
50+
self.assertIsNotNone(result.get("access_token"), result)
51+

0 commit comments

Comments
 (0)