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.1.0.tar.gz (580.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.1.0-py3-none-any.whl (270.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: litestar_security-0.1.0.tar.gz
  • Upload date:
  • Size: 580.8 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.1.0.tar.gz
Algorithm Hash digest
SHA256 da2f1ccc03f1ee4b85f2860cd468930d2ebad6285205cf3b381120057303c4f7
MD5 aeaeb141c49bdb06112fa00b6faca2d9
BLAKE2b-256 0dd8211d418ddd12b132f9cc3655a5f960c1845ecb021eccb2d6aad72de1e0f9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for litestar_security-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 baf678c3292aa56adc32882643f35a570d5c78cd2dfbc58948d225bd578a96ad
MD5 6663dee225ed4292d3c46e3658e22c18
BLAKE2b-256 a21d862027494b69629d7206d0895f14da127c3e5b6e9bc008060eeae9a8dea1

See more details on using hashes here.

Provenance

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