Skip to content

Commit

Permalink
Fixed refreshes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgolec committed Apr 8, 2024
1 parent d5a3a55 commit 311fe2d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions schwab/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def _register_token_redactions(token):
register_redactions(token)


def client_from_token_file(token_path, api_key, asyncio=False, enforce_enums=True):
def client_from_token_file(token_path, api_key, app_secret, asyncio=False,
enforce_enums=True):
'''
Returns a session from an existing token file. The session will perform
an auth refresh as needed. It will also update the token on disk whenever
Expand All @@ -96,7 +97,7 @@ def client_from_token_file(token_path, api_key, asyncio=False, enforce_enums=Tru
load = __token_loader(token_path)

return client_from_access_functions(
api_key, load, __update_token(token_path), asyncio=asyncio,
api_key, app_secret, load, __update_token(token_path), asyncio=asyncio,
enforce_enums=enforce_enums)


Expand Down Expand Up @@ -137,9 +138,9 @@ async def oauth_client_update_token(t, *args, **kwargs):
# Return a new session configured to refresh credentials
return client_class(
api_key,
session_class(api_key, token=token,
auto_refresh_url=TOKEN_ENDPOINT,
auto_refresh_kwargs={'client_id': api_key},
session_class(api_key,
client_secret=app_secret,
token=token,
update_token=oauth_client_update_token),
token_metadata=metadata_manager, enforce_enums=enforce_enums)

Expand Down Expand Up @@ -167,7 +168,7 @@ def __init__(self, creation_timestamp, unwrapped_token_write_func=None):
self.unwrapped_token_write_func = unwrapped_token_write_func

@classmethod
def from_loaded_token(cls, token, unwrapped_token_write_func=None):
def from_loaded_token(cls, token, app_secret, unwrapped_token_write_func=None):
'''
Returns a new ``TokenMetadata`` object extracted from the metadata of
the loaded token object. If the token has a legacy format which contains
Expand Down Expand Up @@ -264,6 +265,7 @@ def ensure_refresh_token_update(
session_class = session.__class__
return session_class(
api_key,
client_secret=app_secret,
token=new_token,
token_endpoint=TOKEN_ENDPOINT,
update_token=token_write_func)
Expand Down Expand Up @@ -485,7 +487,7 @@ def easy_client(api_key, app_secret, callback_url, token_path,
sys.exit(1)


def client_from_access_functions(api_key, token_read_func,
def client_from_access_functions(api_key, app_secret, token_read_func,
token_write_func, asyncio=False,
enforce_enums=True):
'''
Expand Down Expand Up @@ -525,7 +527,8 @@ def client_from_access_functions(api_key, token_read_func,
token = token_read_func()

# Extract metadata and unpack the token, if necessary
metadata = TokenMetadata.from_loaded_token(token, token_write_func)
metadata = TokenMetadata.from_loaded_token(
token, app_secret, token_write_func)
if TokenMetadata.is_metadata_aware_token(token):
token = token['token']

Expand All @@ -550,7 +553,9 @@ async def oauth_client_update_token(t, *args, **kwargs):
return client_class(
api_key,
session_class(api_key,
token=token,
token_endpoint=TOKEN_ENDPOINT,
update_token=oauth_client_update_token),
token_metadata=metadata, enforce_enums=enforce_enums)
client_secret=app_secret,
token=token,
token_endpoint=TOKEN_ENDPOINT,
update_token=oauth_client_update_token),
token_metadata=metadata,
enforce_enums=enforce_enums)

0 comments on commit 311fe2d

Please sign in to comment.