mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +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.
97 lines
4.4 KiB
TypeScript
97 lines
4.4 KiB
TypeScript
/**
|
|
* `Migration20200101042600_a_session_row_per_sign_in`: the `user_sessions`
|
|
* table behind revocable session tokens. Migrate to the step before it, apply
|
|
* just this one, assert the shape, and that an install with users and no
|
|
* sessions comes through untouched (tokens from before carry no session id
|
|
* and are not looked up, so nothing has to be backfilled).
|
|
*/
|
|
import { describe, expect, it } from 'vitest';
|
|
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
|
|
import type { MikroORM } from '@mikro-orm/sqlite';
|
|
|
|
const TARGET = 'Migration20200101042600_a_session_row_per_sign_in';
|
|
|
|
async function ormBeforeTarget(): Promise<MikroORM> {
|
|
const orm = await createMigrationOrm();
|
|
const names = await pendingNames(orm);
|
|
const idx = names.indexOf(TARGET);
|
|
expect(idx).toBeGreaterThan(0);
|
|
await migrateTo(orm, names[idx - 1]);
|
|
return orm;
|
|
}
|
|
|
|
describe('user_sessions migration', () => {
|
|
it('USERSESSMIG-001: creates the table with a non-null text id, the owner foreign key and both indexes', async () => {
|
|
const orm = await ormBeforeTarget();
|
|
try {
|
|
await rawExec(orm, "INSERT INTO users (id, username, email, password_hash) VALUES (1, 'owner', 'owner@test', 'x')");
|
|
await migrateTo(orm, TARGET);
|
|
|
|
const columns = await rawQuery<{ name: string; type: string; notnull: number; pk: number }>(
|
|
orm,
|
|
"SELECT name, type, \"notnull\", pk FROM pragma_table_info('user_sessions') ORDER BY cid",
|
|
);
|
|
expect(columns).toEqual([
|
|
{ name: 'id', type: 'TEXT', notnull: 1, pk: 1 },
|
|
{ name: 'user_id', type: 'INTEGER', notnull: 1, pk: 0 },
|
|
{ name: 'created_at', type: 'DATETIME', notnull: 1, pk: 0 },
|
|
{ name: 'last_seen_at', type: 'DATETIME', notnull: 1, pk: 0 },
|
|
{ name: 'expires_at', type: 'DATETIME', notnull: 1, pk: 0 },
|
|
{ name: 'revoked_at', type: 'DATETIME', notnull: 0, pk: 0 },
|
|
{ name: 'user_agent', type: 'TEXT', notnull: 0, pk: 0 },
|
|
]);
|
|
|
|
const fks = await rawQuery<{ table: string; from: string; on_delete: string }>(
|
|
orm,
|
|
"SELECT \"table\", \"from\", on_delete FROM pragma_foreign_key_list('user_sessions')",
|
|
);
|
|
expect(fks).toEqual([{ table: 'users', from: 'user_id', on_delete: 'CASCADE' }]);
|
|
|
|
const indexes = await rawQuery<{ name: string }>(orm, "SELECT name FROM pragma_index_list('user_sessions') ORDER BY name");
|
|
expect(indexes.map((i) => i.name)).toEqual(expect.arrayContaining(['idx_user_sessions_expires', 'idx_user_sessions_user']));
|
|
|
|
// The existing account is untouched and has no session rows to begin with.
|
|
expect(await rawQuery(orm, 'SELECT id, username FROM users')).toEqual([{ id: 1, username: 'owner' }]);
|
|
expect(await rawQuery(orm, 'SELECT COUNT(*) AS n FROM user_sessions')).toEqual([{ n: 0 }]);
|
|
} finally {
|
|
await orm.close(true);
|
|
}
|
|
});
|
|
|
|
it('USERSESSMIG-002: a session row defaults its timestamps and goes with its user', async () => {
|
|
const orm = await ormBeforeTarget();
|
|
try {
|
|
await migrateTo(orm, TARGET);
|
|
await rawExec(orm, 'PRAGMA foreign_keys = ON');
|
|
await rawExec(orm, "INSERT INTO users (id, username, email, password_hash) VALUES (2, 'sam', 'sam@test', 'x')");
|
|
await rawExec(orm, "INSERT INTO user_sessions (id, user_id, expires_at) VALUES ('s1', 2, '2030-01-01 00:00:00')");
|
|
|
|
const [row] = await rawQuery<{ created_at: string; last_seen_at: string; revoked_at: null }>(
|
|
orm,
|
|
"SELECT created_at, last_seen_at, revoked_at FROM user_sessions WHERE id = 's1'",
|
|
);
|
|
expect(row.created_at).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
|
expect(row.last_seen_at).toBe(row.created_at);
|
|
expect(row.revoked_at).toBeNull();
|
|
|
|
await rawExec(orm, 'DELETE FROM users WHERE id = 2');
|
|
expect(await rawQuery(orm, 'SELECT COUNT(*) AS n FROM user_sessions')).toEqual([{ n: 0 }]);
|
|
} finally {
|
|
await orm.close(true);
|
|
}
|
|
});
|
|
|
|
it('USERSESSMIG-003: refuses a session row without an id', async () => {
|
|
const orm = await ormBeforeTarget();
|
|
try {
|
|
await migrateTo(orm, TARGET);
|
|
await rawExec(orm, "INSERT INTO users (id, username, email, password_hash) VALUES (3, 'kim', 'kim@test', 'x')");
|
|
await expect(
|
|
rawExec(orm, "INSERT INTO user_sessions (id, user_id, expires_at) VALUES (NULL, 3, '2030-01-01 00:00:00')"),
|
|
).rejects.toThrow(/NOT NULL/);
|
|
} finally {
|
|
await orm.close(true);
|
|
}
|
|
});
|
|
});
|