mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16: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.
113 lines
5.4 KiB
TypeScript
113 lines
5.4 KiB
TypeScript
import { EntitySchema, type EntityRepository } from '@mikro-orm/core';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
import { ALL_ENTITIES } from '../../../src/db/entities';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
|
import { TrekRepository } from '../../../src/db/repositories/_shared/trek-repository';
|
|
|
|
const ENTITIES_DIR = path.join(__dirname, '../../../src/db/entities');
|
|
const REPOSITORIES_DIR = path.join(__dirname, '../../../src/db/repositories');
|
|
|
|
describe('ALL_ENTITIES', () => {
|
|
it('ENT-001: lists exactly one schema per *.entity.ts file', () => {
|
|
const files = fs.readdirSync(ENTITIES_DIR).filter((f) => f.endsWith('.entity.ts')).sort();
|
|
const names = ALL_ENTITIES.map((s) => s.name).sort();
|
|
const expected = files.map((f) => f.replace(/\.entity\.ts$/, '')).sort();
|
|
expect(names).toEqual(expected);
|
|
});
|
|
|
|
it('ENT-002: every entry is an EntitySchema with a class', () => {
|
|
for (const schema of ALL_ENTITIES) {
|
|
expect(schema).toBeInstanceOf(EntitySchema);
|
|
expect(typeof schema.meta.class).toBe('function');
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Plan 2 Task 3: every one of the 132 entities the migrated schema
|
|
* describes (120 rewritten + the 5 document-sync tables added this task,
|
|
* + push_subscriptions and google_api_usage from legacy steps 245 and 254,
|
|
* + tour_types, tours and tour_waypoints from the Tours migration,
|
|
* + scheduler_leases from the cron lease migration
|
|
* + user_sessions, the session rows behind revocable session tokens)
|
|
* is registered, and no two share a class name (a silent duplicate would
|
|
* shadow one of them in `orm.getMetadata()`, exactly the kind of drift
|
|
* `entity-schema-parity.test.ts` can't see — it iterates the metadata map,
|
|
* which de-duplicates by class name).
|
|
*/
|
|
it('ENT-003: exactly 132 entities, every class name unique', () => {
|
|
expect(ALL_ENTITIES.length).toBe(132);
|
|
const names = ALL_ENTITIES.map((s) => s.meta.className);
|
|
expect(new Set(names).size).toBe(names.length);
|
|
});
|
|
|
|
/**
|
|
* Task 1's review flagged this binding as otherwise unpinned: dropping an
|
|
* entity's `repository: () => XRepository` mutation stayed green in every
|
|
* gate up to here (task-1-review.md, minor 1). `em.getRepository(X)` must
|
|
* return an instance of the exact repository class
|
|
* `src/db/repositories/<Name>.repository.ts` exports, for every entity —
|
|
* the 120 the generator wrote a stub for this task plus the 5 pre-existing,
|
|
* hand-written ones (`Days`, `DayNotes`, `Trips`, `BudgetCategoryOrder`,
|
|
* `VacayUserSettings`) it left untouched.
|
|
*/
|
|
describe('ENT-004: em.getRepository(X) is an instance of X.repository.ts', () => {
|
|
let t: TestOrm;
|
|
const testDb = createSnapshotTestDb();
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestOrm(testDb);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await t.close();
|
|
testDb.close();
|
|
});
|
|
|
|
it('every entity resolves to its own repository class', async () => {
|
|
const failures: string[] = [];
|
|
for (const schema of ALL_ENTITIES) {
|
|
const className = schema.meta.className;
|
|
const repoFile = path.join(REPOSITORIES_DIR, `${className}.repository.ts`);
|
|
if (!fs.existsSync(repoFile)) {
|
|
failures.push(`${className}: no src/db/repositories/${className}.repository.ts file`);
|
|
continue;
|
|
}
|
|
const mod: Record<string, unknown> = await import(/* @vite-ignore */ `../../../src/db/repositories/${className}.repository`);
|
|
const RepoClass = mod[`${className}Repository`];
|
|
if (typeof RepoClass !== 'function') {
|
|
failures.push(`${className}: repository module has no exported ${className}Repository class`);
|
|
continue;
|
|
}
|
|
const repo: EntityRepository<object> = t.em.getRepository(schema.class as new () => object);
|
|
if (!(repo instanceof (RepoClass as abstract new (...args: never[]) => unknown))) {
|
|
failures.push(`${className}: em.getRepository() did not return a ${className}Repository instance`);
|
|
}
|
|
// Task 7 review, H2 carry-list item 1: the `extends EntityRepository`
|
|
// ratchet made self-enforcing — every bound repository must also be
|
|
// a TrekRepository (validateRequestContext + the disableIdentityMap
|
|
// read default), not just an instance of its own named class.
|
|
if (!(repo instanceof TrekRepository)) {
|
|
failures.push(`${className}: em.getRepository() did not return a TrekRepository instance`);
|
|
}
|
|
}
|
|
expect(failures).toEqual([]);
|
|
});
|
|
|
|
// Task 7 review, H2: a source-scan companion to the instanceof check
|
|
// above — belt and suspenders against a repository that extends
|
|
// TrekRepository indirectly (or not through this loop's import path).
|
|
it('SCAN-001: exactly one repository file extends EntityRepository directly — the base class itself', () => {
|
|
const files = fs.readdirSync(REPOSITORIES_DIR).filter((f) => f.endsWith('.repository.ts'));
|
|
const hits: string[] = [];
|
|
for (const file of files) {
|
|
const content = fs.readFileSync(path.join(REPOSITORIES_DIR, file), 'utf-8');
|
|
if (/\bextends\s+EntityRepository\b/.test(content)) hits.push(file);
|
|
}
|
|
expect(hits).toEqual([]); // the base class lives in _shared/, not matched by *.repository.ts
|
|
});
|
|
});
|
|
});
|