Skip to content

Commit 93c4c79

Browse files
authored
Merge pull request #262 from AzureAD/dont-add-account-when-silent
Do not add account when performing acquire_token_silent()
2 parents ffc6590 + 0d5719d commit 93c4c79

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

msal/application.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,11 @@ def _acquire_token_silent_by_finding_specific_refresh_token(
752752
response = client.obtain_token_by_refresh_token(
753753
entry, rt_getter=lambda token_item: token_item["secret"],
754754
on_removing_rt=rt_remover or self.token_cache.remove_rt,
755+
on_obtaining_tokens=lambda event: self.token_cache.add(dict(
756+
event,
757+
environment=authority.instance,
758+
skip_account_creation=True, # To honor a concurrent remove_account()
759+
)),
755760
scope=scopes,
756761
headers={
757762
CLIENT_REQUEST_ID: correlation_id or _get_new_correlation_id(),

msal/oauth2cli/oauth2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ def __init__(self,
462462
def _obtain_token(
463463
self, grant_type, params=None, data=None,
464464
also_save_rt=False,
465+
on_obtaining_tokens=None,
465466
*args, **kwargs):
466467
_data = data.copy() # to prevent side effect
467468
resp = super(Client, self)._obtain_token(
@@ -481,7 +482,7 @@ def _obtain_token(
481482
# but our obtain_token_by_authorization_code(...) encourages
482483
# app developer to still explicitly provide a scope here.
483484
scope = _data.get("scope")
484-
self.on_obtaining_tokens({
485+
(on_obtaining_tokens or self.on_obtaining_tokens)({
485486
"client_id": self.client_id,
486487
"scope": scope,
487488
"token_endpoint": self.configuration["token_endpoint"],
@@ -495,6 +496,7 @@ def obtain_token_by_refresh_token(self, token_item, scope=None,
495496
rt_getter=lambda token_item: token_item["refresh_token"],
496497
on_removing_rt=None,
497498
on_updating_rt=None,
499+
on_obtaining_tokens=None,
498500
**kwargs):
499501
# type: (Union[str, dict], Union[str, list, set, tuple], Callable) -> dict
500502
"""This is an overload which will trigger token storage callbacks.

msal/token_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def __add(self, event, now=None):
172172
at["key_id"] = data.get("key_id")
173173
self.modify(self.CredentialType.ACCESS_TOKEN, at, at)
174174

175-
if client_info:
175+
if client_info and not event.get("skip_account_creation"):
176176
account = {
177177
"home_account_id": home_account_id,
178178
"environment": environment,

0 commit comments

Comments
 (0)