Skip to content

Commit c1a0ce1

Browse files
committed
Sort scopes before writing to token cache
1 parent 866ba2b commit c1a0ce1

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

msal/token_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def __add(self, event, now=None):
160160
decode_id_token(id_token, client_id=event["client_id"]) if id_token else {})
161161
client_info, home_account_id = self.__parse_account(response, id_token_claims)
162162

163-
target = ' '.join(event.get("scope") or []) # Per schema, we don't sort it
163+
target = ' '.join(sorted(event.get("scope") or [])) # Schema should have required sorting
164164

165165
with self._lock:
166166
now = int(time.time() if now is None else now)

tests/test_token_cache.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ def testAddByAad(self):
7676
'home_account_id': "uid.utid",
7777
'realm': 'contoso',
7878
'secret': 'an access token',
79-
'target': 's2 s1 s3',
79+
'target': 's1 s2 s3', # Sorted
8080
'token_type': 'some type',
8181
},
8282
self.cache._cache["AccessToken"].get(
83-
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s2 s1 s3')
83+
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s1 s2 s3')
8484
)
8585
self.assertEqual(
8686
{
@@ -90,10 +90,10 @@ def testAddByAad(self):
9090
'home_account_id': "uid.utid",
9191
'last_modification_time': '1000',
9292
'secret': 'a refresh token',
93-
'target': 's2 s1 s3',
93+
'target': 's1 s2 s3', # Sorted
9494
},
9595
self.cache._cache["RefreshToken"].get(
96-
'uid.utid-login.example.com-refreshtoken-my_client_id--s2 s1 s3')
96+
'uid.utid-login.example.com-refreshtoken-my_client_id--s1 s2 s3')
9797
)
9898
self.assertEqual(
9999
{
@@ -150,11 +150,11 @@ def testAddByAdfs(self):
150150
'home_account_id': "subject",
151151
'realm': 'adfs',
152152
'secret': 'an access token',
153-
'target': 's2 s1 s3',
153+
'target': 's1 s2 s3', # Sorted
154154
'token_type': 'some type',
155155
},
156156
self.cache._cache["AccessToken"].get(
157-
'subject-fs.msidlab8.com-accesstoken-my_client_id-adfs-s2 s1 s3')
157+
'subject-fs.msidlab8.com-accesstoken-my_client_id-adfs-s1 s2 s3')
158158
)
159159
self.assertEqual(
160160
{
@@ -164,10 +164,10 @@ def testAddByAdfs(self):
164164
'home_account_id': "subject",
165165
'last_modification_time': "1000",
166166
'secret': 'a refresh token',
167-
'target': 's2 s1 s3',
167+
'target': 's1 s2 s3', # Sorted
168168
},
169169
self.cache._cache["RefreshToken"].get(
170-
'subject-fs.msidlab8.com-refreshtoken-my_client_id--s2 s1 s3')
170+
'subject-fs.msidlab8.com-refreshtoken-my_client_id--s1 s2 s3')
171171
)
172172
self.assertEqual(
173173
{
@@ -214,7 +214,7 @@ def test_key_id_is_also_recorded(self):
214214
refresh_token="a refresh token"),
215215
}, now=1000)
216216
cached_key_id = self.cache._cache["AccessToken"].get(
217-
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s2 s1 s3',
217+
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s1 s2 s3',
218218
{}).get("key_id")
219219
self.assertEqual(my_key_id, cached_key_id, "AT should be bound to the key")
220220

@@ -229,7 +229,7 @@ def test_refresh_in_should_be_recorded_as_refresh_on(self): # Sounds weird. Yep
229229
), #refresh_token="a refresh token"),
230230
}, now=1000)
231231
refresh_on = self.cache._cache["AccessToken"].get(
232-
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s2 s1 s3',
232+
'uid.utid-login.example.com-accesstoken-my_client_id-contoso-s1 s2 s3',
233233
{}).get("refresh_on")
234234
self.assertEqual("2800", refresh_on, "Should save refresh_on")
235235

0 commit comments

Comments
 (0)