Skip to content

Auth

AdminAuth

Bases: AuthenticationBackend

Source code in hestia/extensions/sqladmin/auth.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class AdminAuth(AuthenticationBackend):
    async def login(self, request: Request) -> bool:
        """
        Performs the login logic for authentication.
        :param request: The incoming request.
        :return: True if the login is successful, False otherwise.
        """
        form = await request.form()

        username, password = form["username"], form["password"]

        # Validate username/password credentials
        if not _is_user_valid(username, password):
            return False

        # And update session
        request.session.update({"token": "..."})

        return True

    async def logout(self, request: Request) -> bool:
        """
        Performs the logout logic.
        :param request: The incoming request.
        :return: True if the logout is successful, False otherwise.
        """
        # Usually, you'd want to just clear the session
        request.session.clear()
        return True

    async def authenticate(self, request: Request) -> Optional[RedirectResponse]:
        """
        Authenticates the request.
        :param request: The incoming request.
        :return: Optional RedirectResponse if authentication fails, None otherwise.
        """
        token = request.session.get("token")

        if not token:
            return RedirectResponse(request.url_for("admin:login"), status_code=302)
        return None

authenticate(request) async

Authenticates the request.

Parameters:

Name Type Description Default
request Request

The incoming request.

required

Returns:

Type Description
Optional[RedirectResponse]

Optional RedirectResponse if authentication fails, None otherwise.

Source code in hestia/extensions/sqladmin/auth.py
57
58
59
60
61
62
63
64
65
66
67
async def authenticate(self, request: Request) -> Optional[RedirectResponse]:
    """
    Authenticates the request.
    :param request: The incoming request.
    :return: Optional RedirectResponse if authentication fails, None otherwise.
    """
    token = request.session.get("token")

    if not token:
        return RedirectResponse(request.url_for("admin:login"), status_code=302)
    return None

login(request) async

Performs the login logic for authentication.

Parameters:

Name Type Description Default
request Request

The incoming request.

required

Returns:

Type Description
bool

True if the login is successful, False otherwise.

Source code in hestia/extensions/sqladmin/auth.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
async def login(self, request: Request) -> bool:
    """
    Performs the login logic for authentication.
    :param request: The incoming request.
    :return: True if the login is successful, False otherwise.
    """
    form = await request.form()

    username, password = form["username"], form["password"]

    # Validate username/password credentials
    if not _is_user_valid(username, password):
        return False

    # And update session
    request.session.update({"token": "..."})

    return True

logout(request) async

Performs the logout logic.

Parameters:

Name Type Description Default
request Request

The incoming request.

required

Returns:

Type Description
bool

True if the logout is successful, False otherwise.

Source code in hestia/extensions/sqladmin/auth.py
47
48
49
50
51
52
53
54
55
async def logout(self, request: Request) -> bool:
    """
    Performs the logout logic.
    :param request: The incoming request.
    :return: True if the logout is successful, False otherwise.
    """
    # Usually, you'd want to just clear the session
    request.session.clear()
    return True