Files
loki/scripts/test/token-session-db.ts
Cato 198d77e523 feat(auth): an agent's token opens a browser session, with the token's power and no more (#1679)
* feat(auth): an agent's token opens a browser session, with the token's power and no more

A browser cannot send a Bearer header on a page load, so a Viewer seat with a
ck_ token was sent to sign-in from /projects, /terminal, the workbench,
history and feedback.

- POST /api/token-session (Bearer ck_ only) mints a link that opens once,
  within two minutes. The link carries a random code; the server keeps its
  SHA-256 (token_sessions, migration 0138). The token is never in the link.
- GET /api/auth/loki/token-session (an AccountKit plugin endpoint) opens it:
  a session for the token's user that ends after an hour, is never
  refreshed, and is recorded as the token's before its cookie is set.
- Such a session reads like the user (pages, GET on the API) and writes only
  where a token is accepted: the proxy marks its writes and person-only
  routes read them as signed out. It never reaches tokens, a shell, the
  password, linked sign-ins or export, nor AccountKit's passkey and second
  factor endpoints. Revoking or expiring the token signs it out.

Tests: scripts/test/token-session.ts (landing paths, the refused routes, the
middleware decision, the wiring); scripts/test/token-session-db.ts in
test:db (single use, expiry, revoked and expired tokens, hash-only storage).

* style(drizzle): format the 0138 snapshot and journal
2026-11-12 22:24:41 +01:00

140 lines
5.1 KiB
TypeScript

/**
* A token-session link opens once, before it expires, while its token lives;
* and the session it opened reads as signed out once the token is gone
* (src/lib/auth/token-session.ts), against a real Postgres.
*
* Same rules as lane-and-reaper: TEST_DATABASE_URL, or DATABASE_URL in CI.
*/
const target =
process.env.TEST_DATABASE_URL ??
(process.env.CI === "true" ? process.env.DATABASE_URL : undefined);
if (!target) {
console.log(
"token-session-db: SKIP — needs TEST_DATABASE_URL (or CI=true with DATABASE_URL). " +
"It writes rows, so it never borrows a dev database by default.",
);
process.exit(0);
}
process.env.DATABASE_URL = target;
async function main(): Promise<number> {
const { db } = await import("@/db");
const { sql } = await import("drizzle-orm");
const { createAgentToken, deleteAgentToken } = await import("@/db/queries/agent-tokens");
const q = await import("@/db/queries/token-sessions");
let passed = 0;
const failures: string[] = [];
async function check(name: string, fn: () => Promise<void>) {
try {
await fn();
passed++;
console.log(` ✓ ${name}`);
} catch (err) {
failures.push(name);
const cause = (err as { cause?: { message?: string } }).cause?.message;
console.log(` ✗ ${name}\n ${cause ?? (err as Error).message.split("\n")[0]}`);
}
}
function assert(cond: unknown, msg: string): asserts cond {
if (!cond) throw new Error(msg);
}
const stamp = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
const rows = await db.execute<{ id: string }>(
sql`INSERT INTO users (username) VALUES (${`tokensession-${stamp}`}) RETURNING id`,
);
const user = rows[0]!.id;
const { token, record } = await createAgentToken(user, "qa seat");
const mint = (ttlSeconds = 120) =>
q.mintTokenSessionLink({
userId: user,
agentTokenId: record.id,
nextPath: "/projects",
ttlSeconds,
});
await check("a live token is found, an unknown one is not", async () => {
assert((await q.findLiveAgentToken(token))?.userId === user, "the token was not found");
assert(
(await q.findLiveAgentToken(`ck_${"0".repeat(64)}`)) === null,
"a made-up token was found",
);
});
await check("the code is kept as a hash, never in plain", async () => {
const { code } = await mint();
const stored = await db.execute<{ n: number }>(
sql`SELECT count(*)::int AS n FROM token_sessions WHERE code_hash = ${code}`,
);
assert(stored[0]!.n === 0, "the plaintext code is stored");
});
await check("a link opens once", async () => {
const { code } = await mint();
const first = await q.consumeTokenSessionLink(code);
assert(first?.userId === user && first.nextPath === "/projects", "the link did not open");
assert((await q.consumeTokenSessionLink(code)) === null, "the link opened twice");
});
await check("an expired link does not open", async () => {
const { code } = await mint(-1);
assert((await q.consumeTokenSessionLink(code)) === null, "an expired link opened");
});
await check("an unknown code does not open", async () => {
assert((await q.consumeTokenSessionLink("nope")) === null, "an unknown code opened");
assert((await q.consumeTokenSessionLink("")) === null, "an empty code opened");
});
await check("a session is the token's while the token lives, then signed out", async () => {
const { code } = await mint();
const link = await q.consumeTokenSessionLink(code);
assert(link, "the link did not open");
const sessionId = `sess-${stamp}`;
await q.bindTokenSession(link.id, sessionId);
assert(
(await q.tokenSessionState(sessionId))?.live === true,
"a live token's session is not live",
);
assert(
(await q.tokenSessionState(`other-${stamp}`)) === null,
"a person's session reads as a token's",
);
const pending = await mint();
await deleteAgentToken(record.id, user);
assert(
(await q.tokenSessionState(sessionId))?.live === false,
"a revoked token's session lives on",
);
assert(
(await q.consumeTokenSessionLink(pending.code)) === null,
"a link minted before the token was revoked still opens",
);
});
await check("an expired token's link does not open", async () => {
const other = await createAgentToken(user, "qa seat 2");
const { code } = await q.mintTokenSessionLink({
userId: user,
agentTokenId: other.record.id,
nextPath: "/",
ttlSeconds: 120,
});
await db.execute(
sql`UPDATE agent_tokens SET expires_at = NOW() - INTERVAL '1 minute' WHERE id = ${other.record.id}`,
);
assert((await q.consumeTokenSessionLink(code)) === null, "an expired token's link opened");
});
await db.execute(sql`DELETE FROM token_sessions WHERE user_id = ${user}`).catch(() => undefined);
await db.execute(sql`DELETE FROM agent_tokens WHERE user_id = ${user}`).catch(() => undefined);
await db.execute(sql`DELETE FROM users WHERE id = ${user}`).catch(() => undefined);
console.log(`\ntoken-session-db: ${passed} passed, ${failures.length} failed`);
return failures.length ? 1 : 0;
}
void main().then((code) => process.exit(code));