authentication#

class starlite.middleware.authentication.AuthenticationResult#

Bases: BaseModel

Pydantic model for authentication data.

user: Any#

The user model, this can be any value corresponding to a user of the API.

auth: Any#

The auth value, this can for example be a JWT token.

class starlite.middleware.authentication.AbstractAuthenticationMiddleware#

Bases: ABC

Abstract AuthenticationMiddleware that allows users to create their own AuthenticationMiddleware by extending it and overriding AbstractAuthenticationMiddleware.authenticate_request().

__init__(app: ASGIApp, exclude: Optional[Union[str, List[str]]] = None, exclude_from_auth_key: str = 'exclude_from_auth', scopes: Optional[Scopes] = None) None#

Initialize AbstractAuthenticationMiddleware.

Parameters:
  • app – An ASGIApp, this value is the next ASGI handler to call in the middleware stack.

  • exclude – A pattern or list of patterns to skip in the authentication middleware.

  • exclude_from_auth_key – An identifier to use on routes to disable authentication for a particular route.

  • scopes – ASGI scopes processed by the authentication middleware.

async __call__(scope: Scope, receive: Receive, send: Send) None#

ASGI callable.

Parameters:
  • scope – The ASGI connection scope.

  • receive – The ASGI receive function.

  • send – The ASGI send function.

Returns:

None

abstract async authenticate_request(connection: ASGIConnection) AuthenticationResult#

Receive the http connection and return an AuthenticationResult.

Notes

  • This method must be overridden by subclasses.

Parameters:

connection – An ASGIConnection instance.

Raises:

NotAuthorizedException | PermissionDeniedException – if authentication fails.

Returns:

An instance of AuthenticationResult.