Skip to content

Login Helper

pyecotrend_ista.login_helper

Login helper for Keycloak.

Classes:

Functions:

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

Refresh the access token using the provided refresh token.

Parameters:

  • refresh_token
    (str) –

    The refresh token obtained from previous authentication.

Returns:

  • tuple[str, int, str]

    Tuple containing the refreshed access token, its expiration time in seconds, and the new refresh token.

get_token

get_token() -> GetTokenResponse

Retrieve access and refresh tokens using the obtained authorization code.

Raises:

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:

  • token
    (str) –

    The access token to be used for authorization.

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:

logout

logout(token) -> dict | Any | bytes | dict[str, str]

Log out the user session from the identity provider.

Parameters:

  • token
    (str) –

    Refresh token associated with the user session.

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:

  • bytes or dict

    Content of the response message.

Raises:

Notes

Source from https://github.com/marcospereirampj/python-keycloak/blob/c98189ca6951f12f1023ed3370c9aaa0d81e4aa4/src/keycloak/exceptions.py