Skip to content

Commit a6dbbff

Browse files
committed
WIP: Accurately remove ATs owned by sibling apps
1 parent f76f3c3 commit a6dbbff

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

msal/application.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,37 @@ def _get_authority_aliases(self, instance):
280280
return [alias for alias in group if alias != instance]
281281
return []
282282

283+
def sign_out(self, account):
284+
"""Remove all relevant RTs and ATs from token cache"""
285+
owned_by_account = {
286+
"environment": account["environment"],
287+
"home_account_id": (account or {}).get("home_account_id"),}
288+
289+
owned_by_account_and_app = dict(owned_by_account, client=self.client_id)
290+
for rt in self.token_cache.find( # Remove RTs
291+
TokenCache.CredentialType.REFRESH_TOKEN,
292+
query=owned_by_account_and_app):
293+
self.token_cache.remove_rt(rt)
294+
for at in self.token_cache.find( # Remove ATs
295+
TokenCache.CredentialType.ACCESS_TOKEN,
296+
query=owned_by_account_and_app): # regardless of realm
297+
self.token_cache.remove_at(at) # TODO
298+
299+
app_metadata = self._get_app_metadata(account["environment"])
300+
if app_metadata.get("family_id"): # Now let's settle family business
301+
for rt in self.token_cache.find( # Remove FRTs
302+
TokenCache.CredentialType.REFRESH_TOKEN, query=dict(
303+
owned_by_account,
304+
family_id=app_metadata["family_id"])):
305+
self.token_cache.remove_rt(rt)
306+
for sibling_app in self.token_cache.find( # Remove siblings' ATs
307+
TokenCache.CredentialType.APP_METADATA,
308+
query={"family_id": app_metadata.get["family_id"]}):
309+
for at in self.token_cache.find( # Remove ATs, regardless of realm
310+
TokenCache.CredentialType.ACCESS_TOKEN, query=dict(
311+
owned_by_account, client_id=sibling_app["client_id"])):
312+
self.token_cache.remove_at(at) # TODO
313+
283314
def acquire_token_silent(
284315
self,
285316
scopes, # type: List[str]
@@ -364,10 +395,7 @@ def _acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
364395
"home_account_id": (account or {}).get("home_account_id"),
365396
# "realm": authority.tenant, # AAD RTs are tenant-independent
366397
}
367-
apps = self.token_cache.find( # Use find(), rather than token_cache.get(...)
368-
TokenCache.CredentialType.APP_METADATA, query={
369-
"environment": authority.instance, "client_id": self.client_id})
370-
app_metadata = apps[0] if apps else {}
398+
app_metadata = self._get_app_metadata(authority.instance)
371399
if not app_metadata: # Meaning this app is now used for the first time.
372400
# When/if we have a way to directly detect current app's family,
373401
# we'll rewrite this block, to support multiple families.
@@ -396,6 +424,12 @@ def _acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
396424
return self._acquire_token_silent_by_finding_specific_refresh_token(
397425
authority, scopes, dict(query, client_id=self.client_id), **kwargs)
398426

427+
def _get_app_metadata(self, environment):
428+
apps = self.token_cache.find( # Use find(), rather than token_cache.get(...)
429+
TokenCache.CredentialType.APP_METADATA, query={
430+
"environment": environment, "client_id": self.client_id})
431+
return apps[0] if apps else {}
432+
399433
def _acquire_token_silent_by_finding_specific_refresh_token(
400434
self, authority, scopes, query,
401435
rt_remover=None, break_condition=lambda response: False, **kwargs):

0 commit comments

Comments
 (0)