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:
ABCInterface 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:
session –
requests.Sessioninstance 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:
session –
requests.Sessioninstance 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:
AuthenticatorAuthenticator using environment variables to fetch credentials.
- authenticate(session: Session, auth_url: str) Response¶
Retrieve an authentication response from a remote API.
- Parameters:
session –
requests.Sessioninstance 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 username and password could not be fetched from environment variables.
ParityOSAuthError – If login with provided credentials fails.
- class parityos.services.authentication.InteractiveAuth(username: str = '', login_attempts: int = 3, *, username_env_var: str = 'PARITYOS_USER', password_env_var: str = 'PARITYOS_PASS')¶
Bases:
AuthenticatorInteractive 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_varattribute, or as input via the prompt.- 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.usernameif not empty, taken from the environment variableself.username_env_varif set or the user is prompted.The password is taken from the environment variable
self.password_env_varif set or the user is prompted.If the first login attempt is unsuccessful, the user is prompted for credentials on all subsequent attempts.
- Parameters:
session –
requests.Sessioninstance 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.