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.
96 lines
4.8 KiB
JavaScript
96 lines
4.8 KiB
JavaScript
/**
|
|
* Admin recovery — reset (or create) an admin account when you are locked out.
|
|
*
|
|
* Usage inside the container:
|
|
* docker exec -it trek node server/reset-admin.js
|
|
* docker exec -it -e RESET_ADMIN_EMAIL=me@example.com -e RESET_ADMIN_PASSWORD=secret trek node server/reset-admin.js
|
|
*
|
|
* Defaults to admin@trek.local with a generated password (printed below). The
|
|
* account is flagged must_change_password, so you are prompted to set a new one
|
|
* on first login. Honours TREK_DB_FILE, TREK_DB_JOURNAL_MODE and
|
|
* TREK_DB_SYNCHRONOUS the same way the server does.
|
|
*/
|
|
const path = require('path');
|
|
const crypto = require('crypto');
|
|
const Database = require('better-sqlite3');
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
// Kept in sync with the seeder/authService cost factor.
|
|
const BCRYPT_COST = 12;
|
|
|
|
const email = process.env.RESET_ADMIN_EMAIL || 'admin@trek.local';
|
|
const password = process.env.RESET_ADMIN_PASSWORD || crypto.randomBytes(12).toString('base64url');
|
|
const generated = !process.env.RESET_ADMIN_PASSWORD;
|
|
|
|
// journal_mode is stored in the database file header, not per connection, so
|
|
// opening the file here decides the mode for the server too. Plain JS outside
|
|
// the Nest context can't reach src/app-config, hence the direct read — the
|
|
// defaults must match parsers.resolveDurability() (WAL + NORMAL, FULL once a
|
|
// rollback journal is in play). Run this via `docker exec` and the container's
|
|
// environment applies, same as for the server.
|
|
const journalModes = ['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF'];
|
|
const syncLevels = ['OFF', 'NORMAL', 'FULL', 'EXTRA'];
|
|
|
|
const wantedJournal = (process.env.TREK_DB_JOURNAL_MODE || '').trim().toUpperCase();
|
|
const journalMode = journalModes.includes(wantedJournal) ? wantedJournal : 'WAL';
|
|
if (wantedJournal && journalMode !== wantedJournal) {
|
|
console.warn(`TREK_DB_JOURNAL_MODE="${process.env.TREK_DB_JOURNAL_MODE}" is not a SQLite journal mode — using ${journalMode}.`);
|
|
}
|
|
|
|
const wantedSync = (process.env.TREK_DB_SYNCHRONOUS || '').trim().toUpperCase();
|
|
const defaultSync = journalMode === 'WAL' ? 'NORMAL' : 'FULL';
|
|
const synchronous = syncLevels.includes(wantedSync) ? wantedSync : defaultSync;
|
|
if (wantedSync && synchronous !== wantedSync) {
|
|
console.warn(`TREK_DB_SYNCHRONOUS="${process.env.TREK_DB_SYNCHRONOUS}" is not a SQLite synchronous level — using ${synchronous}.`);
|
|
}
|
|
|
|
const dbPath = process.env.TREK_DB_FILE || path.join(__dirname, 'data/travel.db');
|
|
const db = new Database(dbPath);
|
|
// Spelled out rather than left to better-sqlite3's default, so switching the
|
|
// journal mode below waits for a busy server instead of failing on the spot.
|
|
// Five seconds, same as src/db/database.ts.
|
|
db.exec('PRAGMA busy_timeout = 5000');
|
|
// Temp files in memory, like every connection src/db/connection.ts opens: a
|
|
// container with a read-only root and no tmpfs on /tmp has nowhere to put them
|
|
// (#2518).
|
|
db.exec('PRAGMA temp_store = MEMORY');
|
|
db.exec(`PRAGMA journal_mode = ${journalMode}`);
|
|
db.exec(`PRAGMA synchronous = ${synchronous}`);
|
|
|
|
const hash = bcrypt.hashSync(password, BCRYPT_COST);
|
|
// Case-insensitive like login, so Admin@example.com is found as admin@example.com.
|
|
const existing = db.prepare('SELECT id FROM users WHERE LOWER(email) = LOWER(?)').get(email);
|
|
|
|
if (existing) {
|
|
// password_version moves on, as on every password change in the app: it is
|
|
// what ends the sessions still signed in to an account that needed resetting.
|
|
db.prepare('UPDATE users SET password_hash = ?, role = ?, must_change_password = 1, password_version = COALESCE(password_version, 0) + 1 WHERE id = ?')
|
|
.run(hash, 'admin', existing.id);
|
|
// Its sessions end too, so the account's session list shows none of them
|
|
// as live. The table is missing on a database that no server tracking
|
|
// sessions has booted yet; the password_version bump above already refuses
|
|
// every token there.
|
|
const sessionsTable = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'user_sessions'").get();
|
|
if (sessionsTable) {
|
|
db.prepare('UPDATE user_sessions SET revoked_at = CURRENT_TIMESTAMP WHERE user_id = ? AND revoked_at IS NULL')
|
|
.run(existing.id);
|
|
}
|
|
console.log(`\n✓ Admin password reset: ${email}`);
|
|
} else {
|
|
// 'admin' is usually taken by the first-run seed — pick the first free username
|
|
// so the insert can't trip the UNIQUE(username) constraint.
|
|
let username = 'admin';
|
|
let n = 1;
|
|
while (db.prepare('SELECT 1 FROM users WHERE username = ?').get(username)) {
|
|
username = `admin${n++}`;
|
|
}
|
|
db.prepare('INSERT INTO users (username, email, password_hash, role, must_change_password) VALUES (?, ?, ?, ?, 1)')
|
|
.run(username, email, hash, 'admin');
|
|
console.log(`\n✓ Admin account created: ${email} (username: ${username})`);
|
|
}
|
|
|
|
if (generated) console.log(` Password: ${password}`);
|
|
console.log(' You will be asked to change the password on first login.\n');
|
|
|
|
db.close();
|