Globus SDK TokenStorage

A Globus SDK contrib module providing simple token storage.

Basic Usage

Install with pip install globus-sdk-tokenstorage

You can then import helpers from globus_sdk_tokenstorage. For example:

import os
import globus_sdk
from globus_sdk_tokenstorage import SimpleJSONFileAdapter

my_file_adapter = SimpleJSONFileAdapter(
    os.path.expanduser('~/mytokens.json'),
    resource_server='transfer.api.globus.org')

if not my_file_adapter.file_exists():
    # ... do a login low, getting back initial tokens
    # elided for simplicity here
    token_response = ...
    # now store the tokens, and pull out the tokens for the
    # resource server we want
    my_file_adapter.store(token_response)
    by_rs = token_response.by_resource_server
    tokens = by_rs['transfer.api.globus.org']
else:
    # otherwise, we already did this whole song-and-dance, so just
    # load the tokens from that file
    tokens = my_file_adapter.read_as_dict()


# RereshTokenAuthorizer and ClientCredentialsAuthorizer both use
# `on_refresh` callbacks
# this feature is therefore only relevant for those auth types
#
# auth_client is the internal auth client used for refreshes,
# and which was used in the login flow
# note that this is all normal SDK usage wherein my_file_adapter
# is providing the on_refresh callback
auth_client = ...
authorizer = globus_sdk.RefreshTokenAuthorizer(
    tokens['refresh_token'], auth_client,
    tokens['access_token'], tokens['access_token_expires'],
    on_refresh=my_file_adapter.on_refresh)

# or, for client credentials
authorizer = globus_sdk.ClientCredentialsAuthorizer(
    auth_client, ['urn:globus:auth:transfer.api.globus.org:all'],
    on_refresh=m_file_adapter.on_refresh)

# and then use as normal, tada!
tc = globus_sdk.TransferClient(authorizer=authorizer)

Full Library Contents

class globus_sdk_tokenstorage.abstract_base.AbstractStorageAdapter

Bases: object

on_refresh(token_response)

By default, the on_refresh handler for a token storage adapter simply stores the token response.

class globus_sdk_tokenstorage.file_adapters.AbstractFileAdapter

Bases: globus_sdk_tokenstorage.abstract_base.AbstractStorageAdapter

File adapters are for single-user cases, where we can assume that there’s a simple file-per-user and users are only ever attempting to read their own files.

file_exists()

Check if the file used by this file storage adapter exists.

class globus_sdk_tokenstorage.SimpleJSONFileAdapter(filename, resource_server=None, scopes=None)

Bases: globus_sdk_tokenstorage.file_adapters.AbstractFileAdapter

Parameters:
  • filename – the name of the file to write to and read from
  • resource_server – the resource server name for tokens to look up in a token response object
  • scopes – a list of scope names for tokens to look up in a token response object

A storage adapter for storing tokens in JSON files. Callers must provide exactly one of resource_server and scopes

store(token_response)

By default, self.on_refresh is just an alias for this function.

Given a token response, extract the token data for the configured scopes or resource servers of this file adapter and write it to self.filename as JSON data. Additionally will write the version of globus_sdk_tokenstorage which was in use.

Under the assumption that this may be running on a system with multiple local users, this sets the umask such that only the owner of the resulting file can read or write it.

read_as_dict()

Load the config file contents as JSON and return the resulting dict object.

Although the whole token response is passed in for self.store, this will only return the token data for the particular scopes or resource server for which this File Adapter is configured.