Skip to content

Commit 3afa44a

Browse files
committed
Add more test behavior to validate id token behaviors
1 parent 6519ff2 commit 3afa44a

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

msal/token_cache.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@
22
import threading
33
import time
44
import logging
5-
import base64
65

76
from .authority import canonicalize
7+
from .oauth2cli.oidc import base64decode, decode_id_token
88

99

1010
logger = logging.getLogger(__name__)
1111

1212
def is_subdict_of(small, big):
1313
return dict(big, **small) == big
1414

15-
def base64decode(raw): # This can handle a padding-less raw input
16-
raw += '=' * (-len(raw) % 4) # https://stackoverflow.com/a/32517907/728675
17-
return base64.b64decode(raw).decode("utf-8")
18-
1915

2016
class TokenCache(object):
2117
"""This is considered as a base class containing minimal cache behavior.
@@ -112,8 +108,8 @@ def add(self, event, now=None):
112108
}
113109

114110
if client_info:
115-
decoded_id_token = json.loads(
116-
base64decode(id_token.split('.')[1])) if id_token else {}
111+
decoded_id_token = decode_id_token(
112+
id_token, client_id=event["client_id"]) if id_token else {}
117113
key = self._build_account_key(home_account_id, environment, realm)
118114
self._cache.setdefault(self.CredentialType.ACCOUNT, {})[key] = {
119115
"home_account_id": home_account_id,

tests/test_application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def setUp(self):
181181
"token_endpoint": "{}/oauth2/v2.0/token".format(self.authority_url),
182182
"response": TokenCacheTestCase.build_response(
183183
access_token="Siblings won't share AT. test_remove_account() will.",
184-
id_token=TokenCacheTestCase.build_id_token(),
184+
id_token=TokenCacheTestCase.build_id_token(aud=self.preexisting_family_app_id),
185185
uid=self.uid, utid=self.utid, refresh_token=self.frt, foci="1"),
186186
}) # The add(...) helper populates correct home_account_id for future searching
187187

tests/test_token_cache.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import base64
33
import json
4+
import time
45

56
from msal.token_cache import *
67
from tests import unittest
@@ -13,12 +14,17 @@
1314
class TokenCacheTestCase(unittest.TestCase):
1415

1516
@staticmethod
16-
def build_id_token(sub="sub", oid="oid", preferred_username="me", **kwargs):
17+
def build_id_token(
18+
iss="issuer", sub="subject", aud="my_client_id", exp=None, iat=None,
19+
preferred_username="me", **claims):
1720
return "header.%s.signature" % base64.b64encode(json.dumps(dict({
21+
"iss": iss,
1822
"sub": sub,
19-
"oid": oid,
23+
"aud": aud,
24+
"exp": exp or (time.time() + 100),
25+
"iat": iat or time.time(),
2026
"preferred_username": preferred_username,
21-
}, **kwargs)).encode()).decode('utf-8')
27+
}, **claims)).encode()).decode('utf-8')
2228

2329
@staticmethod
2430
def build_response( # simulate a response from AAD
@@ -54,9 +60,11 @@ def setUp(self):
5460
self.cache = TokenCache()
5561

5662
def testAdd(self):
57-
id_token = self.build_id_token(oid="object1234", preferred_username="John Doe")
63+
client_id = "my_client_id"
64+
id_token = self.build_id_token(
65+
oid="object1234", preferred_username="John Doe", aud=client_id)
5866
self.cache.add({
59-
"client_id": "my_client_id",
67+
"client_id": client_id,
6068
"scope": ["s2", "s1", "s3"], # Not in particular order
6169
"token_endpoint": "https://login.example.com/contoso/v2/token",
6270
"response": self.build_response(

0 commit comments

Comments
 (0)