acmadauth.tokencaches module

Token cache implementations for ACMAD Auth.

class acmadauth.tokencaches.JsonFileTokenCache(file_path)

Bases: TokenCache

A simple JSON file-based token cache for testing purposes only.

Danger

Do not use this cache in production environments! This implementation stores tokens in plain text in a local file. Consider using KeyringTokenCache for secure storage. If you need a secure file-based cache, KeyringTokenCache can be used with keyrings.cryptfile.

Parameters:

file_path (str) – Path to the JSON file to use for caching tokens.

clear()

Clear the token set from the cache.

Raises:

TokenCacheClearError – If there is an error clearing the token set.

Return type:

None

load()

Load the token set from the cache.

Return type:

TokenSet | None

Returns:

The loaded token set, or None if not found.

Raises:

TokenCacheLoadError – If there is an error loading the token set.

save(token_set)

Save the token set to the cache.

Parameters:

token_set (TokenSet) – The token set to save.

Raises:

TokenCacheSaveError – If there is an error saving the token set.

Return type:

None

class acmadauth.tokencaches.KeyringTokenCache(service_name, keyring_backend=None)

Bases: TokenCache

A secure token cache implementation using the system keyring. It uses the python-keyring library to store and retrieve tokens securely. This implementation is suitable for production use. By default, it uses the system’s default keyring backend, but a custom backend can be provided. See https://pypi.org/project/keyring/ for more details on keyring backends.

Parameters:
  • service_name (str) – The service name to use for storing tokens in the keyring (e.g., myapp:oidc:<issuer-host-or-hash>:<client_id>).

  • keyring_backend (Optional[keyring.backend.KeyringBackend]) – Optional custom keyring backend to use.

clear()

Clear the token set from the cache.

Raises:

TokenCacheClearError – If there is an error clearing the token set.

Return type:

None

load()

Load the token set from the cache.

Return type:

TokenSet | None

Returns:

The loaded token set, or None if not found.

Raises:

TokenCacheLoadError – If there is an error loading the token set.

save(token_set)

Save the token set to the cache.

Parameters:

token_set (TokenSet) – The token set to save.

Raises:

TokenCacheSaveError – If there is an error saving the token set.

Return type:

None

class acmadauth.tokencaches.MemoryTokenCache

Bases: TokenCache

An in-memory token cache. Tokens are lost when the application exits. Suitable for testing or short-lived applications.

clear()

Clear the token set from the cache.

Raises:

TokenCacheClearError – If there is an error clearing the token set.

Return type:

None

load()

Load the token set from the cache.

Return type:

TokenSet | None

Returns:

The loaded token set, or None if not found.

Raises:

TokenCacheLoadError – If there is an error loading the token set.

save(token_set)

Save the token set to the cache.

Parameters:

token_set (TokenSet) – The token set to save.

Raises:

TokenCacheSaveError – If there is an error saving the token set.

Return type:

None

class acmadauth.tokencaches.TokenCache

Bases: ABC

abstractmethod clear()

Clear the token set from the cache.

Raises:

TokenCacheClearError – If there is an error clearing the token set.

Return type:

None

abstractmethod load()

Load the token set from the cache.

Return type:

TokenSet | None

Returns:

The loaded token set, or None if not found.

Raises:

TokenCacheLoadError – If there is an error loading the token set.

abstractmethod save(token_set)

Save the token set to the cache.

Parameters:

token_set (TokenSet) – The token set to save.

Raises:

TokenCacheSaveError – If there is an error saving the token set.

Return type:

None