Skip to main content

Litestar Security

Authentication and authorization for Litestar applications.

Litestar Security connects authentication providers to Litestar's middleware, dependency injection, guards, OpenAPI schema, and WebSocket lifecycle. Use local sessions or tokens, OAuth and OIDC, Google IAP, API keys, or workload JWTs without tying your application to a database library.

Quickstart

Install the package:

pip install litestar-security

Create app.py:

from litestar import Litestar, get
from litestar.di import NamedDependency

from litestar_security import (
    SecurityConfig,
    SecurityContext,
    SecurityPlugin,
    public,
)


@get("/", auth=public(), sync_to_thread=False)
def index(security_context: NamedDependency[SecurityContext]) -> dict[str, bool]:
    return {"authenticated": bool(security_context.evidence)}


app = Litestar(route_handlers=[index], plugins=[SecurityPlugin(SecurityConfig())])

Run the application:

litestar --app app:app run

Public routes must be explicit. Once an authentication provider and authorization resolver are configured, routes are protected by default and guards can enforce application permissions:

from litestar import get

from litestar_security import required, requires_team_role


@get(
    "/teams/{team_id:str}",
    auth=required(),
    guards=[requires_team_role(team_parameter="team_id", roles={"owner"})],
)
async def team_settings(team_id: str) -> dict[str, str]:
    return {"team_id": team_id}

Next steps

Litestar Security supports Python 3.10 through 3.14 and is licensed under MIT.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

litestar_security-0.2.0.tar.gz (704.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

litestar_security-0.2.0-py3-none-any.whl (338.9 kB view details)

Uploaded Python 3

File details

Details for the file litestar_security-0.2.0.tar.gz.

File metadata

  • Download URL: litestar_security-0.2.0.tar.gz
  • Upload date:
  • Size: 704.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for litestar_security-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f795e9e0d2161594020a97479dfc08e750ac9ce1e38600c6b0f5caa988f4e4cc
MD5 b8e460d83e202335e67fb29796ed415b
BLAKE2b-256 fbc4c0641bd82f4dfc96ed85dae9d390313971d2ce49dc11fdac46d66208ce55

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_security-0.2.0.tar.gz:

Publisher: publish.yml on cofin/litestar-security

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file litestar_security-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_security-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be99b46eb5a2949121f413744a9e11e5a29dc7d0a5df0cafe0b63ea0ce30bdf1
MD5 772dd56dc5d54152961dca36657965bf
BLAKE2b-256 106228334fc2b993c50e603f4254c8e347bb21cd26446d12bbbadf8700fc5402

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_security-0.2.0-py3-none-any.whl:

Publisher: publish.yml on cofin/litestar-security

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page