This commit is contained in:
Debian
2026-03-30 07:16:50 +00:00
parent 75f48591e9
commit 42b50b78cc
6 changed files with 56 additions and 283 deletions
-64
View File
@@ -15,18 +15,6 @@ export type ProfileRow = {
updated_at: number;
};
export type SessionRow = {
id: string;
email: string;
role: "admin" | "user";
csrf_token: string;
expires_at: number;
created_at: number;
last_seen_at: number;
user_agent_hash: string;
ip_hash: string;
};
type AdminRow = {
email: string;
password_hash: string;
@@ -66,18 +54,6 @@ db.exec(`
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
email TEXT NOT NULL,
role TEXT NOT NULL CHECK(role IN ('admin', 'user')),
csrf_token TEXT NOT NULL,
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
last_seen_at INTEGER NOT NULL,
user_agent_hash TEXT NOT NULL,
ip_hash TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS forwarding_rules (
email TEXT PRIMARY KEY,
target_email TEXT,
@@ -124,18 +100,6 @@ const upsertAdminStatement = db.query(`
VALUES (?1, ?2, ?3, ?3)
ON CONFLICT(email) DO UPDATE SET password_hash = excluded.password_hash, updated_at = excluded.updated_at
`);
const insertSessionStatement = db.query(`
INSERT INTO sessions (id, email, role, csrf_token, expires_at, created_at, last_seen_at, user_agent_hash, ip_hash)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6, ?7, ?8)
`);
const getSessionStatement = db.query("SELECT * FROM sessions WHERE id = ?1");
const touchSessionStatement = db.query(`
UPDATE sessions
SET expires_at = ?1, last_seen_at = ?2, ip_hash = ?3
WHERE id = ?4
`);
const deleteSessionStatement = db.query("DELETE FROM sessions WHERE id = ?1");
const deleteExpiredSessionsStatement = db.query("DELETE FROM sessions WHERE expires_at <= ?1");
const getForwardingStatement = db.query("SELECT * FROM forwarding_rules WHERE email = ?1");
const upsertForwardingStatement = db.query(`
INSERT INTO forwarding_rules (email, target_email, enabled, updated_at)
@@ -173,33 +137,6 @@ export const upsertAdmin = (email: string, passwordHash: string): void => {
ensureProfile(email, "admin");
};
export const createSession = (session: SessionRow): void => {
insertSessionStatement.run(
session.id,
session.email,
session.role,
session.csrf_token,
session.expires_at,
session.created_at,
session.user_agent_hash,
session.ip_hash
);
};
export const getSession = (id: string): SessionRow | null => (getSessionStatement.get(id) as SessionRow | null) ?? null;
export const touchSession = (id: string, expiresAt: number, ipHash: string): void => {
touchSessionStatement.run(expiresAt, Date.now(), ipHash, id);
};
export const deleteSession = (id: string): void => {
deleteSessionStatement.run(id);
};
export const deleteExpiredSessions = (): void => {
deleteExpiredSessionsStatement.run(Date.now());
};
export const getForwardingRule = (email: string): ForwardingRow | null =>
(getForwardingStatement.get(email) as ForwardingRow | null) ?? null;
@@ -225,4 +162,3 @@ export const deleteAvatarFile = async (avatarPath: string | null): Promise<void>
// Best-effort cleanup of replaced avatars.
}
};