Token Manager#

Summary

The TokenManager handles the management of access tokens, ensuring API authorization is successful.

class TokenManager(config: ConfigManager)[source]#

Manages the Bearer Token required for accessing the Jamf API.

The TokenManager class handles all operations related to the token lifecycle, including fetching, saving, and validating the access token. It is initialized with a ConfigManager instance, which provides the necessary configuration and credentials.

Initializes the TokenManager with a provided ConfigManager instance.

Parameters:

config (ConfigManager) – A ConfigManager instance for managing credentials and configurations.

Raises:

ValueError – If the JamfClient configuration is invalid.

save_token(token: AccessToken)[source]#

Saves the access token and its expiration date securely.

This method stores the access token and its expiration date in the keyring for later retrieval. It also updates the JamfClient instance with the new token.

Parameters:

token (AccessToken) – The AccessToken instance containing the token and its expiration date.

token_valid() bool[source]#

Determines if the current access token is still valid.

This method checks if the token has expired by evaluating its expiration date.

Returns:

True if the token is valid (not expired), otherwise False.

Return type:

bool

get_credentials() Tuple[source]#

Retrieves the client ID and client secret for authentication.

These credentials are required for generating a new access token.

Returns:

A tuple containing the client ID and client secret.

Return type:

Tuple[AnyStr, AnyStr]

update_token(token_str: AnyStr, expires_in: int)[source]#

Updates the current access token with a new value and expiration time.

This method is typically called after receiving a new token from the API.

Parameters:
  • token_str (AnyStr) – The new token string.

  • expires_in (int) – The number of seconds until the token expires.

check_token_lifetime(client: JamfClient | None = None) bool[source]#

Evaluates the remaining lifetime of the access token.

This method checks if the token’s remaining lifetime is sufficient. If the lifetime is less than 5 minutes, a warning is issued.

Parameters:

client (Optional[JamfClient]) – The JamfClient instance to check the token for. If None, defaults to the current instance.

Returns:

True if the token’s remaining lifetime is more than 5 minutes, otherwise False.

Return type:

bool

async fetch_token() AccessToken | None[source]#

Asynchronously fetches a new access token from the Jamf API.

This method sends a request to the Jamf API to obtain a new token. The token is then saved and returned for use.

Returns:

The fetched AccessToken instance, or None if the request fails.

Return type:

Optional[AccessToken]