Skip to main content
Litestar Security

Storage-agnostic authentication, authorization, local account lifecycle, external identity providers, and WebSocket security for Litestar.

PyPI Version Python Versions CI Documentation License: MIT Discord

Getting Started · Authentication & Guards · Local Accounts · Providers · JWT & JWKS · WebSockets · API Reference


litestar-security connects local accounts, OAuth 2.0 and OIDC providers, Google Cloud IAP assertions, prefixed API keys, and workload JWTs to Litestar's ASGI middleware, dependency injection, guards, OpenAPI schema, and WebSocket lifecycle. Every persistence, cache, secret-custody, and rate-limit boundary is modeled as a narrow Protocol port with zero runtime ORM, database driver, or cache SDK dependencies.

Capability Matrix

Capability Primary Symbols Documentation
Plugin, Policy & Context SecurityPlugin, SecurityConfig, SecurityContext, Principal, CurrentUser, SecureController, PublicController, public, optional, required, exclude Authentication & Guards
Authorization Guards requires_authenticated, requires_role, requires_scope, requires_tenant, requires_tenant_role, requires_capability, requires_assurance, requires_all_of, requires_any_of Authentication & Guards
Local Accounts, MFA & Passkeys LocalAuth, LocalAuthConfig, Argon2PasswordHasher, MFAConfig, MFAService, PasskeyConfig, PyWebAuthnVerifier, AESGCMSecretProtector, StepUpService Local Accounts
OAuth 2.0, OIDC & Google IAP OAuthConfig, OIDCProvider, google_oidc_provider, keycloak_oidc_provider, GitHubOAuthProvider, OIDCDiscoveryClient, GoogleIAPConfig, GoogleIAPClaims External Providers
JWT Signing, JWKS & API Keys CompositeBearerConfig, ServiceTokenConfig, LocalKeyRing, CachedJWKSProvider, HttpxJWKSFetcher, APIKeyConfig, ProtectedResourceConfig JWT & JWKS
WebSocket Security WebSocketSecurityConfig, WebSocketConnectTokenIssuer, issue_websocket_connect_token, supervise_websocket_lifetime, WebSocketRevocationSource WebSocket Security
Hardening & Rate Limiting SecurityHeadersConfig, ContentSecurityPolicy, csp_nonce, StoreRateLimiter, forwarded_client_key, litestar security routes Hardening
Storage Ports & Test Kit InMemorySecurityBackend, FakeClock, StoreConformanceFactories, assert_security_backend_conformance Custom Stores & Testing

Installation

uv add litestar-security

Optional Extras

JWT and JWKS verification, OIDC token validation, Google IAP assertions, and API-key authentication ship in the core package. Install optional extras only for the local-account or OAuth capabilities a deployment uses:

Extra Install Command Purpose
argon2 uv add "litestar-security[argon2]" Argon2id password hashing via Argon2PasswordHasher
mfa uv add "litestar-security[mfa]" TOTP provisioning and login challenges (MFAService, MFALoginService), recovery codes, and step-up flows
passkeys uv add "litestar-security[passkeys]" WebAuthn passkey registration and assertion verification (PyWebAuthnVerifier)
oauth uv add "litestar-security[oauth]" OAuth 2.0 and OIDC lifecycle feature marker (zero extra third-party dependencies)

Extras compose cleanly in a single install command:

uv add "litestar-security[argon2,mfa,passkeys]"

Quickstart

Public routes must be explicit once an authentication mechanism is registered; unannotated routes compile to required() at startup and project their security schemes directly into OpenAPI:

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

from litestar_security import (
    Principal,
    SecurityConfig,
    SecurityContext,
    SecurityPlugin,
    public,
    required,
    requires_tenant_role,
)


@get("/health", auth=public(), sync_to_thread=False)
def health(security_context: NamedDependency[SecurityContext]) -> dict[str, bool]:
    """Report request authentication state on an explicitly public route."""
    return {"authenticated": security_context.evidence is not None}


@get(
    "/tenants/{tenant_id:str}",
    auth=required(),
    guards=[requires_tenant_role(tenant_parameter="tenant_id", roles={"owner"})],
)
async def tenant_settings(
    tenant_id: str,
    principal: NamedDependency[Principal[object]],
) -> dict[str, str]:
    """Enforce authentication and tenant-scoped owner membership."""
    return {"tenant_id": tenant_id, "principal_id": principal.id or ""}


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

Inspect the compiled policy, CSRF posture, and inherited guards for every registered route with the CLI plugin:

uv run litestar --app app:app security routes

Try the repository's runnable local session, token, hybrid, OAuth, IAP, and multi-tenant example applications:

LITESTAR_SECURITY_EXAMPLE=local-hybrid uv run litestar --app examples.app:create_app run

Documentation

Full guides and API reference are published at https://cofin.github.io/litestar-security/:

Contributing & License

Contributions are welcome; see the Contributing Guide for local setup, boundary gates, and backend conformance testing. litestar-security supports Python 3.10 through 3.14 and is released under the MIT License.

Metadata

Release files for litestar-security 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litestar-security 0.8.0
File Size Uploaded Provenance Actions
litestar_security-0.8.0.tar.gz 967.5 kB With provenance Details for litestar_security-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for litestar-security 0.8.0
File Interpreter ABI Platform Actions
litestar_security-0.8.0-py3-none-any.whl Python 3 none any Details for litestar_security-0.8.0-py3-none-any.whl

Total release size: 1.4 MB

Release files / litestar_security-0.8.0.tar.gz

Download URL litestar_security-0.8.0.tar.gz
Size 967.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3d6f61bd1eb6475192cc70f9eb241e51033e9af49dd8aed0e0db7e96fa495070
BLAKE2b-256 checksum
How to use checksums
9625f83aaa5db6fbaa0486a94cef208c9de9ff8192da3b9d7462c310f23ef6a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / litestar_security-0.8.0-py3-none-any.whl

Download URL litestar_security-0.8.0-py3-none-any.whl
Size 392.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c040217889fb045b88eeba59e0a03ceeb878317d4c7bd0c37492ac18f1f7bc7a
BLAKE2b-256 checksum
How to use checksums
aa14e88d8af70477b0417898d3ea710f34709f65de1be9469e53f519c85fb609
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for litestar-security 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for litestar_security-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page