authentication

Tools for token authentication with remote APIs.

class parityos.services.authentication.Authenticator(*, username_env_var: str = 'PARITYOS_USER', password_env_var: str = 'PARITYOS_PASS')

Bases: ABC

Interface for authenticators handling token based authentication with remote APIs.

username_env_var: str

Name of environment variable storing the username. Keyword only. Defaults to “PARITYOS_USER”.

password_env_var: str

Name of environment variable storing the password. Keyword only. Defaults to “PARITYOS_PASS”.

get_authentication_token(session: Session, auth_url: str) str

Retrieve an authentication header token from a remote API.

Parameters:
  • sessionrequests.Session instance to use to send the authentication request.

  • auth_url – Fully qualified url to the authentication endpoint of the remote API.

Returns:

Authentication token as a string used as header token in all subsequent requests if login is successful.

Raises:

ParityOSAuthError – If authentication is unsuccessful.

abstractmethod authenticate(session: Session, auth_url: str) Response

Retrieve an authentication response from a remote API.

Parameters:
  • sessionrequests.Session instance to use to send authentication request.

  • auth_url – Fully qualified url to authentication endpoint of remote API.

Returns:

A response from the authentication request if the login is successful. Raises an error otherwise.

class parityos.services.authentication.EnvVarAuth(*, username_env_var: str = 'PARITYOS_USER', password_env_var: str = 'PARITYOS_PASS')

Bases: Authenticator

Authenticator using environment variables to fetch credentials.

authenticate(session: Session, auth_url: str) Response

Retrieve an authentication response from a remote API.

Parameters:
  • sessionrequests.Session instance to use to send authentication request.

  • auth_url – Fully qualified url to authentication endpoint of remote API.

Returns:

A response from the authentication request if the login is successful. Raises an error otherwise.

Raises:
class parityos.services.authentication.InteractiveAuth(username: str = '', login_attempts: int = 3, *, username_env_var: str = 'PARITYOS_USER', password_env_var: str = 'PARITYOS_PASS')

Bases: Authenticator

Interactive Authenticator that prompts the user on the command line if necessary.

For security reasons, the password cannot be passed as argument. It has to be provided either through an environment variable set by the self.password_env_var attribute, or as input via the prompt.

username: str

Username to try at first login attempt. Optional.

login_attempts: int

Number of retry attempts in case of failed authentication. Defaults to 3.

authenticate(session: Session, auth_url: str) Response

Retrieve an authentication response from a remote API.

On the first login attempt, the username is either taken from self.username if not empty, taken from the environment variable self.username_env_var if set or the user is prompted.

The password is taken from the environment variable self.password_env_var if set or the user is prompted.

If the first login attempt is unsuccessful, the user is prompted for credentials on all subsequent attempts.

Parameters:
  • sessionrequests.Session instance to use to send authentication request.

  • auth_url – Fully qualified url to authentication endpoint of remote API.

Returns:

A response from the authentication request if the login is successful. Raises an error otherwise.

Raises:

ParityOSAuthError – If all login attempts have failed or in case of any other error during the authentication request.