Skip to content

pywry.auth.token_store

Pluggable token storage backends for OAuth2 tokens.


TokenStore (ABC)

pywry.auth.token_store.TokenStore

Bases: ABC

Abstract base class for OAuth2 token storage.

All methods are async to support both local and network-backed stores.

Functions

save abstractmethod async

save(key: str, tokens: OAuthTokenSet) -> None

Save tokens under the given key.

Parameters:

Name Type Description Default
key str

Unique identifier (e.g., user ID or session key).

required
tokens OAuthTokenSet

The token set to persist.

required

load abstractmethod async

load(key: str) -> OAuthTokenSet | None

Load tokens for the given key.

Parameters:

Name Type Description Default
key str

Unique identifier.

required

Returns:

Type Description
OAuthTokenSet or None

The stored token set, or None if not found.

delete abstractmethod async

delete(key: str) -> None

Delete tokens for the given key.

Parameters:

Name Type Description Default
key str

Unique identifier.

required

exists abstractmethod async

exists(key: str) -> bool

Check if tokens exist for the given key.

Parameters:

Name Type Description Default
key str

Unique identifier.

required

Returns:

Type Description
bool

True if tokens are stored for this key.

list_keys abstractmethod async

list_keys() -> list[str]

List all stored token keys.

Returns:

Type Description
list[str]

All keys with stored tokens.


MemoryTokenStore

pywry.auth.token_store.MemoryTokenStore

MemoryTokenStore()

Bases: TokenStore

In-memory token store for development and single-process use.

Thread-safe via asyncio.Lock, same pattern as MemorySessionStore.

Initialize the memory token store.

Functions

save async

save(key: str, tokens: OAuthTokenSet) -> None

Save tokens in memory.

load async

load(key: str) -> OAuthTokenSet | None

Load tokens from memory.

delete async

delete(key: str) -> None

Delete tokens from memory.

exists async

exists(key: str) -> bool

Check if tokens exist in memory.

list_keys async

list_keys() -> list[str]

List all token keys in memory.


KeyringTokenStore

pywry.auth.token_store.KeyringTokenStore

KeyringTokenStore(service_name: str = 'pywry-oauth')

Bases: TokenStore

OS keyring-backed token store for persistent native credentials.

Requires the keyring package: pip install pywry[auth]

Parameters:

Name Type Description Default
service_name str

Service name for keyring storage (default "pywry-oauth").

'pywry-oauth'

Initialize the keyring token store.

Functions

save async

save(key: str, tokens: OAuthTokenSet) -> None

Save tokens to the OS keyring.

load async

load(key: str) -> OAuthTokenSet | None

Load tokens from the OS keyring.

delete async

delete(key: str) -> None

Delete tokens from the OS keyring.

exists async

exists(key: str) -> bool

Check if tokens exist in the OS keyring.

list_keys async

list_keys() -> list[str]

List keys (not supported by all keyring backends).

Returns an empty list as the keyring API does not provide a standard way to enumerate entries.


RedisTokenStore

pywry.auth.token_store.RedisTokenStore

RedisTokenStore(redis_url: str = 'redis://localhost:6379/0', prefix: str = 'pywry', pool_size: int = 10)

Bases: TokenStore

Redis-backed token store for multi-worker deployments.

Uses Redis hash keys with optional TTL based on token expires_in.

Parameters:

Name Type Description Default
redis_url str

Redis connection URL.

'redis://localhost:6379/0'
prefix str

Key prefix for namespacing (default "pywry").

'pywry'
pool_size int

Connection pool size (default 10).

10

Initialize the Redis token store.

Functions

save async

save(key: str, tokens: OAuthTokenSet) -> None

Save tokens to Redis with optional TTL.

load async

load(key: str) -> OAuthTokenSet | None

Load tokens from Redis.

delete async

delete(key: str) -> None

Delete tokens from Redis.

exists async

exists(key: str) -> bool

Check if tokens exist in Redis.

list_keys async

list_keys() -> list[str]

List all token keys in Redis.


Factory

pywry.auth.token_store.get_token_store

get_token_store(backend: str = 'memory', **kwargs: Any) -> TokenStore

Factory function for token stores.

Returns a singleton instance. Call reset_token_store() to clear the cached instance (e.g. in tests).

Parameters:

Name Type Description Default
backend str

Storage backend: "memory", "keyring", or "redis".

'memory'
**kwargs Any

Additional keyword arguments passed to the store constructor.

{}

Returns:

Type Description
TokenStore

A configured token store instance.