mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-23 15:36:33 +00:00
9aab9e7513
A session token was a signed JWT and nothing else. Logout only cleared the cookie, a 30-day "remember me" token stayed valid wherever a copy of it had ended up, and nobody could sign out one device short of changing the password or rotating the JWT secret for everyone. Every session token now carries an id (jti) naming a row in the new user_sessions table (id, user_id, created_at, last_seen_at, expires_at, revoked_at, user_agent cut to 256 characters; no address is kept). The rows live behind UserSessionsRepository and the new sessions domain, whose SessionsService is the one issuer: AuthService.generateToken and the OIDC sign-in both go through it, and sliding renewal re-signs a tracked token under the same id and moves its expiry. verifyJwtAndLoadUser takes the repository and refuses a token whose session is revoked, expired or gone, refreshing last_seen_at at most every five minutes. A token without a jti was issued before this change; it passes until its own expiry (or a password change), so the upgrade signs nobody out, and renewal turns it into a tracked session. Sessions end on logout (the one the request names, verified even when expired), on a password change (all, the device gets a new one), a password reset, an admin setting a password, reset-admin.js, an MFA disable (all but the current one), an admin clearing a user's MFA, and with the account. GET /api/auth/sessions lists the active sessions, DELETE /api/auth/sessions/:id ends one and POST /api/auth/sessions/revoke-others ends all but the current, with their contracts in @trek/shared and two new audit actions. There is no UI for them yet. MCP exposes no account or session tools, so there is no parallel tool to change; an MCP call made with a session JWT as bearer goes through the same check. The token in the login, register, demo-login, MFA and passkey bodies stays for API clients but is marked deprecated in the API docs and in the client type, which never read it. A nightly session-purge job on CronRegistrarService deletes the expired and revoked rows. The migration is 042500. The sessions domain gets its own coverage entry at the 80 floor until a full run measures it.
57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import { z } from 'zod';
|
|
|
|
/**
|
|
* Sign-in sessions contract for /api/auth/sessions.
|
|
*
|
|
* Every session token the server issues carries an id (the JWT `jti`) and a
|
|
* row in `user_sessions`, so a user can see where they are signed in and end
|
|
* one of those sessions, or all but the current one. Tokens issued before
|
|
* sessions were tracked carry no id; they are not listed and stay valid until
|
|
* they expire or the password changes.
|
|
*
|
|
* Timestamps are the database text the rest of the API emits
|
|
* (`YYYY-MM-DD HH:MM:SS`, UTC).
|
|
*/
|
|
export const userSessionIdSchema = z.uuid();
|
|
export type UserSessionId = z.infer<typeof userSessionIdSchema>;
|
|
|
|
export const userSessionSchema = z.object({
|
|
id: userSessionIdSchema,
|
|
created_at: z.string(),
|
|
last_seen_at: z.string(),
|
|
expires_at: z.string(),
|
|
/** The browser's User-Agent at sign-in, cut to 256 characters; null when it sent none. */
|
|
user_agent: z.string().nullable(),
|
|
/** True for the session the request itself was made with. */
|
|
current: z.boolean(),
|
|
});
|
|
export type UserSession = z.infer<typeof userSessionSchema>;
|
|
|
|
export const userSessionListResponseSchema = z.object({
|
|
sessions: z.array(userSessionSchema),
|
|
/**
|
|
* False when the request was made with a token issued before sessions were
|
|
* tracked: no row is `current` then, and signing out the others leaves that
|
|
* token alone too.
|
|
*/
|
|
current_tracked: z.boolean(),
|
|
});
|
|
export type UserSessionListResponse = z.infer<typeof userSessionListResponseSchema>;
|
|
|
|
export const userSessionRevokeParamsSchema = z.object({
|
|
id: userSessionIdSchema,
|
|
});
|
|
export type UserSessionRevokeParams = z.infer<typeof userSessionRevokeParamsSchema>;
|
|
|
|
export const userSessionRevokeResponseSchema = z.object({
|
|
success: z.literal(true),
|
|
});
|
|
export type UserSessionRevokeResponse = z.infer<typeof userSessionRevokeResponseSchema>;
|
|
|
|
export const userSessionRevokeOthersResponseSchema = z.object({
|
|
success: z.literal(true),
|
|
/** How many sessions were signed out. */
|
|
revoked: z.number().int().nonnegative(),
|
|
});
|
|
export type UserSessionRevokeOthersResponse = z.infer<typeof userSessionRevokeOthersResponseSchema>;
|