snip.token.storage.keyring_store

Functionalities to save and retrieve tokens from a keyring.

A token always consists of a name, a book_id and a token string. The name is a user defined string to identify the token and should be unique.

We abuse the standard keyring functionalities a bit to store key value pairs. We use a global prefix in combination with the name to store the key value pairs for each token.

kr.set_password(SNIP_KR_IDENTIFIER, f"{name.as_hex}:book_id", book_id)
kr.set_password(SNIP_KR_IDENTIFIER, f"{name.as_hex}:token", token_str)

As we also want to retrieve all tokens from the keyring we need to store an index of all tokens in the keyring.

kr.set_password(SNIP_KR_IDENTIFIER, f"index", f"{name.as_hex},{name2.as_hex},...")

Functions

get_all_tokens(kr)

Get all available tokens from a keyring backend.

get_token(name, kr)

Get a token from the keyring storage.

remove_token(name, kr)

Remove a token from the keyring storage.

save_token(token, kr[, overwrite])

Save a token in the keyring storage.

token_exists(name, kr)

Check if a token exists in the keyring storage.

Module Functions

snip.token.storage.keyring_store.get_all_tokens(kr: KeyringBackend) list[Token]

Get all available tokens from a keyring backend.

Abstracts the parsing of the key value pairs from the keyring backend and returns valid tokens.

Parameters:

kr (KeyringBackend) – The keyring backend to retrieve the tokens from.

Returns:

A list of all valid found in the keyring.

Return type:

list[Token]

snip.token.storage.keyring_store.get_token(name: str, kr: KeyringBackend) Token | None

Get a token from the keyring storage.

Parameters:
  • name (str) – The name of the token.

  • kr (KeyringBackend) – The keyring backend to retrieve the token from.

Returns:

The token object or None if not found.

Return type:

Token | None

snip.token.storage.keyring_store.remove_token(name: str, kr: KeyringBackend)

Remove a token from the keyring storage.

Parameters:
  • name (str) – The name of the token to remove.

  • kr (KeyringBackend) – The keyring backend to remove the token from.

snip.token.storage.keyring_store.save_token(token: Token, kr: KeyringBackend, overwrite: bool = False)

Save a token in the keyring storage.

Parameters:
  • token (Token) – The token object to save.

  • kr (KeyringBackend) – The keyring backend to save the token to.

snip.token.storage.keyring_store.token_exists(name: str, kr: KeyringBackend) bool

Check if a token exists in the keyring storage.

Parameters:
  • name (str) – The name of the token to check.

  • kr (KeyringBackend) – The keyring backend to check the token in.

Returns:

True if the token exists, False otherwise.

Return type:

bool