Skip to content

Authentication

Base class for implementing the Authentication into SQLAdmin. You need to inherit this class and override the methods: login, logout and authenticate.

Source code in sqladmin/authentication.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
class AuthenticationBackend:
    """Base class for implementing the Authentication into SQLAdmin.
    You need to inherit this class and override the methods:
    `login`, `logout` and `authenticate`.
    """

    def __init__(self, secret_key: str) -> None:
        from starlette.middleware.sessions import SessionMiddleware

        self.middlewares = [
            Middleware(SessionMiddleware, secret_key=secret_key),
        ]

    async def login(self, request: Request) -> bool:
        """Implement login logic here.
        You can access the login form data `await request.form()`
        andvalidate the credentials.
        """
        raise NotImplementedError()

    async def logout(self, request: Request) -> Response | bool:
        """Implement logout logic here.
        This will usually clear the session with `request.session.clear()`.

        If a `Response` or `RedirectResponse` is returned,
        that response is returned to the user,
        otherwise the user will be redirected to the index page.
        """
        raise NotImplementedError()

    async def authenticate(self, request: Request) -> Response | bool:
        """Implement authenticate logic here.
        This method will be called for each incoming request
        to validate the authentication.

        If a `Response` or `RedirectResponse` is returned,
        that response is returned to the user,
        otherwise a True/False is expected.
        """
        raise NotImplementedError()

__init__(secret_key)

Source code in sqladmin/authentication.py
18
19
20
21
22
23
def __init__(self, secret_key: str) -> None:
    from starlette.middleware.sessions import SessionMiddleware

    self.middlewares = [
        Middleware(SessionMiddleware, secret_key=secret_key),
    ]

authenticate(request) async

Implement authenticate logic here. This method will be called for each incoming request to validate the authentication.

If a Response or RedirectResponse is returned, that response is returned to the user, otherwise a True/False is expected.

Source code in sqladmin/authentication.py
42
43
44
45
46
47
48
49
50
51
async def authenticate(self, request: Request) -> Response | bool:
    """Implement authenticate logic here.
    This method will be called for each incoming request
    to validate the authentication.

    If a `Response` or `RedirectResponse` is returned,
    that response is returned to the user,
    otherwise a True/False is expected.
    """
    raise NotImplementedError()

login(request) async

Implement login logic here. You can access the login form data await request.form() andvalidate the credentials.

Source code in sqladmin/authentication.py
25
26
27
28
29
30
async def login(self, request: Request) -> bool:
    """Implement login logic here.
    You can access the login form data `await request.form()`
    andvalidate the credentials.
    """
    raise NotImplementedError()

logout(request) async

Implement logout logic here. This will usually clear the session with request.session.clear().

If a Response or RedirectResponse is returned, that response is returned to the user, otherwise the user will be redirected to the index page.

Source code in sqladmin/authentication.py
32
33
34
35
36
37
38
39
40
async def logout(self, request: Request) -> Response | bool:
    """Implement logout logic here.
    This will usually clear the session with `request.session.clear()`.

    If a `Response` or `RedirectResponse` is returned,
    that response is returned to the user,
    otherwise the user will be redirected to the index page.
    """
    raise NotImplementedError()