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.9.2

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.9.2
File Size Uploaded Provenance Actions
litestar_security-0.9.2.tar.gz 976.3 kB With provenance Details for litestar_security-0.9.2.tar.gz

Built distribution (wheel)

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

Total release size: 1.4 MB

Release files / litestar_security-0.9.2.tar.gz

Download URL litestar_security-0.9.2.tar.gz
Size 976.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d5d54db6b1bb2147852561d8909fe7b6d1b382fe8f04caa9d1a39809fdd842bf
BLAKE2b-256 checksum
How to use checksums
5ebb99aa062deca2c3f55cd29e5d816092ef95d2958507088904fca311673537
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 29, 2026.

Transparency log

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

Download URL litestar_security-0.9.2-py3-none-any.whl
Size 396.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36eed057d35fdf265cfaa1d48d37bf3522c0a18f79d83145d2421f2e2e90c967
BLAKE2b-256 checksum
How to use checksums
e1ee62ad87c7d18cf7997429c8e474c3be8b4950d35b768c725b3891a6e96441
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 29, 2026.

Transparency log

Security for litestar-security 0.9.2

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.9.2.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 29, 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