mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-22 15:06:32 +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.
89 lines
4.3 KiB
TypeScript
89 lines
4.3 KiB
TypeScript
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
|
import { HttpException } from '@nestjs/common';
|
|
import jwt from 'jsonwebtoken';
|
|
import type { Request, Response } from 'express';
|
|
|
|
vi.mock('../../../../src/nest/audit/client-ip', () => ({ getClientIp: vi.fn(() => '1.2.3.4') }));
|
|
|
|
import { SessionsController } from '../../../../src/nest/auth/sessions.controller';
|
|
import type { SessionsService } from '../../../../src/nest/sessions/sessions.service';
|
|
import type { AuditService } from '../../../../src/nest/audit/audit.service';
|
|
import type { User } from '../../../../src/types';
|
|
|
|
const SID = '0b7c6f3e-2a51-4c8e-9d43-5f1e2b7a9c10';
|
|
const OTHER = '9f0e1d2c-3b4a-4c5d-8e6f-7a8b9c0d1e2f';
|
|
const user = { id: 7, username: 'u', email: 'u@example.test', role: 'user' } as User;
|
|
|
|
const writeAudit = vi.fn();
|
|
const audit = { writeAudit } as unknown as AuditService;
|
|
|
|
/** A request carrying a session token; the controller only decodes it, the guard verified it. */
|
|
function reqWith(jti?: string): Request {
|
|
const token = jwt.sign({ id: 7, pv: 0 }, 'any-secret', { algorithm: 'HS256', ...(jti ? { jwtid: jti } : {}) });
|
|
return { ip: '9.9.9.9', headers: {}, cookies: { trek_session: token } } as unknown as Request;
|
|
}
|
|
|
|
function resStub() {
|
|
return { clearCookie: vi.fn() } as unknown as Response & { clearCookie: ReturnType<typeof vi.fn> };
|
|
}
|
|
|
|
function controller(o: Partial<Record<'list' | 'revoke' | 'revokeAll', ReturnType<typeof vi.fn>>> = {}) {
|
|
const sessions = { list: vi.fn(), revoke: vi.fn(), revokeAll: vi.fn(), ...o };
|
|
return { c: new SessionsController(sessions as unknown as SessionsService, audit), sessions };
|
|
}
|
|
|
|
beforeEach(() => vi.clearAllMocks());
|
|
|
|
describe('SessionsController', () => {
|
|
it('GET lists the sessions with the current one flagged', async () => {
|
|
const listed = [{ id: SID, created_at: 'a', last_seen_at: 'b', expires_at: 'c', user_agent: null, current: true }];
|
|
const { c, sessions } = controller({ list: vi.fn().mockResolvedValue(listed) });
|
|
|
|
expect(await c.list(user, reqWith(SID))).toEqual({ sessions: listed, current_tracked: true });
|
|
expect(sessions.list).toHaveBeenCalledWith(7, SID);
|
|
});
|
|
|
|
it('GET says so when the request token is not tracked', async () => {
|
|
const { c, sessions } = controller({ list: vi.fn().mockResolvedValue([]) });
|
|
|
|
expect(await c.list(user, reqWith())).toEqual({ sessions: [], current_tracked: false });
|
|
expect(sessions.list).toHaveBeenCalledWith(7, undefined);
|
|
});
|
|
|
|
it('POST revoke-others keeps the current session and audits the count', async () => {
|
|
const { c, sessions } = controller({ revokeAll: vi.fn().mockResolvedValue(3) });
|
|
|
|
expect(await c.revokeOthers(user, reqWith(SID))).toEqual({ success: true, revoked: 3 });
|
|
expect(sessions.revokeAll).toHaveBeenCalledWith(7, SID);
|
|
expect(writeAudit).toHaveBeenCalledWith({ userId: 7, action: 'user.sessions_revoke_others', ip: '1.2.3.4', details: { revoked: 3 } });
|
|
});
|
|
|
|
it('DELETE ends another session and leaves this cookie alone', async () => {
|
|
const { c, sessions } = controller({ revoke: vi.fn().mockResolvedValue(true) });
|
|
const res = resStub();
|
|
|
|
expect(await c.revoke(user, { id: OTHER }, reqWith(SID), res)).toEqual({ success: true });
|
|
expect(sessions.revoke).toHaveBeenCalledWith(7, OTHER);
|
|
expect(res.clearCookie).not.toHaveBeenCalled();
|
|
expect(writeAudit).toHaveBeenCalledWith({ userId: 7, action: 'user.session_revoke', ip: '1.2.3.4', resource: OTHER });
|
|
});
|
|
|
|
it('DELETE of the current session clears the cookie too', async () => {
|
|
const { c } = controller({ revoke: vi.fn().mockResolvedValue(true) });
|
|
const res = resStub();
|
|
|
|
expect(await c.revoke(user, { id: SID }, reqWith(SID), res)).toEqual({ success: true });
|
|
expect(res.clearCookie).toHaveBeenCalledWith('trek_session', expect.any(Object));
|
|
});
|
|
|
|
it('DELETE answers 404 for a session the caller does not have', async () => {
|
|
const { c } = controller({ revoke: vi.fn().mockResolvedValue(false) });
|
|
|
|
const err = await c.revoke(user, { id: OTHER }, reqWith(SID), resStub()).catch((e: unknown) => e);
|
|
expect(err).toBeInstanceOf(HttpException);
|
|
expect((err as HttpException).getStatus()).toBe(404);
|
|
expect((err as HttpException).getResponse()).toEqual({ error: 'Session not found' });
|
|
expect(writeAudit).not.toHaveBeenCalled();
|
|
});
|
|
});
|