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

opened this

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

  • Merged. mason/18490-an-account-s-link-to-an-outside-identity was merged into main as 718f6d1.

Review

Sign in to review this change.
10 files changed +247 −11
src/core/proven_identities.ts +2 −1
@@ -10,6 +10,7 @@
1010 * Plain data in, plain data out; the codes and the network are the callers'.
1111 */
1212 import type { Database } from "bun:sqlite";
13 import { linksOfKind } from "./account_link.ts";
1314
1415 /**
1516 * 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
115116
116117 /** Whether `userId` has a proven identity of `kind`: the one read behind `emailVerified`. */
117118 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;
119120
120121 /**
121122 * 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(
408408 // The same link proves the account's Minecraft connection by this name, if it keeps one
409409 // (#18221).
410410 proveMinecraft(db, code.user_id, player, now);
411 // The account's link to that UUID (#18490) is written by the game_link triggers.
411412 const who = getUserById(db, code.user_id);
412413 audit(app, box, at, code.user_id, "link", "link ****", "linked", now);
outdated

Redundant with the insert trigger (same row, INSERT OR IGNORE), and the Result is ignored. Keep one writer. Note the game_link write above is INSERT OR REPLACE, which bypasses the delete trigger, so a re-link leaves a stale account_link row.

413414 return say(
src/kinds/kind.ts +24 −5
@@ -4,12 +4,14 @@
44 * a kind's directory, so what is specific to one game (Minecraft's compose design, its console,
55 * RCON) stays in `src/kinds/<name>/`.
66 *
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.
1010 */
1111
12 import type { Database } from "bun:sqlite";
1213 import type { App } from "../app.ts";
14 import type { AccountLink } from "../core/account_link.ts";
1315 import type { DnsWant, MachineWant } from "../cloud/plan.ts";
1416 import type { Result } from "../kernel/result.ts";
1517
@@ -70,8 +72,25 @@ export interface Stub {
7072
7173 /** Part 3: who is on a server, and who may be (whitelist, ops). */
7274 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 }
7594 /** Part 5: what a running server reports about itself. */
7695 export type MetricsProbe = Stub;
7796
src/kinds/minecraft/index.ts +4 −3
@@ -1,17 +1,18 @@
11 /**
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`.
44 */
55
66 import type { Kind } from "../kind.ts";
77 import { consoleHtml, consoleText, crashOf, shareOnMclogs } from "./console.ts";
88 import { minecraftDesign } from "./compose.ts";
9 import { minecraftProver } from "./prover.ts";
910
1011 export const minecraft: Kind = {
1112 name: "minecraft",
1213 compose: { design: minecraftDesign },
1314 console: { html: consoleHtml, text: consoleText, crash: crashOf, share: shareOnMclogs },
1415 people: { stub: true, livesIn: "src/games/people.ts" },
15 identity: { stub: true, livesIn: "src/core/proven_identities.ts" },
16 identity: minecraftProver,
1617 metrics: { stub: true, livesIn: "src/games/metrics.ts" },
1718 };
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/kinds.test.ts +2 −2
@@ -13,11 +13,11 @@ test("a kind is looked up by its name; an unknown name has none", () => {
1313 expect(defaultKind()).toBe(kindNamed("minecraft")!);
1414 });
1515
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", () => {
1717 const k = kindNamed("minecraft")!;
1818 expect(typeof k.compose.design).toBe("function");
1919 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);
2121 });
2222
2323 test("the compose design goes through the kind", () => {

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs