Skip to content

Commit 50da5f0

Browse files
committed
Adjusting token cache to work with ADFS2019
1 parent f4a0ba0 commit 50da5f0

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

msal/token_cache.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,25 @@ def add(self, event, now=None):
111111
event, indent=4, sort_keys=True,
112112
default=str, # A workaround when assertion is in bytes in Python 3
113113
))
114+
environment = realm = None
115+
if "token_endpoint" in event:
116+
_, environment, realm = canonicalize(event["token_endpoint"])
114117
response = event.get("response", {})
115118
access_token = response.get("access_token")
116119
refresh_token = response.get("refresh_token")
117120
id_token = response.get("id_token")
118-
client_info = {}
119-
home_account_id = None
120-
if "client_info" in response:
121-
client_info = json.loads(base64decode(response["client_info"]))
122-
home_account_id = "{uid}.{utid}".format(**client_info)
123-
environment = realm = None
124-
if "token_endpoint" in event:
125-
_, environment, realm = canonicalize(event["token_endpoint"])
121+
id_token_claims = (
122+
decode_id_token(id_token, client_id=event["client_id"])
123+
if id_token else {})
124+
client_info = (
125+
json.loads(base64decode(response["client_info"]))
126+
if "client_info" in response
127+
else { # ADFS scenario
128+
"uid": id_token_claims.get("sub"),
129+
"utid": environment, # TBD
130+
}
131+
)
132+
home_account_id = "{uid}.{utid}".format(**client_info)
126133
target = ' '.join(event.get("scope", [])) # Per schema, we don't sort it
127134

128135
with self._lock:
@@ -148,15 +155,15 @@ def add(self, event, now=None):
148155
self.modify(self.CredentialType.ACCESS_TOKEN, at, at)
149156

150157
if client_info:
151-
decoded_id_token = decode_id_token(
152-
id_token, client_id=event["client_id"]) if id_token else {}
153158
account = {
154159
"home_account_id": home_account_id,
155160
"environment": environment,
156161
"realm": realm,
157-
"local_account_id": decoded_id_token.get(
158-
"oid", decoded_id_token.get("sub")),
159-
"username": decoded_id_token.get("preferred_username"),
162+
"local_account_id": id_token_claims.get(
163+
"oid", id_token_claims.get("sub")),
164+
"username": id_token_claims.get("preferred_username") # AAD
165+
or id_token_claims.get("upn") # ADFS 2019
166+
or "", # The schema does not like null
160167
"authority_type":
161168
self.AuthorityType.ADFS if realm == "adfs"
162169
else self.AuthorityType.MSSTS,

0 commit comments

Comments
 (0)