Skip to content

Commit

Permalink
Merge pull request #242 from EBISPOT/refresh_token_fix
Browse files Browse the repository at this point in the history
refresh token authorizer
  • Loading branch information
jdhayhurst authored May 15, 2023
2 parents ad512fd + 5a2d668 commit 483ca37
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions sumstats_service/resources/globus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pathlib
from sumstats_service import config
import globus_sdk
from globus_sdk import (NativeAppAuthClient, TransferClient, AccessTokenAuthorizer,
from globus_sdk import (NativeAppAuthClient, TransferClient, AccessTokenAuthorizer, RefreshTokenAuthorizer,
ClientCredentialsAuthorizer, ConfidentialAppAuthClient, DeleteData,
GCSClient, scopes, GuestCollectionDocument, TransferAPIError, GlobusAPIError)
from sumstats_service.resources.globus_utils import is_remote_session
Expand Down Expand Up @@ -54,9 +54,14 @@ def init_transfer_client() -> TransferClient:
# if we need to get tokens, start the Native App authentication process
tokens = do_native_app_authentication(config.TRANSFER_CLIENT_ID, config.REDIRECT_URI, config.SCOPES)
save_tokens_to_db(tokens)
transfer_access_token = tokens['transfer.api.globus.org']['access_token']
transfer_authorizer = AccessTokenAuthorizer(transfer_access_token)
transfer_client = TransferClient(authorizer=transfer_authorizer)
transfer_tokens = tokens.get('transfer.api.globus.org')
native_app_client = NativeAppAuthClient(client_id=config.TRANSFER_CLIENT_ID)
authorizer = RefreshTokenAuthorizer(transfer_tokens['refresh_token'],
native_app_client,
access_token=transfer_tokens['access_token'],
expires_at=transfer_tokens['expires_at_seconds'],
on_refresh=save_tokens_to_db(tokens))
transfer_client = TransferClient(authorizer=authorizer)
transfer_client.endpoint_autoactivate(config.MAPPED_COLLECTION_ID)
return transfer_client

Expand Down Expand Up @@ -230,14 +235,6 @@ def save_tokens_to_db(tokens) -> None:
globus_db_collection.insert(tokens, check_keys=False)


def update_tokens_file_on_refresh(token_response):
"""
Callback function passed into the RefreshTokenAuthorizer.
Will be invoked any time a new access token is fetched.
"""
save_tokens_to_db(token_response.by_resource_server)


def save_requirements_to_db(requirements):
mongo_client = MongoClient(config.MONGO_URI, username=config.MONGO_USER, password=config.MONGO_PASSWORD)
globus_db = mongo_client[config.MONGO_DB] # 'globus-tokens'
Expand Down

0 comments on commit 483ca37

Please sign in to comment.