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.3.0.tar.gz (809.1 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.3.0-py3-none-any.whl (369.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: litestar_security-0.3.0.tar.gz
  • Upload date:
  • Size: 809.1 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.3.0.tar.gz
Algorithm Hash digest
SHA256 916c03bcb5b4df743bd472fe2f7e7c67fc689c7256b808eb082a513ddd08c7f3
MD5 b2300a05bdf63d4a5e6a0875dcb56a30
BLAKE2b-256 6b25b7f35a2b8fcc4dc17951909cefe7a372b8251d6d03c6a59b3bba36621eb7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for litestar_security-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 580d6b2645a9edd6fe13d8127b18cbdcbdb84b8ca80a0eeeb821ae50cc51af75
MD5 9d7a2c6c809e5a0ec89dd9b425e225b6
BLAKE2b-256 06a7261a4383f240c9eee05ad74fc16709c34634147fc6345961e8a31a934641

See more details on using hashes here.

Provenance

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