oauth2.store — Storing and retrieving data

Store adapters to persist and retrieve data during the OAuth 2.0 process or for later use. This module provides base classes that can be extended to implement your own solution specific to your needs. It also includes implementations for popular storage systems like memcache.

Data types

class oauth2.AccessToken(client_id, grant_type, token, data={}, expires_at=None, refresh_token=None, scopes=[])[source]

An access token and associated data.

class oauth2.AuthorizationCode(client_id, code, expires_at, redirect_uri, scopes, data=None)[source]

Holds an authorization code and additional information.

class oauth2.Client(identifier, secret, redirect_uris=[])[source]

Representation of a client application.

Base classes

class oauth2.store.AccessTokenStore[source]

Base class for persisting an access token after it has been generated.

Used in two-legged and three-legged authentication flows.

fetch_by_refresh_token(refresh_token)[source]

Fetches an access token from the store using its refresh token to identify it.

Parameters:refresh_token – A string containing the refresh token.
save_token(access_token)[source]

Stores an access token and additional data.

Parameters:access_token – An instance of oauth2.AccessToken.
class oauth2.store.AuthCodeStore[source]

Base class for writing and retrieving an auth token during the Authorization Code Grant flow.

fetch_by_code(code)[source]

Returns an AuthorizationCode fetched from a storage.

Parameters:code – The authorization code.
Returns:An instance of oauth2.AuthorizationCode.
Raises :AuthCodeNotFound if no data could be retrieved for given code.
save_code(authorization_code)[source]

Stores the data belonging to an authorization code token.

Parameters:authorization_code – An instance of oauth2.AuthorizationCode.
class oauth2.store.ClientStore[source]

Base class for handling OAuth2 clients.

fetch_by_client_id(client_id)[source]

Retrieve a client by its identifier.

Parameters:client_id – Identifier of a client app.
Returns:An instance of oauth2.Client.
Raises :ClientNotFoundError

Concrete classes

class oauth2.store.LocalClientStore[source]

Bases: oauth2.store.ClientStore

Stores clients in memory.

add_client(client_id, client_secret, redirect_uris)[source]

Add a client app.

Parameters:
  • client_id – Identifier of the client app.
  • client_secret – Secret the client app uses for authentication against the OAuth 2.0 server.
  • redirect_uris – A list of URIs to redirect to.
fetch_by_client_id(client_id)[source]

Retrieve a client by its identifier.

Parameters:client_id – Identifier of a client app.
Returns:An instance of oauth2.Client.
Raises :ClientNotFoundError
class oauth2.store.LocalTokenStore[source]

Bases: oauth2.store.AccessTokenStore, oauth2.store.AuthCodeStore

Stores tokens in memory.

Useful for testing purposes or APIs with a very limited set of clients. Use memcache or redis as storage to be able to scale.

fetch_by_code(code)[source]

Returns an AuthorizationCode.

Parameters:code – The authorization code.
Returns:An instance of oauth2.AuthorizationCode.
Raises :AuthCodeNotFound if no data could be retrieved for given code.
fetch_by_refresh_token(refresh_token)[source]

Find an access token by its refresh token.

Parameters:refresh_token – The refresh token that was assigned to an AccessToken.
Returns:The oauth2.AccessToken.
Raises :oauth2.error.AccessTokenNotFound
fetch_by_token(token)[source]

Returns data associated with an access token or None if no data was found.

Useful for cases like validation where the access token needs to be read again.

Parameters:token – A access token code.
Returns:An instance of oauth2.AccessToken.
save_code(authorization_code)[source]

Stores the data belonging to an authorization code token.

Parameters:authorization_code – An instance of oauth2.AuthorizationCode.
save_token(access_token)[source]

Stores an access token and additional data in memory.

Parameters:client_id – An instance of oauth2.AccessToken.
class oauth2.store.MemcacheTokenStore(mc=None, prefix='oauth2', *args, **kwargs)[source]

Bases: oauth2.store.AccessTokenStore, oauth2.store.AuthCodeStore

Uses memcache to store access tokens and auth tokens.

This Store supports pylibmc and python-memcached. It tries to use pylibmc first and falls back to python-memcached. Arguments are passed to the underlying client implementation.

Initialization by passing an object:

# This example uses python-memcached
import memcache

# Somewhere in your application
mc = memcache.Client(servers=['127.0.0.1:11211'], debug=0)
# ...
token_store = MemcacheTokenStore(mc=mc)

Initialization using pylibmc:

token_store = MemcacheTokenStore(servers=["127.0.0.1"], binary=True,
                                 behaviors={"tcp_nodelay": True,
                                "ketama": True})

Initialization using python-memcached:

token_store = MemcacheTokenStore(servers=['127.0.0.1:11211'], debug=0)
fetch_by_code(code)[source]

Returns data belonging to an authorization code from memcache or None if no data was found.

See oauth2.store.AuthCodeStore.

save_code(authorization_code)[source]

Stores the data belonging to an authorization code token in memcache.

See oauth2.store.AuthCodeStore.

save_token(access_token)[source]

Stores the access token and additional data in memcache.

See oauth2.store.AccessTokenStore.

Read the Docs v: v0.4.0
Versions
latest
v0.4.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.