Skip to content

Client.obtain_token_by_refresh_token fails when token_item is a str #297

Description

@jiasli

Describe the bug
Even though Client.obtain_token_by_refresh_token checks the type of token_item at

rt_getter(token_item)
if not isinstance(token_item, string_types) else token_item,

and calls rt_getter accordingly, it doesn't check the type of token_item at

if on_updating_rt is not False and RT in resp:
(on_updating_rt or self.on_updating_rt)(token_item, resp[RT])

to make sure token_item is converted to a dict before calling self.on_updating_rt.

This causes update_rt to fail at L248:

def update_rt(self, rt_item, new_rt):
assert rt_item.get("credential_type") == self.CredentialType.REFRESH_TOKEN

To Reproduce
Pass a refresh token in str as token_item to Client.obtain_token_by_refresh_token.

Expected behavior
Either:

  • Client.obtain_token_by_refresh_token should succeed with token_item as a str either by
    • converting token_item to a dict before calling on_updating_rt
    • forcing on_updating_rt=False
  • Client.obtain_token_by_refresh_token should reject token_item as a str

What you see instead

  File "D:\cli\microsoft-authentication-library-for-python\msal\application.py", line 714, in acquire_token_silent
    result = self.acquire_token_silent_with_error(
  File "D:\cli\microsoft-authentication-library-for-python\msal\application.py", line 767, in acquire_token_silent_with_error
    result = self._acquire_token_silent_from_cache_and_possibly_refresh_it(
  File "D:\cli\microsoft-authentication-library-for-python\msal\application.py", line 840, in _acquire_token_silent_from_cache_and_possibly_refresh_it
    return self._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
  File "d:\cli\azure-cli\src\azure-cli-core\azure\cli\core\_msal.py", line 17, in _acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family
    return self._acquire_token_silent_by_finding_specific_refresh_token(
  File "d:\cli\azure-cli\src\azure-cli-core\azure\cli\core\_msal.py", line 28, in _acquire_token_silent_by_finding_specific_refresh_token
    response = client.obtain_token_by_refresh_token(
  File "D:\cli\microsoft-authentication-library-for-python\msal\oauth2cli\oauth2.py", line 810, in obtain_token_by_refresh_token
    (on_updating_rt or self.on_updating_rt)(token_item, resp[RT])
  File "D:\cli\microsoft-authentication-library-for-python\msal\token_cache.py", line 248, in update_rt
    assert rt_item.get("credential_type") == self.CredentialType.REFRESH_TOKEN
AttributeError: 'str' object has no attribute 'get'

The MSAL Python version you are using
1.8.0

Additional context
This issue is revealed by Azure/azure-cli#16672 (comment).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions