Skip to content

Commit

Permalink
Prevent crash on token_cache.find(..., query=None)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayluo committed Jan 9, 2024
1 parent 5272fbd commit 84bdfab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions msal/token_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def _find(self, credential_type, target=None, query=None): # O(n) generator

preferred_result = None
if (credential_type == self.CredentialType.ACCESS_TOKEN
and isinstance(query, dict)
and "home_account_id" in query and "environment" in query
and "client_id" in query and "realm" in query and target
): # Special case for O(1) AT lookup
Expand Down
11 changes: 8 additions & 3 deletions tests/test_token_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def testAddByAad(self):
expires_in=3600, access_token="an access token",
id_token=id_token, refresh_token="a refresh token"),
}, now=1000)
self.assertEqual(
{
access_token_entry = {
'cached_at': "1000",
'client_id': 'my_client_id',
'credential_type': 'AccessToken',
Expand All @@ -78,10 +77,16 @@ def testAddByAad(self):
'secret': 'an access token',
'target': 's1 s2 s3', # Sorted
'token_type': 'some type',
},
}
self.assertEqual(
access_token_entry,
self.cache._cache["AccessToken"].get(
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s1 s2 s3')
)
self.assertIn(
access_token_entry,
self.cache.find(self.cache.CredentialType.ACCESS_TOKEN),
"find(..., query=None) should not crash, even though MSAL does not use it")
self.assertEqual(
{
'client_id': 'my_client_id',
Expand Down

0 comments on commit 84bdfab

Please sign in to comment.