Skip to content

Commit 233a18f

Browse files
Ugonnaak1fengga
authored andcommitted
add support for force refresh in broker layer
remove commented code adjust check order Refactor
1 parent b9f3879 commit 233a18f

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ src/build
4949
docs/_build/
5050
# Visual Studio Files
5151
/.vs/*
52+
.vscode/*
5253
/tests/.vs/*
5354

5455
# vim files

msal/application.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,20 +1568,35 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
15681568
None, # Unknown data from older MSAL. Broker might still work.
15691569
):
15701570
from .broker import _acquire_token_silently
1571+
_authority = "https://{}/{}".format(
1572+
self.authority.instance, self.authority.tenant)
1573+
claims = _merge_claims_challenge_and_capabilities(
1574+
self._client_capabilities, claims_challenge)
15711575
response = _acquire_token_silently(
1572-
"https://{}/{}".format(self.authority.instance, self.authority.tenant),
1576+
_authority,
15731577
self.client_id,
15741578
account["local_account_id"],
15751579
scopes,
1576-
claims=_merge_claims_challenge_and_capabilities(
1577-
self._client_capabilities, claims_challenge),
1580+
claims=claims,
15781581
correlation_id=correlation_id,
15791582
auth_scheme=auth_scheme,
15801583
**data)
1584+
if force_refresh and response.get("access_token"):
1585+
response = _acquire_token_silently(
1586+
_authority,
1587+
self.client_id,
1588+
account["local_account_id"],
1589+
scopes,
1590+
claims=claims,
1591+
correlation_id=correlation_id,
1592+
auth_scheme=auth_scheme,
1593+
at_to_renew=response.get("access_token"),
1594+
**data)
15811595
if response: # Broker provides a decisive outcome
15821596
account_was_established_by_broker = account.get(
15831597
"account_source") == _GRANT_TYPE_BROKER
15841598
broker_attempt_succeeded_just_now = "error" not in response
1599+
15851600
if account_was_established_by_broker or broker_attempt_succeeded_just_now:
15861601
return self._process_broker_response(response, scopes, data)
15871602

msal/broker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def _signin_interactively(
215215
def _acquire_token_silently(
216216
authority, client_id, account_id, scopes, claims=None, correlation_id=None,
217217
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
@@ -224,6 +225,8 @@ def _acquire_token_silently(
224225
return
225226
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
226227
params.set_requested_scopes(scopes)
228+
if at_to_renew:
229+
params.set_access_token_to_renew(at_to_renew)
227230
if claims:
228231
params.set_decoded_claims(claims)
229232
if auth_scheme:

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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from tests import unittest
2+
import msal
3+
import sys
4+
5+
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"]
10+
_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
11+
pca = msal.PublicClientApplication(
12+
_AZURE_CLI,
13+
authority="https://login.microsoftonline.com/organizations",
14+
enable_broker_on_mac=True,
15+
enable_broker_on_windows=True,
16+
)
17+
18+
19+
class ForceRefreshTestCase(unittest.TestCase):
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+
)
27+
accounts = pca.get_accounts()
28+
self.assertNotEqual(
29+
[], accounts,
30+
"Interactive flow should have established a logged-in account")
31+
account = accounts[0]
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)