nolito.tokens module

class nolito.tokens.KeyringTokenStore(*, service, username, metadata_file)[source]

Bases: object

Create a token store using the Keyring

Parameters:
  • service (str) – The name of the keyring service

  • username (str) – The username for the keyring service

  • metadata_file (Path) – The path to the metadata file

clear()[source]

Clear the information stored in the keyring or the metadata file

Return type:

None

classmethod from_settings(settings)[source]

Create the token store based on Nolito settings

Parameters:

settings (NolitoSettings) – The Nolito settings

Return type:

KeyringTokenStore

load()[source]

Load a token set from keyring secrets and metadata

Return type:

TokenSet | None

Returns:

The token set based on the current store

save(tokens)[source]

Save the tokens to the keyring and metadata

Parameters:

tokens (TokenSet) – The token set to store

Return type:

None

class nolito.tokens.TokenSet(access_token, refresh_token, expires_at, token_type='Bearer', scope=None)[source]

Bases: object

Set of tokens used by the API

Parameters:
  • access_token (str) – The access token

  • refresh_token (str) – The refresh token

  • expires_at (int) – The expiration time as a UNIX timestamp (sec)

  • token_type (str) – The token type (defaults to "Bearer")

  • scope (str | None) – The token scope (defaults to None)

classmethod from_oauth_payload(payload, now=None)[source]

Extract the token set from an OAuth payload

Parameters:
  • payload (dict[str, Any]) – The OAuth payload as returned by a POST request to the token/ endpoint.

  • now (int | None) – Current time in seconds (defaults to time.time() when None).

Raises:

RuntimeError – Raised if no expires_in key is found.

Return type:

TokenSet

is_expired(leeway_seconds=60)[source]

Check if the token is expired or expires soon

Parameters:

leeway_seconds (int) – How soon is the token allowed to expire (in sec)

Return type:

bool

Returns:

True if the token is expired, False otherwise.