Skip to content

Commit 84bdfab

Browse files
committed
Prevent crash on token_cache.find(..., query=None)
1 parent 5272fbd commit 84bdfab

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

msal/token_cache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def _find(self, credential_type, target=None, query=None): # O(n) generator
128128

129129
preferred_result = None
130130
if (credential_type == self.CredentialType.ACCESS_TOKEN
131+
and isinstance(query, dict)
131132
and "home_account_id" in query and "environment" in query
132133
and "client_id" in query and "realm" in query and target
133134
): # Special case for O(1) AT lookup

tests/test_token_cache.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ def testAddByAad(self):
6565
expires_in=3600, access_token="an access token",
6666
id_token=id_token, refresh_token="a refresh token"),
6767
}, now=1000)
68-
self.assertEqual(
69-
{
68+
access_token_entry = {
7069
'cached_at': "1000",
7170
'client_id': 'my_client_id',
7271
'credential_type': 'AccessToken',
@@ -78,10 +77,16 @@ def testAddByAad(self):
7877
'secret': 'an access token',
7978
'target': 's1 s2 s3', # Sorted
8079
'token_type': 'some type',
81-
},
80+
}
81+
self.assertEqual(
82+
access_token_entry,
8283
self.cache._cache["AccessToken"].get(
8384
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s1 s2 s3')
8485
)
86+
self.assertIn(
87+
access_token_entry,
88+
self.cache.find(self.cache.CredentialType.ACCESS_TOKEN),
89+
"find(..., query=None) should not crash, even though MSAL does not use it")
8590
self.assertEqual(
8691
{
8792
'client_id': 'my_client_id',

0 commit comments

Comments
 (0)