Skip to content

Commit 389042f

Browse files
committed
Broker did not output token_type, we have to DIY
1 parent f79debd commit 389042f

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

msal/application.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,7 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
12891289
except RuntimeError: # TODO: TBD
12901290
logger.debug("Broker is unavailable on this platform. Fallback to non-broker.")
12911291
else:
1292+
data = kwargs.get("data", {})
12921293
response = _acquire_token_silently(
12931294
"https://{}/{}".format(self.authority.instance, self.authority.tenant),
12941295
self.client_id,
@@ -1297,10 +1298,9 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
12971298
claims=_merge_claims_challenge_and_capabilities(
12981299
self._client_capabilities, claims_challenge),
12991300
correlation_id=correlation_id,
1300-
)
1301-
if response: # The broker provided a decisive outcome for this account
1302-
return self._process_broker_response( # Then we use it
1303-
response, scopes, kwargs.get("data", {}))
1301+
**data)
1302+
if response: # The broker provided a decisive outcome, so we use it
1303+
return self._process_broker_response(response, scopes, data)
13041304

13051305
result = _clean_up(self._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
13061306
authority, self._decorate_scope(scopes), account,

msal/broker.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _read_account_by_id(account_id, correlation_id):
8080
or None) # None happens when the account was not created by broker
8181

8282

83-
def _convert_result(result, client_id): # Mimic an on-the-wire response from AAD
83+
def _convert_result(result, client_id, expected_token_type=None): # Mimic an on-the-wire response from AAD
8484
error = result.get_error()
8585
if error:
8686
return _convert_error(error, client_id)
@@ -95,8 +95,11 @@ def _convert_result(result, client_id): # Mimic an on-the-wire response from AA
9595
"id_token_claims": id_token_claims,
9696
"client_info": account.get_client_info(),
9797
"_account_id": account.get_account_id(),
98-
"token_type": "Bearer", # Hardcoded, for now. It is unavailable from broker.
98+
"token_type": expected_token_type or "Bearer", # Workaround its absence from broker
9999
}.items() if v}
100+
likely_a_cert = return_value["access_token"].startswith("AAAA") # Empirical observation
101+
if return_value["token_type"].lower() == "ssh-cert" and not likely_a_cert:
102+
logger.warn("Looks like we could not get an SSH Cert")
100103
granted_scopes = result.get_granted_scopes() # New in pymsalruntime 0.3.x
101104
if granted_scopes:
102105
return_value["scope"] = " ".join(granted_scopes) # Mimic the on-the-wire data format
@@ -130,7 +133,8 @@ def _signin_silently(
130133
correlation_id or _get_new_correlation_id(),
131134
lambda result, callback_data=callback_data: callback_data.complete(result))
132135
callback_data.signal.wait()
133-
return _convert_result(callback_data.result, client_id)
136+
return _convert_result(
137+
callback_data.result, client_id, expected_token_type=kwargs.get("token_type"))
134138

135139

136140
def _signin_interactively(
@@ -173,11 +177,13 @@ def _signin_interactively(
173177
login_hint, # None value will be accepted since pymsalruntime 0.3+
174178
lambda result, callback_data=callback_data: callback_data.complete(result))
175179
callback_data.signal.wait()
176-
return _convert_result(callback_data.result, client_id)
180+
return _convert_result(
181+
callback_data.result, client_id, expected_token_type=kwargs.get("token_type"))
177182

178183

179184
def _acquire_token_silently(
180-
authority, client_id, account_id, scopes, claims=None, correlation_id=None):
185+
authority, client_id, account_id, scopes, claims=None, correlation_id=None,
186+
**kwargs):
181187
correlation_id = correlation_id or _get_new_correlation_id()
182188
account = _read_account_by_id(account_id, correlation_id)
183189
if isinstance(account, pymsalruntime.MSALRuntimeError):
@@ -188,14 +194,18 @@ def _acquire_token_silently(
188194
params.set_requested_scopes(scopes)
189195
if claims:
190196
params.set_decoded_claims(claims)
197+
for k, v in kwargs.items(): # This can be used to support domain_hint, max_age, etc.
198+
if v is not None:
199+
params.set_additional_parameter(k, str(v))
191200
callback_data = _CallbackData()
192201
pymsalruntime.acquire_token_silently(
193202
params,
194203
correlation_id,
195204
account,
196205
lambda result, callback_data=callback_data: callback_data.complete(result))
197206
callback_data.signal.wait()
198-
return _convert_result(callback_data.result, client_id)
207+
return _convert_result(
208+
callback_data.result, client_id, expected_token_type=kwargs.get("token_type"))
199209

200210

201211
def _acquire_token_interactively(

0 commit comments

Comments
 (0)