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.

PARAMETER DESCRIPTION
key

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

TYPE: str

tokens

The token set to persist.

TYPE: OAuthTokenSet

load abstractmethod async

load(key: str) -> OAuthTokenSet | None

Load tokens for the given key.

PARAMETER DESCRIPTION
key

Unique identifier.

TYPE: str

RETURNS 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.

PARAMETER DESCRIPTION
key

Unique identifier.

TYPE: str

exists abstractmethod async

exists(key: str) -> bool

Check if tokens exist for the given key.

PARAMETER DESCRIPTION
key

Unique identifier.

TYPE: str

RETURNS DESCRIPTION
bool

True if tokens are stored for this key.

list_keys abstractmethod async

list_keys() -> list[str]

List all stored token keys.

RETURNS 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]

PARAMETER DESCRIPTION
service_name

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

TYPE: str DEFAULT: '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.

PARAMETER DESCRIPTION
redis_url

Redis connection URL.

TYPE: str DEFAULT: 'redis://localhost:6379/0'

prefix

Key prefix for namespacing (default "pywry").

TYPE: str DEFAULT: 'pywry'

pool_size

Connection pool size (default 10).

TYPE: int DEFAULT: 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).

PARAMETER DESCRIPTION
backend

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

TYPE: str DEFAULT: 'memory'

**kwargs

Additional keyword arguments passed to the store constructor.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
TokenStore

A configured token store instance.