Skip to content

Commit 685b14b

Browse files
authored
Returning refresh token errors which were discarded (#186)
1 parent c235d4e commit 685b14b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

msal/application.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,16 +633,18 @@ def _acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
633633
**kwargs)
634634
if at and "error" not in at:
635635
return at
636+
last_resp = None
636637
if app_metadata.get("family_id"): # Meaning this app belongs to this family
637-
at = self._acquire_token_silent_by_finding_specific_refresh_token(
638+
last_resp = at = self._acquire_token_silent_by_finding_specific_refresh_token(
638639
authority, scopes, dict(query, family_id=app_metadata["family_id"]),
639640
**kwargs)
640641
if at and "error" not in at:
641642
return at
642643
# Either this app is an orphan, so we will naturally use its own RT;
643644
# or all attempts above have failed, so we fall back to non-foci behavior.
644645
return self._acquire_token_silent_by_finding_specific_refresh_token(
645-
authority, scopes, dict(query, client_id=self.client_id), **kwargs)
646+
authority, scopes, dict(query, client_id=self.client_id),
647+
**kwargs) or last_resp
646648

647649
def _get_app_metadata(self, environment):
648650
apps = self.token_cache.find( # Use find(), rather than token_cache.get(...)

tests/test_application.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ def tester(url, data=None, **kwargs):
176176

177177
# Will not test scenario of app leaving family. Per specs, it won't happen.
178178

179+
def test_preexisting_family_app_will_attempt_frt_and_return_error(self):
180+
error_response = '{"error": "invalid_grant", "error_description": "xyz"}'
181+
def tester(url, data=None, **kwargs):
182+
self.assertEqual(
183+
self.frt, data.get("refresh_token"), "Should attempt the FRT")
184+
return MinimalResponse(status_code=400, text=error_response)
185+
app = ClientApplication(
186+
"preexisting_family_app", authority=self.authority_url, token_cache=self.cache)
187+
resp = app._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
188+
self.authority, self.scopes, self.account, post=tester)
189+
logger.debug("%s.cache = %s", self.id(), self.cache.serialize())
190+
self.assertEqual(json.loads(error_response), resp, "Error raised will be returned")
191+
179192
def test_family_app_remove_account(self):
180193
logger.debug("%s.cache = %s", self.id(), self.cache.serialize())
181194
app = ClientApplication(

0 commit comments

Comments
 (0)