Skip to content
refs
History
78 lines · 3.4 KBRaw
1import { createAccessToken, now, sha256 } from "../auth.ts";
2import type { Person } from "../contract/actor.ts";
3import type { Me, Waitlist } from "../contract/handles.ts";
4import type { WaitlistEntry } from "../contract/views.ts";
5import * as mail from "../mail.ts";
6import { captureAccountEvent } from "../analytics.ts";
7import { conflict, invalid, ok, problem } from "../outcomes.ts";
8import { activateUser } from "../repos.ts";
9import { agentGrants, revokeAgentGrant } from "../oauth.ts";
10
11export function openMe(env: Env, person: Person): Me {
12 const db = env.DB;
13 return {
14 person,
15
16 async tokens() {
17 const { results } = await db
18 .prepare("SELECT id, name, created_at AS createdAt, last_used_at AS lastUsedAt FROM access_tokens WHERE user_id = ? ORDER BY created_at DESC")
19 .bind(person.id)
20 .all<{ id: string; name: string; createdAt: number; lastUsedAt: number | null }>();
21 return results;
22 },
23
24 async createToken(name) {
25 const text = name.trim();
26 if (!text || text.length > 64) return invalid("Token name must be 1–64 characters.", "name");
27 const secret = await createAccessToken(db, person.id, text);
28 const token = (await db.prepare("SELECT id, name, created_at AS createdAt, last_used_at AS lastUsedAt FROM access_tokens WHERE token_hash = ?").bind(await sha256(secret)).first<{ id: string; name: string; createdAt: number; lastUsedAt: number | null }>())!;
29 return ok({ token, secret });
30 },
31
32 async revokeToken(id) {
33 await db.prepare("DELETE FROM access_tokens WHERE id = ? AND user_id = ?").bind(id, person.id).run();
34 return ok();
35 },
36
37 agentGrants: () => agentGrants(env, person.id),
38
39 async revokeAgentGrant(id) {
40 await revokeAgentGrant(env, person.id, id);
41 await endGrantSessions(env.DB, person.id, id, now());
42 return ok();
43 },
44 };
45}
46
47export function openWaitlist(env: Env, ctx: ExecutionContext): Waitlist {
48 const db = env.DB;
49 return {
50 async list() {
51 const { results } = await db
52 .prepare("SELECT id, name, email, handle, created_at AS joinedAt FROM users WHERE status = 'waitlisted' ORDER BY created_at")
53 .all<WaitlistEntry>();
54 return results;
55 },
56
57 async approve(id) {
58 try {
59 const results = await db.batch(activateUser(db, id, now()));
60 if (!results.at(-1)!.meta.changes) return problem("conflict", "This person is no longer on the waitlist.");
61 } catch {
62 return problem("conflict", "Someone already has this person's handle. Ask them to sign up again with another one.");
63 }
64 captureAccountEvent(env, ctx, { name: "account.approved", userId: id, approvalPath: "waitlist" });
65 const person = await db.prepare("SELECT email, handle FROM users WHERE id = ?").bind(id).first<{ email: string; handle: string }>();
66 if (person) mail.send(env, ctx, mail.approved(env.PUBLIC_ORIGIN, person.email, person.handle));
67 return ok();
68 },
69 };
70}
71
72/**
73 * Ends the live sessions an OAuth grant started, once the human revokes it, so their git tokens stop working too.
74 * Sessions from before grants were recorded have no grant, and end with any revocation.
75 */
76export async function endGrantSessions(db: D1Database, userId: string, grantId: string, at: number): Promise<void> {
77 await db.prepare("UPDATE agent_sessions SET status = 'finished', ended_at = ? WHERE user_id = ? AND (grant_id = ? OR grant_id IS NULL) AND ended_at IS NULL").bind(at, userId, grantId).run();
78}