Перейти к содержанию

AuthenticationMiddleware class

Warning

The current page still doesn't have a translation for this language.

But you can help translating it: Contributing.

This is the reference for the main object AuthenticationMiddleware that contains all the parameters, attributes and functions.

Use this class for authentication middleware in Ravyn.

ravyn.middleware.authentication.AuthenticationMiddleware

AuthenticationMiddleware(app, backend=None, on_error=None)

Bases: AuthenticationMiddleware

PARAMETER DESCRIPTION
app

The ASGI application callable wrapped by this middleware.

TYPE: ASGIApp

backend

One or more authentication backends used to authenticate the connection. If multiple backends are provided, they are tried in order.

TYPE: AuthenticationBackend | Sequence[AuthenticationBackend] | None DEFAULT: None

on_error

An optional error handler function called when authentication fails. It receives the Connection and AuthenticationError and must return an ASGI-compatible Response object.

TYPE: Callable[[Connection, AuthenticationError], Response] | None DEFAULT: None

Source code in ravyn/middleware/authentication.py
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
def __init__(
    self,
    app: Annotated[
        ASGIApp,
        Doc(
            """
            The ASGI application callable wrapped by this middleware.
            """
        ),
    ],
    backend: Annotated[
        AuthenticationBackend | Sequence[AuthenticationBackend] | None,
        Doc(
            """
            One or more authentication backends used to authenticate the connection.
            If multiple backends are provided, they are tried in order.
            """
        ),
    ] = None,
    on_error: Annotated[
        Callable[[Connection, AuthenticationError], Response] | None,
        Doc(
            """
            An optional error handler function called when authentication fails.
            It receives the Connection and AuthenticationError and must return an
            ASGI-compatible Response object.
            """
        ),
    ] = None,
) -> None:
    super().__init__(app=app, backend=backend, on_error=on_error)  # type: ignore[arg-type]

app instance-attribute

app = app

on_error instance-attribute

on_error = (
    on_error if on_error is not None else default_on_error
)

scopes instance-attribute

scopes = {HTTP, WEBSOCKET}

backend instance-attribute

backend

authenticate async

authenticate(conn, **kwargs)

Authorize users here.

Source code in lilya/middleware/authentication.py
124
125
126
127
128
129
130
131
132
async def authenticate(self, conn: Connection, **kwargs: Any) -> None | AuthResult:
    """Authorize users here."""

    for backend in self.backend:
        # exceptions are passed through to __call__ and there handled
        auth_result = await backend.authenticate(conn)
        if auth_result is not None:
            return auth_result
    return None

default_on_error staticmethod

default_on_error(connection, exc)
Source code in lilya/middleware/authentication.py
 99
100
101
@staticmethod
def default_on_error(connection: Connection, exc: Exception) -> Response:
    return PlainText(str(exc), status_code=status.HTTP_400_BAD_REQUEST)

ravyn.middleware.authentication.AuthResult

Bases: ArbitraryBaseModel

user instance-attribute

user

Arbitrary user coming from authentication and assignable to request.user.

model_config class-attribute instance-attribute

model_config = ConfigDict(arbitrary_types_allowed=True)
    - "!^model_config"