Cookbook: Cookie transport with CSRF¶
Use CookieTransport when you want the access token (or session reference) in an HttpOnly cookie instead of Authorization headers.
Requirements¶
- Instantiate
CookieTransportwith cookie names and attributes appropriate for your environment (secure=Truein production behind HTTPS). - Set
csrf_secretonLitestarAuthConfigso the plugin can build LitestarCSRFConfigand, when enabled, registerCSRFMiddleware. - For unsafe HTTP methods (POST, PUT, PATCH, DELETE), send the header configured by
csrf_header_name(defaultX-CSRF-Token) matching the CSRF cookie value.
Behavior¶
The plugin detects cookie transports during validation and wires CSRF when configured. Requests that mutate state without a valid CSRF pairing are rejected fail-closed.