Login Helper¶
pyecotrend_ista.login_helper ¶
Login helper for Keycloak.
Classes:
-
LoginHelper–Login helper for Keycloak.
Functions:
-
raise_error_from_response–Raise an exception for the response.
LoginHelper ¶
LoginHelper(username: str, password: str, totp: str | None = None, session: Session | None = None, logger=None) Login helper for Keycloak.
Attributes:
-
session(Session) –Optional session object for making HTTP requests.
-
username(str) –Username for authentication.
-
password(str) –Password for authentication.
-
cookie(str) –Authentication cookie.
-
auth_code(str) –Authorization code.
-
form_action(str) –Form action URL for authentication.
Notes
This class provides utility methods for handling authentication and session management using Keycloak.
Parameters:
-
(username¶str) –Username for authentication.
-
(password¶str) –Password for authentication.
-
(totp¶str, default:None) –Time-based One-Time Password if enabled, by default None.
-
(session¶Session, default:None) –Optional session object for making HTTP requests, by default None.
-
(logger¶Logger, default:None) –Logger object for logging messages, by default None.
Methods:
-
refresh_token–Refresh the access token using the provided refresh token.
-
get_token–Retrieve access and refresh tokens using the obtained authorization code.
-
userinfo–Retrieve user information from the Keycloak provider.
-
logout–Log out the user session from the identity provider.
refresh_token ¶
refresh_token(refresh_token) -> tuple get_token ¶
get_token() -> GetTokenResponse Retrieve access and refresh tokens using the obtained authorization code.
Raises:
-
KeycloakPostError–If there's an error during the POST request to retrieve tokens.
-
KeycloakInvalidTokenError–If the response status code is not 200, indicating an invalid token.
Returns:
-
GetTokenResponse–A TypedDict containing authentication tokens including 'accessToken', 'accessTokenExpiresIn', and 'refreshToken'.
userinfo ¶
userinfo(token) -> Any Retrieve user information from the Keycloak provider.
This method sends a GET request to the Keycloak userinfo endpoint using the provided token in the Authorization header. It returns the JSON response containing user information.
Parameters:
Returns:
-
Any–A dictionary containing the user information if the request is successful, or an empty dictionary if the user is a demo user.
Raises:
-
KeycloakOperationError–If the request fails due to a Keycloak operation error.
logout ¶
logout(token) -> dict | Any | bytes | dict[str, str] Log out the user session from the identity provider.
Parameters:
Returns:
-
Union[dict, Any, bytes, dict[str, str]]–Response data from the logout request. The exact type may vary based on the response content.
Raises:
-
KeycloakPostError–If an error occurs during the POST request to logout the user.
raise_error_from_response ¶
raise_error_from_response(response: Response, error, expected_codes=None, skip_exists=False) -> dict | Any | bytes | dict[str, str] Raise an exception for the response.
Parameters:
-
(response¶Response) –The response object.
-
(error¶dict or Exception) –Error object to raise.
-
(expected_codes¶Sequence[int], default:None) –Set of expected codes, which should not raise the exception.
-
(skip_exists¶bool, default:False) –Indicates whether the response on already existing object should be ignored.
Returns:
Raises:
-
KeycloakError–In case of unexpected status codes.