Public: anyone can read this; the crew can change it.
An account's link to an outside identity is one table keyed by kind, with a prover per kind #18536
Merged 1h 55m ago, live in production
mason/forge:mason/18490-an-account-s-link-to-an-outside-identity → main 0 commits fork 2 threads · 2 unresolved
10 files
changed +247
−11
- src/core/account_link.ts
- src/core/migration-pins/20261206183000-account-link.pin
- src/core/migrations/20261206183000-account-link.ts
- src/core/proven_identities.ts
- src/games/chat.ts
- src/kinds/kind.ts
- src/kinds/minecraft/index.ts
- src/kinds/minecraft/prover.ts
- test/account-link.test.ts
- test/kinds.test.ts
src/core/account_link.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | /** | |
| 2 | * An account's links to outside identities (#18490): one table, `account_link`, a row per | |
| 3 | * (account, kind, outside id), each with when it was proved and how. Plain data in and out; the | |
| 4 | * proof itself belongs to a kind's prover (`src/kinds`), which says what its outside id looks like. | |
| 5 | * Until the next release moves the writes here, `game_link` triggers also write the Minecraft rows. | |
| 6 | */ | |
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | ||
| 9 | export interface AccountLink { | |
| 10 | readonly account: string; | |
| 11 | readonly kind: string; | |
| 12 | readonly outsideId: string; | |
| 13 | readonly provedAt: string; | |
| 14 | readonly how: string; | |
| 15 | } | |
| 16 | ||
| 17 | interface Row { account: string; kind: string; outside_id: string; proved_at: string; how: string } | |
| 18 | const shown = (r: Row): AccountLink => ({ account: r.account, kind: r.kind, outsideId: r.outside_id, provedAt: r.proved_at, how: r.how }); | |
| 19 | ||
| 20 | /** Record a link as proved; one already kept stays as it was. True when a row was added. */ | |
| 21 | export function addLink(db: Database, link: AccountLink): boolean { | |
| 22 | return db | |
| 23 | .query("INSERT OR IGNORE INTO account_link (account, kind, outside_id, proved_at, how) VALUES (?1, ?2, ?3, ?4, ?5)") | |
| 24 | .run(link.account, link.kind, link.outsideId, link.provedAt, link.how).changes > 0; | |
| 25 | } | |
| 26 | ||
| 27 | /** The account's links of `kind`, oldest first. */ | |
| 28 | export const linksOfKind = (db: Database, account: string, kind: string): AccountLink[] => | |
| 29 | db | |
| 30 | .query<Row, [string, string]>("SELECT * FROM account_link WHERE account = ?1 AND kind = ?2 ORDER BY proved_at, outside_id") | |
| 31 | .all(account, kind) | |
| 32 | .map(shown); | |
| 33 | ||
| 34 | /** The accounts that proved this outside id. */ | |
| 35 | export const ownersOf = (db: Database, kind: string, outsideId: string): string[] => | |
| 36 | db | |
| 37 | .query<{ account: string }, [string, string]>("SELECT account FROM account_link WHERE kind = ?1 AND outside_id = ?2 ORDER BY proved_at, account") | |
| 38 | .all(kind, outsideId) | |
| 39 | .map((r) => r.account); |
src/core/migration-pins/20261206183000-account-link.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | c815a2f52b44d243d8777b129950aa1ca7c9983cba4740e819756675c0995488 |
src/core/migrations/20261206183000-account-link.ts added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | // An account's link to an outside identity, step 1 of 2 (#18490): one table keyed by kind, | |
| 2 | // account_link (account, kind, outside id, proved at, how), beside game_link, which stays. It is | |
| 3 | // filled from the links that name a UUID, and triggers on game_link keep it in step, so the | |
| 4 | // previous release, which writes only game_link, runs against this database unchanged and its | |
| 5 | // links show here. The triggers are the one writer while both releases run (the prover's `prove` | |
| 6 | // is used from step 2, when the writes move): INSERT, UPDATE and DELETE are covered, and so is | |
| 7 | // INSERT OR REPLACE, which removes the row it replaces without firing a DELETE trigger, so a | |
| 8 | // BEFORE INSERT trigger clears that row's account_link first. A UUID is kept as the server listed | |
| 9 | // it, lowercased, whether or not it passes the prover's cleaner. A link with no UUID proves nothing | |
| 10 | // and has no row. One outside id can be linked on several boxes: its row goes with the last of | |
| 11 | // them. Step 2 will move the writes here and drop game_link. | |
| 12 | export default ` | |
| 13 | CREATE TABLE IF NOT EXISTS account_link ( | |
| 14 | account TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE, | |
| 15 | kind TEXT NOT NULL, | |
| 16 | outside_id TEXT NOT NULL, | |
| 17 | proved_at TEXT NOT NULL, | |
| 18 | how TEXT NOT NULL, | |
| 19 | PRIMARY KEY (account, kind, outside_id) | |
| 20 | ); | |
| 21 | CREATE INDEX IF NOT EXISTS account_link_outside ON account_link (kind, outside_id); | |
| 22 | INSERT OR IGNORE INTO account_link (account, kind, outside_id, proved_at, how) | |
| 23 | SELECT user_id, 'minecraft', lower(uuid), MIN(created_at), 'game-door' FROM game_link WHERE uuid != '' GROUP BY user_id, lower(uuid); | |
| 24 | CREATE TRIGGER IF NOT EXISTS account_link_from_game_replace BEFORE INSERT ON game_link | |
| 25 | WHEN EXISTS (SELECT 1 FROM game_link WHERE box_id = NEW.box_id AND player = NEW.player AND uuid != '') BEGIN | |
| 26 | DELETE FROM account_link WHERE kind = 'minecraft' | |
| 27 | AND (account, outside_id) IN (SELECT user_id, lower(uuid) FROM game_link WHERE box_id = NEW.box_id AND player = NEW.player) | |
| 28 | AND NOT EXISTS (SELECT 1 FROM game_link g WHERE g.user_id = account_link.account AND lower(g.uuid) = account_link.outside_id | |
| 29 | AND NOT (g.box_id = NEW.box_id AND g.player = NEW.player)); | |
| 30 | END; | |
| 31 | CREATE TRIGGER IF NOT EXISTS account_link_from_game_insert AFTER INSERT ON game_link WHEN NEW.uuid != '' BEGIN | |
| 32 | INSERT OR IGNORE INTO account_link (account, kind, outside_id, proved_at, how) VALUES (NEW.user_id, 'minecraft', lower(NEW.uuid), NEW.created_at, 'game-door'); | |
| 33 | END; | |
| 34 | CREATE TRIGGER IF NOT EXISTS account_link_from_game_delete AFTER DELETE ON game_link WHEN OLD.uuid != '' BEGIN | |
| 35 | DELETE FROM account_link WHERE account = OLD.user_id AND kind = 'minecraft' AND outside_id = lower(OLD.uuid) | |
| 36 | AND NOT EXISTS (SELECT 1 FROM game_link WHERE user_id = OLD.user_id AND lower(uuid) = lower(OLD.uuid)); | |
| 37 | END; | |
| 38 | CREATE TRIGGER IF NOT EXISTS account_link_from_game_update AFTER UPDATE ON game_link BEGIN | |
| 39 | DELETE FROM account_link WHERE OLD.uuid != '' AND account = OLD.user_id AND kind = 'minecraft' AND outside_id = lower(OLD.uuid) | |
| 40 | AND NOT EXISTS (SELECT 1 FROM game_link WHERE user_id = OLD.user_id AND lower(uuid) = lower(OLD.uuid)); | |
| 41 | INSERT OR IGNORE INTO account_link (account, kind, outside_id, proved_at, how) | |
| 42 | SELECT NEW.user_id, 'minecraft', lower(NEW.uuid), NEW.created_at, 'game-door' WHERE NEW.uuid != ''; | |
| 43 | END; | |
| 44 | `; |
src/core/proven_identities.ts +2 −1
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | * Plain data in, plain data out; the codes and the network are the callers'. |
| 11 | 11 | */ |
| 12 | 12 | import type { Database } from "bun:sqlite"; |
| 13 | import { linksOfKind } from "./account_link.ts"; | |
| 13 | 14 | |
| 14 | 15 | /** |
| 15 | 16 | * SQL for when the account aliased `u` proved its own email, or NULL: its proven email connection |
| @@ -115,7 +116,7 @@ export const identitiesOf = (db: Database, userId: string): Identity[] => listOf | ||
| 115 | 116 | |
| 116 | 117 | /** Whether `userId` has a proven identity of `kind`: the one read behind `emailVerified`. */ |
| 117 | 118 | export const provenAs = (db: Database, userId: string, kind: IdentityKind): boolean => |
| 118 | kind === "email" ? emailIdentity(db, userId)?.proven === true : minecraftIdentities(db, userId).some((i) => i.proven); | |
| 119 | kind === "email" ? emailIdentity(db, userId)?.proven === true : linksOfKind(db, userId, kind).length > 0; | |
| 119 | 120 | |
| 120 | 121 | /** |
| 121 | 122 | * Remove the account's email from its list (#18160): the address becomes its stand-in |
src/games/chat.ts +1
| @@ -408,6 +408,7 @@ function one( | ||
| 408 | 408 | // The same link proves the account's Minecraft connection by this name, if it keeps one |
| 409 | 409 | // (#18221). |
| 410 | 410 | proveMinecraft(db, code.user_id, player, now); |
| 411 | // The account's link to that UUID (#18490) is written by the game_link triggers. | |
| 411 | 412 | const who = getUserById(db, code.user_id); |
| 412 | 413 | audit(app, box, at, code.user_id, "link", "link ****", "linked", now); |
| 413 | 414 | return say( |
src/kinds/kind.ts +24 −5
| @@ -4,12 +4,14 @@ | ||
| 4 | 4 | * a kind's directory, so what is specific to one game (Minecraft's compose design, its console, |
| 5 | 5 | * RCON) stays in `src/kinds/<name>/`. |
| 6 | 6 | * |
| 7 | * The compose design and the console adapter live behind it. The people adapter, identity prover | |
| 8 | * and metrics probe are declared here and still stubbed by each kind: the code behind them is in | |
| 9 | * `src/games` until the next move. | |
| 7 | * The compose design and the console adapter live behind it. The identity prover is the first | |
| 8 | * of the three declared parts to move; the people adapter and metrics probe are still stubbed by | |
| 9 | * each kind: the code behind them is in `src/games` until the next move. | |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import type { Database } from "bun:sqlite"; | |
| 12 | 13 | import type { App } from "../app.ts"; |
| 14 | import type { AccountLink } from "../core/account_link.ts"; | |
| 13 | 15 | import type { DnsWant, MachineWant } from "../cloud/plan.ts"; |
| 14 | 16 | import type { Result } from "../kernel/result.ts"; |
| 15 | 17 | |
| @@ -70,8 +72,25 @@ export interface Stub { | ||
| 70 | 72 | |
| 71 | 73 | /** Part 3: who is on a server, and who may be (whitelist, ops). */ |
| 72 | 74 | export type PeopleAdapter = Stub; |
| 73 | /** Part 4: proof that a person owns a name in the game. */ | |
| 74 | export type IdentityProver = Stub; | |
| 75 | /** | |
| 76 | * Part 4: proof that a person owns a name in the game (#18490). The proof is one row of | |
| 77 | * `account_link` keyed by this kind; the prover says what the kind's outside id is and writes and | |
| 78 | * reads the rows. | |
| 79 | */ | |
| 80 | export interface IdentityProver { | |
| 81 | /** The `kind` of the rows this prover keeps. */ | |
| 82 | readonly kind: string; | |
| 83 | /** The outside id as kept, or undefined when it is not one of this kind's. */ | |
| 84 | clean(raw: unknown): string | undefined; | |
| 85 | /** | |
| 86 | * Record that `account` proved `outsideId` now; what is wrong when it is not an id of this kind. | |
| 87 | */ | |
| 88 | prove(db: Database, account: string, outsideId: unknown, now: number): Result<AccountLink, string>; | |
| 89 | /** The account's proved links of this kind, oldest first. */ | |
| 90 | proved(db: Database, account: string): AccountLink[]; | |
| 91 | /** The accounts that proved this outside id. */ | |
| 92 | owners(db: Database, outsideId: unknown): string[]; | |
| 93 | } | |
| 75 | 94 | /** Part 5: what a running server reports about itself. */ |
| 76 | 95 | export type MetricsProbe = Stub; |
| 77 | 96 |
src/kinds/minecraft/index.ts +4 −3
| @@ -1,17 +1,18 @@ | ||
| 1 | 1 | /** |
| 2 | * The Minecraft kind: its compose design and console here; people, identity and metrics still in | |
| 3 | * `src/games`. | |
| 2 | * The Minecraft kind: its compose design, console and identity prover here; people and metrics | |
| 3 | * still in `src/games`. | |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | import type { Kind } from "../kind.ts"; |
| 7 | 7 | import { consoleHtml, consoleText, crashOf, shareOnMclogs } from "./console.ts"; |
| 8 | 8 | import { minecraftDesign } from "./compose.ts"; |
| 9 | import { minecraftProver } from "./prover.ts"; | |
| 9 | 10 | |
| 10 | 11 | export const minecraft: Kind = { |
| 11 | 12 | name: "minecraft", |
| 12 | 13 | compose: { design: minecraftDesign }, |
| 13 | 14 | console: { html: consoleHtml, text: consoleText, crash: crashOf, share: shareOnMclogs }, |
| 14 | 15 | people: { stub: true, livesIn: "src/games/people.ts" }, |
| 15 | identity: { stub: true, livesIn: "src/core/proven_identities.ts" }, | |
| 16 | identity: minecraftProver, | |
| 16 | 17 | metrics: { stub: true, livesIn: "src/games/metrics.ts" }, |
| 17 | 18 | }; |
src/kinds/minecraft/prover.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | /** | |
| 2 | * Minecraft's identity prover (#18490): a player is proved by the UUID the game server itself | |
| 3 | * lists when the person types the forge's code in game, so the outside id is that UUID. | |
| 4 | */ | |
| 5 | ||
| 6 | import type { Database } from "bun:sqlite"; | |
| 7 | import { addLink, linksOfKind, ownersOf, type AccountLink } from "../../core/account_link.ts"; | |
| 8 | import { err, ok, type Result } from "../../kernel/result.ts"; | |
| 9 | import type { IdentityProver } from "../kind.ts"; | |
| 10 | ||
| 11 | const KIND = "minecraft"; | |
| 12 | export const GAME_DOOR = "game-door"; | |
| 13 | ||
| 14 | /** A UUID as kept: lowercase, with or without dashes; undefined when it is not one. */ | |
| 15 | export function cleanUuid(raw: unknown): string | undefined { | |
| 16 | if (typeof raw !== "string") return undefined; | |
| 17 | const u = raw.trim().toLowerCase(); | |
| 18 | return /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/.test(u) ? u : undefined; | |
| 19 | } | |
| 20 | ||
| 21 | function prove(db: Database, account: string, outsideId: unknown, now: number): Result<AccountLink, string> { | |
| 22 | const uuid = cleanUuid(outsideId); | |
| 23 | if (uuid === undefined) return err("not a Minecraft UUID"); | |
| 24 | const link: AccountLink = { account, kind: KIND, outsideId: uuid, provedAt: new Date(now).toISOString(), how: GAME_DOOR }; | |
| 25 | addLink(db, link); | |
| 26 | return ok(linksOfKind(db, account, KIND).find((l) => l.outsideId === uuid) ?? link); | |
| 27 | } | |
| 28 | ||
| 29 | export const minecraftProver: IdentityProver = { | |
| 30 | kind: KIND, | |
| 31 | clean: cleanUuid, | |
| 32 | prove, | |
| 33 | proved: (db, account) => linksOfKind(db, account, KIND), | |
| 34 | owners: (db, outsideId) => ownersOf(db, KIND, cleanUuid(outsideId) ?? ""), | |
| 35 | }; |
test/account-link.test.ts added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | /** | |
| 2 | * An account's link to an outside identity (#18490, step 1 of 2): one table keyed by kind beside | |
| 3 | * game_link, kept in step by triggers so the previous release runs on it; Minecraft's prover is | |
| 4 | * the first. | |
| 5 | */ | |
| 6 | import { Database } from "bun:sqlite"; | |
| 7 | import { describe, expect, test } from "bun:test"; | |
| 8 | import { migrationFiles } from "../src/core/schema.ts"; | |
| 9 | import { migrate } from "../src/kernel/migrate.ts"; | |
| 10 | import { defaultKind } from "../src/kinds/index.ts"; | |
| 11 | ||
| 12 | const NEW = "20261206183000-account-link"; | |
| 13 | const UUID = "069a79f4-44e9-4726-a5be-fca90e38aaf5"; | |
| 14 | const before = migrationFiles.filter((m) => m.name !== NEW); | |
| 15 | const links = (db: Database) => db.query("SELECT account, kind, outside_id, how FROM account_link ORDER BY outside_id").all(); | |
| 16 | ||
| 17 | function seeded(db: Database): void { | |
| 18 | for (const id of ["u1", "u2"]) | |
| 19 | db.query("INSERT INTO user (id, username, email, password_hash, created_at) VALUES (?1, ?1, ?1 || '@x.test', 'h', 'now')").run(id); | |
| 20 | } | |
| 21 | const link = (db: Database, box: string, player: string, user: string, uuid: string) => | |
| 22 | db.query("INSERT INTO game_link (box_id, player, user_id, created_at, uuid) VALUES (?1, ?2, ?3, '2026-12-01', ?4)").run(box, player, user, uuid); | |
| 23 | ||
| 24 | describe("a snapshot from before this change", () => { | |
| 25 | test("restores and boots: the new migration is the only one applied, and the old links are carried over", () => { | |
| 26 | const db = new Database(":memory:"); | |
| 27 | migrate(db, before); | |
| 28 | seeded(db); | |
| 29 | link(db, "b1", "Alex", "u1", UUID.toUpperCase()); | |
| 30 | link(db, "b2", "Alex", "u1", UUID); | |
| 31 | link(db, "b1", "Old", "u2", ""); | |
| 32 | expect(db.query("SELECT 1 FROM sqlite_master WHERE name = 'account_link'").get()).toBeNull(); | |
| 33 | expect(migrate(db, migrationFiles).applied).toEqual([NEW]); | |
| 34 | expect(links(db)).toEqual([{ account: "u1", kind: "minecraft", outside_id: UUID, how: "game-door" }]); | |
| 35 | expect(db.query("SELECT COUNT(*) AS n FROM game_link").get()).toEqual({ n: 3 }); | |
| 36 | }); | |
| 37 | }); | |
| 38 | ||
| 39 | describe("the previous release's writes to game_link", () => { | |
| 40 | test("show in account_link, and a link goes when its last box link does", () => { | |
| 41 | const db = new Database(":memory:"); | |
| 42 | db.run("PRAGMA foreign_keys = ON"); | |
| 43 | migrate(db, migrationFiles); | |
| 44 | seeded(db); | |
| 45 | link(db, "b1", "Alex", "u1", UUID); | |
| 46 | link(db, "b2", "Alex", "u1", UUID); | |
| 47 | link(db, "b3", "Nobody", "u2", ""); | |
| 48 | expect(links(db)).toHaveLength(1); | |
| 49 | db.query("DELETE FROM game_link WHERE box_id = 'b1'").run(); | |
| 50 | expect(links(db)).toHaveLength(1); | |
| 51 | db.query("UPDATE game_link SET uuid = '' WHERE box_id = 'b2'").run(); | |
| 52 | expect(links(db)).toEqual([]); | |
| 53 | db.query("UPDATE game_link SET uuid = ?1, user_id = 'u2' WHERE box_id = 'b3'").run(UUID); | |
| 54 | expect(links(db)).toEqual([{ account: "u2", kind: "minecraft", outside_id: UUID, how: "game-door" }]); | |
| 55 | db.query("DELETE FROM user WHERE id = 'u2'").run(); | |
| 56 | expect(links(db)).toEqual([]); | |
| 57 | }); | |
| 58 | }); | |
| 59 | ||
| 60 | describe("Minecraft's prover", () => { | |
| 61 | test("proves a UUID once, lists it, finds its owners, and refuses what is not a UUID", () => { | |
| 62 | const db = new Database(":memory:"); | |
| 63 | migrate(db, migrationFiles); | |
| 64 | seeded(db); | |
| 65 | const p = defaultKind().identity; | |
| 66 | expect(p.kind).toBe("minecraft"); | |
| 67 | expect(p.prove(db, "u1", "Alex", 1).ok).toBe(false); | |
| 68 | expect(p.prove(db, "u1", UUID.toUpperCase(), 1).ok).toBe(true); | |
| 69 | expect(p.prove(db, "u1", UUID, 2).ok).toBe(true); | |
| 70 | expect(p.proved(db, "u1").map((l) => [l.outsideId, l.how])).toEqual([[UUID, "game-door"]]); | |
| 71 | expect(p.proved(db, "u2")).toEqual([]); | |
| 72 | expect(p.owners(db, UUID)).toEqual(["u1"]); | |
| 73 | expect(p.owners(db, "nope")).toEqual([]); | |
| 74 | }); | |
| 75 | }); | |
| 76 | ||
| 77 | describe("INSERT OR REPLACE on game_link, as chat.ts writes it", () => { | |
| 78 | test("a re-link with a new UUID and another account leaves no stale row", () => { | |
| 79 | const db = new Database(":memory:"); | |
| 80 | migrate(db, migrationFiles); | |
| 81 | seeded(db); | |
| 82 | const other = "11111111-2222-4333-8444-555555555555"; | |
| 83 | const replace = (user: string, uuid: string) => | |
| 84 | db.query("INSERT OR REPLACE INTO game_link (box_id, player, user_id, uuid, created_at) VALUES ('b1', 'Alex', ?1, ?2, '2026-12-02')").run(user, uuid); | |
| 85 | replace("u1", UUID); | |
| 86 | link(db, "b2", "Alex", "u1", UUID); | |
| 87 | replace("u1", other); | |
| 88 | expect(links(db).map((l) => (l as { outside_id: string }).outside_id).sort()).toEqual([UUID, other].sort()); | |
| 89 | replace("u2", UUID); | |
| 90 | expect(links(db)).toEqual([ | |
| 91 | { account: "u1", kind: "minecraft", outside_id: UUID, how: "game-door" }, | |
| 92 | { account: "u2", kind: "minecraft", outside_id: UUID, how: "game-door" }, | |
| 93 | ]); | |
| 94 | }); | |
| 95 | }); |
test/kinds.test.ts +2 −2
| @@ -13,11 +13,11 @@ test("a kind is looked up by its name; an unknown name has none", () => { | ||
| 13 | 13 | expect(defaultKind()).toBe(kindNamed("minecraft")!); |
| 14 | 14 | }); |
| 15 | 15 | |
| 16 | test("a kind has all five parts, the unmoved three declared as stubs", () => { | |
| 16 | test("a kind has all five parts, the unmoved two declared as stubs", () => { | |
| 17 | 17 | const k = kindNamed("minecraft")!; |
| 18 | 18 | expect(typeof k.compose.design).toBe("function"); |
| 19 | 19 | expect(typeof k.console.html).toBe("function"); |
| 20 | for (const part of [k.people, k.identity, k.metrics]) expect(part.stub).toBe(true); | |
| 20 | for (const part of [k.people, k.metrics]) expect(part.stub).toBe(true); | |
| 21 | 21 | }); |
| 22 | 22 | |
| 23 | 23 | test("the compose design goes through the kind", () => { |
new above
offline
DELETE triggers do not fire for rows removed by INSERT OR REPLACE (recursive_triggers is off), and chat.ts writes game_link that way. Add a BEFORE INSERT trigger that clears the account_link row of the game_link row about to be replaced, and test it with a real INSERT OR REPLACE changing uuid and user_id.