Storage-agnostic authentication, authorization, local account lifecycle, external identity providers, and WebSocket security for Litestar.
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/:
- Getting Started — Installation, optional extras, session vs. token vs. hybrid local profiles, and runnable examples.
- Authentication & Guards — Compile-time route plans,
SecureControllerandPublicController, authorization snapshots, and composablerequires_*guards. - Local Accounts, MFA & Passkeys — Account registration, password policy, rotating refresh families, TOTP, recovery codes, WebAuthn passkeys, step-up grants, and generated
/authroutes. - External Identity Providers — OAuth 2.0 and OIDC flows, Google Cloud IAP workload/human assertions, prefixed API keys, JWT & JWKS, and OAuth 2.1 resource-server metadata.
- WebSocket Security — Policy-bound connect tokens, strict
Originchecks, and live connection expiry/revocation supervision. - Operations & Extending — Plugin composition, rate limiting, CSRF and CSP hardening, and custom storage conformance testing.
- API Reference — Complete typed public API reference and Changelog.
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.7.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| litestar_security-0.7.2.tar.gz | 952.3 kB | With provenance | Details for litestar_security-0.7.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| litestar_security-0.7.2-py3-none-any.whl | Python 3 | none | any | Details for litestar_security-0.7.2-py3-none-any.whl |
Total release size: 1.3 MB
Release files / litestar_security-0.7.2.tar.gz
| Download URL | litestar_security-0.7.2.tar.gz |
|---|---|
| Size | 952.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f78b03f446e9c0790d9b56c1656a9e053f889094004dff550223723616cdcd6f
|
|
BLAKE2b-256 checksum How to use checksums |
59a42e8d5c85d7d00386da217c231647f1440f4437e68fa59c150906e19a2523
|
| 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 12, 2026.
Transparency logRelease files / litestar_security-0.7.2-py3-none-any.whl
| Download URL | litestar_security-0.7.2-py3-none-any.whl |
|---|---|
| Size | 388.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
48d36299173f39aa218eca67cc4f8f608f33331728318ff7d56f7ba5fabc66b1
|
|
BLAKE2b-256 checksum How to use checksums |
eddc6e7bea8c15769a6ac536d6e5fe59d2ea8e1fb59dd2aabfaaa26bf2372611
|
| 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 12, 2026.
Transparency logSecurity for litestar-security 0.7.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.
Attestation for litestar_security-0.7.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 12, 2026.
Transparency logA 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.