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 requires_team_role, required


@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.4.0.tar.gz (818.8 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.4.0-py3-none-any.whl (376.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for litestar_security-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c2946efad092bae9b651f87230db80f696bfc7442044922cfaa728d28a516717
MD5 94dad8074f76331b2ef1eeb3b9d7caf8
BLAKE2b-256 28be768118f5c6eba1b94c2de59db5d5c54219d2c9f84ce5f65f32416ea01ca8

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_security-0.4.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.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_security-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b9e49ab67e86f0c590fac1e6d277b7b7195f9427e522ae2155b5eb1e0fc1c11a
MD5 08da4013be2d5a37811d4cde007cc91f
BLAKE2b-256 291bf577ba9e3ecdccd9bfe9c786cd5b6022779722aa60f2f62377c970d066dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_security-0.4.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