diff --git a/sumstats_service/resources/globus.py b/sumstats_service/resources/globus.py index a7c4f92..5cdfde5 100644 --- a/sumstats_service/resources/globus.py +++ b/sumstats_service/resources/globus.py @@ -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 @@ -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 @@ -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'