Skip to content

Commit c10ace1

Browse files
committed
Refactor
1 parent c380781 commit c10ace1

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
lines changed

msal/application.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,31 +1542,30 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
15421542
None, # Unknown data from older MSAL. Broker might still work.
15431543
):
15441544
from .broker import _acquire_token_silently
1545+
_authority = "https://{}/{}".format(
1546+
self.authority.instance, self.authority.tenant)
1547+
claims = _merge_claims_challenge_and_capabilities(
1548+
self._client_capabilities, claims_challenge)
15451549
response = _acquire_token_silently(
1546-
"https://{}/{}".format(self.authority.instance, self.authority.tenant),
1550+
_authority,
15471551
self.client_id,
15481552
account["local_account_id"],
15491553
scopes,
1550-
claims=_merge_claims_challenge_and_capabilities(
1551-
self._client_capabilities, claims_challenge),
1554+
claims=claims,
15521555
correlation_id=correlation_id,
15531556
auth_scheme=auth_scheme,
15541557
**data)
1555-
1556-
if (force_refresh and response.get("access_token")):
1557-
at_to_renew = response.get("access_token")
1558-
response = _acquire_token_silently(
1559-
"https://{}/{}".format(self.authority.instance, self.authority.tenant),
1560-
self.client_id,
1561-
account["local_account_id"],
1562-
scopes,
1563-
claims=_merge_claims_challenge_and_capabilities(
1564-
self._client_capabilities, claims_challenge),
1565-
correlation_id=correlation_id,
1566-
auth_scheme=auth_scheme,
1567-
at_to_renew= at_to_renew,
1568-
**data)
1569-
1558+
if force_refresh and response.get("access_token"):
1559+
response = _acquire_token_silently(
1560+
_authority,
1561+
self.client_id,
1562+
account["local_account_id"],
1563+
scopes,
1564+
claims=claims,
1565+
correlation_id=correlation_id,
1566+
auth_scheme=auth_scheme,
1567+
at_to_renew=response.get("access_token"),
1568+
**data)
15701569
if response: # Broker provides a decisive outcome
15711570
account_was_established_by_broker = account.get(
15721571
"account_source") == _GRANT_TYPE_BROKER

msal/broker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ def _signin_interactively(
214214

215215
def _acquire_token_silently(
216216
authority, client_id, account_id, scopes, claims=None, correlation_id=None,
217-
auth_scheme=None, at_to_renew=None,
217+
auth_scheme=None,
218+
at_to_renew=None,
218219
**kwargs):
219220
# For MSA PT scenario where you use the /organizations, yes,
220221
# acquireTokenSilently is expected to fail. - Sam Wilson

tests/test_account_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ def test_interactive_flow_and_its_silent_call_should_invoke_broker(self, _, mock
7373

7474
result = app.acquire_token_silent_with_error(
7575
[SCOPE], account, force_refresh=True, post=_mock_post)
76-
mocked_broker_ats.assert_called_once()
76+
mocked_broker_ats.assert_called()
7777
self.assertEqual(result["token_source"], "broker")
7878

tests/test_force_refresh.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
11
from tests import unittest
22
import msal
3-
import logging
43
import sys
54

6-
if not sys.platform.startswith("win"):
7-
raise unittest.SkipTest("Currently, our broker supports Windows")
85

9-
SCOPE_ARM = "https://management.azure.com/.default"
6+
if sys.platform not in ("win32", "darwin"):
7+
raise unittest.SkipTest(f"Our broker does not support {sys.platform}")
8+
9+
SCOPES = ["https://management.azure.com/.default"]
1010
_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
1111
pca = msal.PublicClientApplication(
1212
_AZURE_CLI,
1313
authority="https://login.microsoftonline.com/organizations",
1414
enable_broker_on_mac=True,
15-
enable_broker_on_windows=True)
15+
enable_broker_on_windows=True,
16+
)
17+
1618

1719
class ForceRefreshTestCase(unittest.TestCase):
18-
def test_silent_with_force_refresh(self):
19-
print("Testing silent flow with force_refresh=True")
20-
result = pca.acquire_token_interactive(scopes=[SCOPE_ARM], prompt="select_account", parent_window_handle=pca.CONSOLE_WINDOW_HANDLE, enable_msa_passthrough=True)
20+
def test_silent_with_force_refresh_should_return_a_new_token(self):
21+
result = pca.acquire_token_interactive(
22+
scopes=SCOPES,
23+
prompt="select_account",
24+
parent_window_handle=pca.CONSOLE_WINDOW_HANDLE,
25+
enable_msa_passthrough=True,
26+
)
2127
accounts = pca.get_accounts()
28+
self.assertNotEqual(
29+
[], accounts,
30+
"Interactive flow should have established a logged-in account")
2231
account = accounts[0]
23-
assert account, "The logged in account should have been established by interactive flow"
24-
oldToken = result.get("access_token")
25-
26-
27-
result = pca.acquire_token_silent(
28-
scopes=[SCOPE_ARM],
29-
account=account,
30-
force_refresh=False)
31-
32-
# This token should have been recieved from cache
33-
assert result.get("access_token") == oldToken, "Token should not be refreshed"
34-
35-
36-
result = pca.acquire_token_silent(
37-
scopes=[SCOPE_ARM],
38-
account=account,
39-
force_refresh=True)
40-
41-
# Token will be different proving it is not token from cache and was renewed
42-
assert result.get("access_token") != oldToken, "Token should be refreshed"
32+
old_token = result.get("access_token")
33+
34+
result = pca.acquire_token_silent(SCOPES, account)
35+
assertion = "This token should have been received from cache"
36+
self.assertEqual(result.get("access_token"), old_token, assertion)
37+
self.assertEqual(result.get("token_source"), "cache", assertion)
38+
39+
result = pca.acquire_token_silent(SCOPES, account, force_refresh=True)
40+
assertion = "A new token should have been received from broker"
41+
self.assertNotEqual(result.get("access_token"), old_token, assertion)
42+
self.assertEqual(result.get("token_source"), "broker", assertion)
43+

0 commit comments

Comments
 (0)