About litestar-authentication
A skill for designing authentication in Litestar applications. It guides developers in selecting built-in backends or custom middleware, separating identity checks from authorization, and validating protected and public routes.
Key features
- 01Choosing an authentication backend
- 02App-level authentication setup
- 03Explicit public-route exclusions
- 04Typed authenticated request context
- 05Authentication boundary test checklist
Use cases
- 01Selecting session or JWT authentication
- 02Adding authentication to Litestar routes
- 03Reviewing public-route exclusions
- 04Testing login and logout behavior
Install
Copy the command and run it in a terminal.
$
npx skills add https:/ / github.com/ alti3/ litestar-skills --skill litestar-authenticationAfter installing, type/litestar-authenticationin Claude Code to confirm it loaded.
Bundled filesagents/openai.yamlreferences/auth-patterns.md
Details
From the project READMEAuthentication
Execution Workflow
- Choose the identity mechanism first: session, JWT header, JWT cookie, OAuth2 password bearer, or custom middleware based on client and trust boundaries.
- Attach authentication once at app scope with
on_app_initor middleware. - Configure exclusion rules explicitly for login, schema, health, and other public routes.
- Use
request.userandrequest.authonly after authentication has been established. - Apply guards separately for authorization checks.
- Keep missing or invalid credential behavior consistent with the exception-handling strategy.
Core Rules
- Keep authentication separate from request parsing and authorization.
- Let request parsing validate input fields before auth-specific business logic where possible.
- Prefer built-in security backends before custom middleware.
- Use custom middleware only when the identity source does not fit a built-in backend.
- Treat
request.userandrequest.authas authenticated context, not parsing shortcuts. - Keep exclusion rules narrow and reviewable.
- Normalize identity failures to
401behavior unless the project has a documented alternative.
Decision Guide
- Use
SessionAuthfor browser-first applications with server-managed sessions. - Use
JWTAuthfor token-in-header API clients. - Use
JWTCookieAuthfor browser flows where token cookies are intentional. - Use
OAuth2PasswordBearerAuthwhen password-flow token issuance semantics are required. - Use
AbstractAuthenticationMiddlewarewhen the credential source or validation logic is custom. - Use
litestar-securitywhen the task includes authorization policy, secrets, and defense-in-depth concerns beyond identity wiring.
Reference Files
Read only the sections you need:
- For custom middleware, built-in backends, request auth context, exclusion rules, and login patterns, read references/auth-patterns.md.
Recommended Defaults
- Keep authentication middleware or backend registration at app scope.
- Exclude only explicitly public routes.
- Access authenticated context through typed
RequestorASGIConnectionafter auth runs. - Keep token issuance and logout flows explicit and testable.
- Hand off
401/403response formatting tolitestar-exception-handling.
Anti-Patterns
- Parsing auth headers manually in every route handler.
- Mixing authorization policy into authentication middleware.
- Treating
request.useras available on routes excluded from authentication. - Using broad path exclusions that create accidental public surface area.
- Hardcoding secrets or sample tokens outside local examples.
Validation Checklist
- Confirm protected endpoints reject missing or invalid credentials.
- Confirm public routes are public only when explicitly excluded.
- Confirm
request.userandrequest.authhave the expected types on authenticated routes. - Confirm login and logout flows produce the intended token or session behavior.
- Confirm identity failures line up with the exception-handling contract.
- Confirm tests cover both authenticated and unauthenticated paths.
Cross-Skill Handoffs
- Use
litestar-requestsfor header, cookie, and body parsing concerns that are not identity-specific. - Use
litestar-securityfor authorization, secret hygiene, and broader security posture. - Use
litestar-exception-handlingto standardize401and403contracts. - Use
litestar-testingfor auth boundary, exclusion-rule, and token-flow tests.
Litestar References
- https://docs.litestar.dev/latest/usage/security/abstract-authentication-middleware.html
- https://docs.litestar.dev/latest/usage/security/security-backends.html
- https://docs.litestar.dev/latest/usage/security/jwt.html
- https://docs.litestar.dev/latest/usage/security/excluding-and-including-endpoints.html
Comments
Markdown supported