feat(database): store dashboard sessions, deploy targets and deployments
Sessions and API tokens keep only a hash of the value handed to the client, so a database dump cannot be replayed. A deployment denormalises its target name so the history survives the deletion of that target. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
3 files changed
+363
No files matched your search
@@ -0,0 +1,107 @@
|
||||
import { getDatabase } from '../index.js';
|
||||
|
||||
/** Browser sessions and machine tokens of the web dashboard (M16-T02 / M16-T03). */
|
||||
|
||||
export function createSession({
|
||||
id,
|
||||
tokenHash,
|
||||
userId = null,
|
||||
username = null,
|
||||
displayName = null,
|
||||
avatar = null,
|
||||
accessLevel = 0,
|
||||
source = 'discord',
|
||||
ip = null,
|
||||
userAgent = null,
|
||||
expiresAt,
|
||||
}) {
|
||||
getDatabase()
|
||||
.prepare(
|
||||
`INSERT INTO web_sessions
|
||||
(id, token_hash, user_id, username, display_name, avatar, access_level, source, ip, user_agent, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
)
|
||||
.run(id, tokenHash, userId, username, displayName, avatar, accessLevel, source, ip, userAgent, expiresAt);
|
||||
return getSession(id);
|
||||
}
|
||||
|
||||
export function getSession(id) {
|
||||
return getDatabase().prepare('SELECT * FROM web_sessions WHERE id = ?').get(id) ?? null;
|
||||
}
|
||||
|
||||
/** Only returns sessions that have not expired, so callers never have to check. */
|
||||
export function getLiveSessionByHash(tokenHash) {
|
||||
return (
|
||||
getDatabase()
|
||||
.prepare("SELECT * FROM web_sessions WHERE token_hash = ? AND expires_at > datetime('now')")
|
||||
.get(tokenHash) ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function touchSession(id) {
|
||||
return getDatabase().prepare("UPDATE web_sessions SET last_seen_at = datetime('now') WHERE id = ?").run(id)
|
||||
.changes;
|
||||
}
|
||||
|
||||
export function deleteSession(id) {
|
||||
return getDatabase().prepare('DELETE FROM web_sessions WHERE id = ?').run(id).changes;
|
||||
}
|
||||
|
||||
export function deleteSessionsOfUser(userId) {
|
||||
return getDatabase().prepare('DELETE FROM web_sessions WHERE user_id = ?').run(userId).changes;
|
||||
}
|
||||
|
||||
export function listSessions(limit = 50) {
|
||||
return getDatabase()
|
||||
.prepare(
|
||||
`SELECT id, user_id, username, display_name, avatar, access_level, source, ip, user_agent,
|
||||
created_at, last_seen_at, expires_at
|
||||
FROM web_sessions WHERE expires_at > datetime('now') ORDER BY last_seen_at DESC LIMIT ?`,
|
||||
)
|
||||
.all(limit);
|
||||
}
|
||||
|
||||
export function purgeExpiredSessions() {
|
||||
return getDatabase().prepare("DELETE FROM web_sessions WHERE expires_at <= datetime('now')").run().changes;
|
||||
}
|
||||
|
||||
export function createApiToken({ name, tokenHash, prefix, accessLevel, createdBy = null, expiresAt = null }) {
|
||||
const { lastInsertRowid } = getDatabase()
|
||||
.prepare(
|
||||
`INSERT INTO web_api_tokens (name, token_hash, prefix, access_level, created_by, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
)
|
||||
.run(name, tokenHash, prefix, accessLevel, createdBy, expiresAt);
|
||||
return Number(lastInsertRowid);
|
||||
}
|
||||
|
||||
export function getLiveApiTokenByHash(tokenHash) {
|
||||
return (
|
||||
getDatabase()
|
||||
.prepare(
|
||||
`SELECT * FROM web_api_tokens
|
||||
WHERE token_hash = ? AND revoked_at IS NULL
|
||||
AND (expires_at IS NULL OR expires_at > datetime('now'))`,
|
||||
)
|
||||
.get(tokenHash) ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function touchApiToken(id) {
|
||||
getDatabase().prepare("UPDATE web_api_tokens SET last_used_at = datetime('now') WHERE id = ?").run(id);
|
||||
}
|
||||
|
||||
export function listApiTokens() {
|
||||
return getDatabase()
|
||||
.prepare(
|
||||
`SELECT id, name, prefix, access_level, created_by, created_at, last_used_at, expires_at, revoked_at
|
||||
FROM web_api_tokens ORDER BY id DESC`,
|
||||
)
|
||||
.all();
|
||||
}
|
||||
|
||||
export function revokeApiToken(id) {
|
||||
return getDatabase()
|
||||
.prepare("UPDATE web_api_tokens SET revoked_at = datetime('now') WHERE id = ? AND revoked_at IS NULL")
|
||||
.run(id).changes;
|
||||
}
|
||||
Reference in new issue
Block a user