Cotton code
75 lines · 3.9 KB · TypeScriptRaw
/** * Bearer tokens for Cotton MCP: rows of the personal object's `mcp_token` * table holding the token's name, scopes and sha256 — never the token. The * shape is the Rust one (`ctn_<databaseId>_<base64url 32 bytes>`, the hash of * the whole string), so a caller tells them apart the same way. * * SCOPES: `owner` (everything, the vault's reveal included), `coordinator` * (read, write and machines), `read`, `write`, `machines`, and at most one * `lane:<name>` (reads, and writes only its session * log). The owner's first token is minted by signing in as the owner phone and * acting `mint_token`; an owner token mints and revokes the rest the same way. */import { randomToken, sha256Hex } from "./http.ts";import type { McpCaller } from "./mcp/context.ts";import type { StoredRow } from "./store.ts";import { allowOf } from "./key_allow.ts";export const TOKEN_TABLE = "mcp_token";const WORDS = new Set(["owner", "coordinator", "read", "write", "machines"]);const LANE = /^lane:([A-Za-z0-9_-]{1,60})$/;/** The scope words a mint may ask for, or the sentence refusing them. */export function checkScopes(scopes: unknown): string[] | string {  if (!Array.isArray(scopes) || !scopes.length) return "scopes must be a list of words: owner, coordinator, read, write, machines, lane:<name>";  const out: string[] = [];  let lanes = 0;  for (const s of scopes) {    if (typeof s !== "string" || !(WORDS.has(s) || LANE.test(s))) return `unknown scope ${JSON.stringify(s)}`;    if (LANE.test(s)) lanes++;    if (!out.includes(s)) out.push(s);  }  if (lanes > 1) return "a token is at most one lane";  if (lanes && (out.includes("owner") || out.includes("coordinator") || out.includes("write"))) return "a lane token reads and passes its own doors; it holds no owner, coordinator or write";  return out;}export async function mintSecret(databaseId: string): Promise<{ token: string; hash: string }> {  const token = `ctn_${databaseId}_${randomToken(32)}`;  return { token, hash: await sha256Hex(token) };}/** A machine's short host name as a mint gives it ("mac-c", "ci-box"): plain, short, or refused by name. */export function checkMachine(machine: unknown): string | null | { refused: string } {  if (machine === undefined || machine === null || machine === "") return null;  if (typeof machine !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._-]{0,39}$/.test(machine)) return { refused: "machine is a short host name: letters, digits, dots, dashes, at most 40" };  return machine;}/** * What asks, for an unlock card (owner, 23 Nov 2026: the card named the build session by its lane name, which tells a person * nothing): `code`, a token minted by hand or for a lane (no OAuth connection): Claude Code, on its `machine` when * the mint said one; `app`, an assistant connected by sign-in, named by its client. The phone's core says it. */export function askerOf(store: { get(table: string, id: string): StoredRow | null }, tokenId: string): { asker: "code" | "app"; machine?: string } {  const row = store.get(TOKEN_TABLE, tokenId);  const connection = row?.data.connection_id;  if (typeof connection === "string" && connection) return { asker: "app" };  const machine = row?.data.machine;  return typeof machine === "string" && machine ? { asker: "code", machine } : { asker: "code" };}/** The caller a token row names, or the sentence it is refused with (Rust answers every dead token "unknown token"). */export function callerOf(row: StoredRow | null, now: string): McpCaller | null {  if (!row) return null;  const d = row.data;  if (typeof d.revoked_at === "string" && d.revoked_at) return null;  if (typeof d.expires_at === "string" && d.expires_at && d.expires_at <= now) return null;  const scopes = new Set(Array.isArray(d.scopes) ? (d.scopes as string[]) : []);  let lane: string | null = null;  for (const s of scopes) {    const m = LANE.exec(s);    if (m) lane = m[1]!;  }  return { tokenId: row.id, name: String(d.name ?? ""), scopes, lane, allow: allowOf(d) };}