Skip to content

Refreshing a token whose scope is a string sends the scope split into single characters #252

Backlog

Description

@hackermengzhi

Which version of the SDK are you using?

15.5.0 (master at f1c9043)

Summary

The fix for #68 made can_refresh_access_token() accept scope as a str:

https://github.com/XeroAPI/xero-python/blob/f1c9043/xero_python/api_client/oauth2.py#L216

but TokenApi.refresh_token() still builds the request with " ".join(scope):

https://github.com/XeroAPI/xero-python/blob/f1c9043/xero_python/api_client/oauth2.py#L31

When scope is a string, " ".join() iterates over its characters. The case #68 was meant to fix now gets past the check and sends this to the token endpoint:

scope sent: 'o p e n i d   o f f l i n e _ a c c e s s   a c c o u n t i n g . t r a n s a c t i o n s'

Steps to reproduce

from xero_python.api_client.oauth2 import TokenApi

class FakeApiClient:
    def call_api(self, url, method, post_params=None, **kw):
        print(repr(post_params["scope"])); raise SystemExit

TokenApi(FakeApiClient(), "id", "secret").refresh_token(
    "refresh-token", "openid offline_access accounting.transactions")

The same happens through api_client.refresh_oauth2_token() when the token returned by oauth2_token_getter has a space-delimited scope. That is the format Xero returns, and what libraries such as Authlib store.

What you expected would happen

scope=openid offline_access accounting.transactions

Suggested fix

Normalise in one place, e.g. in OAuth2Token.update_token():

if isinstance(scope, str):
    scope = scope.split()

or in refresh_token():

"scope": scope if isinstance(scope, str) else " ".join(scope),

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

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions