Skip to content

BasicAuth

fastagency.ui.mesop.auth.basic_auth.BasicAuth #

BasicAuth(allowed_users: dict[str, str])

Initialize the authentication component with allowed users.

Args: allowed_users (dict[str, str]): A dictionary where the keys are usernames and the values are passwords.

Initializes: _self (AuthProtocol): Ensures the instance conforms to the AuthProtocol interface. allowed_users (dict[str, str]): A dictionary where the keys are usernames and the values are hashed passwords.

Source code in fastagency/ui/mesop/auth/basic_auth/basic_auth.py
def __init__(self, allowed_users: dict[str, str]) -> None:
    """Initialize the authentication component with allowed users.

    Args:
    allowed_users (dict[str, str]): A dictionary where the keys are usernames and the values are passwords.

    Initializes:
    _self (AuthProtocol): Ensures the instance conforms to the AuthProtocol interface.
    allowed_users (dict[str, str]): A dictionary where the keys are usernames and the values are hashed passwords.
    """
    # mypy check if self is AuthProtocol
    _self: AuthProtocol = self

    self.allowed_users = allowed_users

allowed_users instance-attribute #

allowed_users = allowed_users

auth_component #

auth_component() -> component
Source code in fastagency/ui/mesop/auth/basic_auth/basic_auth.py
def auth_component(self) -> me.component:
    styles = MesopHomePageStyles()
    state = me.state(State)
    if state.authenticated_user:
        with me.box(style=styles.logout_btn_container):
            basic_auth_component(
                on_auth_changed=self.on_auth_changed,
                authenticated_user=state.authenticated_user,
            )
    else:
        with me.box(style=styles.login_box):  # noqa: SIM117
            with me.box(style=styles.login_btn_container):
                basic_auth_component(
                    on_auth_changed=self.on_auth_changed,
                    authenticated_user=state.authenticated_user,
                )

create_security_policy #

create_security_policy(
    policy: SecurityPolicy,
) -> SecurityPolicy
Source code in fastagency/ui/mesop/auth/basic_auth/basic_auth.py
def create_security_policy(self, policy: me.SecurityPolicy) -> me.SecurityPolicy:
    return policy

is_authorized #

is_authorized(username: str, password: str) -> bool
Source code in fastagency/ui/mesop/auth/basic_auth/basic_auth.py
def is_authorized(self, username: str, password: str) -> bool:
    if username not in self.allowed_users:
        return False

    password_hash = self.allowed_users[username]
    return self._verify_password(password, password_hash)

on_auth_changed #

on_auth_changed(e: WebEvent) -> None
Source code in fastagency/ui/mesop/auth/basic_auth/basic_auth.py
def on_auth_changed(self, e: mel.WebEvent) -> None:
    state = me.state(State)

    if e.value is None:
        state.authenticated_user = ""
        return

    username, password = e.value["username"], e.value["password"]

    if not self.is_authorized(username, password):
        raise me.MesopUserException("Invalid username or password")

    state.authenticated_user = username