- Cookie sessions for human users in browsers. A signed JWT sits in an
httpOnlycookie. The session record lives in your database so you can revoke it instantly. - JWT sessions for SPAs, mobile apps, and server-to-server flows. Stateless access tokens paired with rotating refresh tokens.
- Ephemeral agent sessions for AI agents (Claude, GPT-with-browsing, operator loops). Short-lived, budget-bounded credentials that expire by time or action count, whichever comes first.
Cookie sessions
Configure the session manager
PasscreateCookieSessionManager a config object and your kavach.db instance. The manager handles creation, validation, refresh, and revocation.lib/kavach.ts
Create a session after sign-in
CallcreateSession with a user ID after your authentication logic succeeds. The response includes the cookie header to send back to the browser.routes/sign-in.ts
Validate on each request
Read the cookie from the incoming request and validate it. If valid, the session data is returned. If expired or revoked,success is false.middleware.ts
autoRefresh: true, validating a session that is more than halfway through its lifetime automatically extends it. The response from validateSession includes a refreshedCookieHeader field when a refresh occurred. Forward it in your response.middleware.ts
Revoke on sign-out
Revoking a session removes it from the database immediately. Any subsequent validation attempt returnsSESSION_REVOKED.routes/sign-out.ts
JWT sessions
Use JWT sessions when cookies don’t work: SPAs calling a separate API origin, mobile apps, server-to-server flows. Access tokens are short-lived and stateless. Refresh tokens are opaque random strings stored as SHA-256 hashes, and they rotate on every use.lib/kavach.ts
Create a session
routes/sign-in.ts
Verify on each request
Access token verification is stateless. No database round-trip.middleware.ts
Refresh
CallingrefreshSession marks the old refresh token as used and issues a new access + refresh pair. Each refresh rotates the token, so a stolen token is invalidated the moment the legitimate client refreshes.
routes/refresh.ts
Session freshness
Some operations should require that the user authenticated recently, not just that they hold any valid session. Changing a password, registering a passkey, or modifying billing details are examples where a session from 6 days ago is not good enough even though it is technically valid.createSessionFreshnessModule wraps this check. When a session is older than freshAge seconds, it returns a 403 response with code SESSION_STALE. Your handler returns it directly.
lib/freshness.ts
routes/change-password.ts
SESSION_STALE, redirect them to a lightweight re-authentication page (password confirmation, passkey prompt, or similar) rather than a full sign-out. After re-auth succeeds, refresh the session’s createdAt timestamp by issuing a new session, then retry the original operation.
The
freshAge threshold is separate from maxAge. A session can be well within its 7-day lifetime but still be considered stale for sensitive operations. Keep freshAge short. 5 to 15 minutes is typical.CSRF protection
Cookie-based sessions are vulnerable to cross-site request forgery unless you add a second layer. KavachOS uses the double-submit cookie pattern: a random token is set in a separate readable cookie and must also appear in the request body or header. An attacker’s page can trigger the cookie but cannot read it to reproduce the header value.lib/csrf.ts
Revocation patterns
- Single session
- All sessions
- All except current
Revoke one session
Session metadata
Store arbitrary data on a session at creation time or update it later. Useful for tracking the device, IP address, or a custom attribute your app needs.Creating a session with metadata
Reading session metadata
Metadata is stored as a JSON column. It is not indexed, so avoid querying sessions by metadata fields. If you need to look up sessions by device or IP, store those in a separate indexed column via a custom schema extension.
Human sessions vs agent tokens
| Human sessions | Agent tokens | |
|---|---|---|
| Format | Signed JWT in httpOnly cookie | kv_... bearer token |
| Lifetime | 7 days (configurable) | 24 hours (configurable) |
| Storage | Database row | SHA-256 hash in database |
| Revocation | Per-session or per-user bulk | Per-agent |
| CSRF | Required (cookie-based) | Not needed (header-based) |
| Freshness guard | Yes, for sensitive operations | N/A |
| Action budget | No | Yes (ephemeral sessions) |
kv_ prefix and are stored only as a SHA-256 hash. The raw token is shown once at creation and cannot be recovered. If a token is lost, issue a new one.
Error codes
| Code | Status | Meaning |
|---|---|---|
SESSION_NOT_FOUND | 401 | No session record matches the provided token |
SESSION_EXPIRED | 401 | Session exceeded its maxAge |
SESSION_REVOKED | 401 | Session was explicitly revoked |
SESSION_STALE | 403 | Session is valid but older than freshAge |
CSRF_INVALID | 403 | CSRF token in header does not match cookie value |
ORIGIN_MISMATCH | 403 | Request Origin header not in the allowed list |
REFRESH_TOKEN_NOT_FOUND | 401 | Refresh token does not exist in the database |
REFRESH_TOKEN_USED | 401 | Refresh token was already exchanged (possible replay) |
REFRESH_TOKEN_EXPIRED | 401 | Refresh token lifetime has elapsed |
CREATE_SESSION_FAILED | 500 | Database error during session creation |
Configuration reference
CookieSessionConfig
Name of the session cookie.
Session lifetime in seconds. After this period the session is considered expired even if it has been used recently.
Automatically extend the session when validation is called and more than half the lifetime has elapsed.
Prevents client-side JavaScript from accessing the cookie. Always set this to true for session cookies.
Only send the cookie over HTTPS. Must be true in production.
Controls cross-site cookie behavior. ‘lax’ works for most apps. ‘strict’ adds more protection. ‘none’ is only for cross-origin cookie transport (requires secure: true).
Restricts the cookie to a URL path prefix.
Set a cookie domain to share sessions across subdomains. Omit for single-domain apps.
JwtSessionConfig
Signing secret. A string uses HMAC-SHA256 and must be at least 32 characters. Pass a CryptoKey or JsonWebKey for asymmetric algorithms (RS256, ES256).
JWT signing algorithm. Inferred from the secret type when not set: ‘HS256’ for strings, ‘RS256’ for RSA keys, ‘ES256’ for EC keys.
Access token lifetime in seconds. Keep this short. Access tokens are stateless and cannot be revoked before expiry.
Refresh token lifetime in seconds. Refresh tokens are stored hashed and can be revoked immediately.
Value for the JWT ‘iss’ claim. Validated on every verify call.
Value for the JWT ‘aud’ claim. Validated on every verify call.
Function called at token creation to attach extra claims to the access token payload.
SessionFreshnessConfig
Maximum session age in seconds before a sensitive operation requires re-authentication. The freshness guard returns 403 SESSION_STALE when the session is older than this.
Security best practices
Set
cookie.sameSite: 'lax' for most apps. 'lax' allows the cookie to be sent on top-level navigations (clicking a link) but blocks it on cross-origin subresource requests, which stops most CSRF attacks without breaking OAuth redirect flows. Only use 'strict' if you have no external links that expect to land in an authenticated state.Short access token TTLs (
accessTokenTtl) reduce the window of exposure for a stolen token, but they increase refresh traffic. 15 minutes is a reasonable default. If you need to revoke access instantly (account ban, credential compromise), route your API through the session validation middleware instead of relying solely on stateless JWT verification.