PY1.0 acceptance (C8 delta check on main f919636; Wave E Y-0 to Y-5 merged):
1. Denylist .3 (gtm/policy, published by this package) adds portfolio-ott-drm
and portfolio-ott-comparison (DRM, Netflix-style, your own Netflix beyond
Menivor), addon-unlaunched (ADDON_LIVE), rally-capacity-purchase
(RALLY_CAPACITY_PURCHASE_LIVE) and at-cost (allowed only next to a live
price source). Re-vendored byte for byte; every new rule has failing
fixtures (policy/test/fixtures/denylist-3-new-phrases.json); .2 fixtures kept.
2. apps/console/app/api/v1/bridge/scribase/{orgs,claims,proof}: signed
(Standard Webhooks, RAILYARD_SCRIBASE_BRIDGE_SECRET), replay-proof calls from
Scribase control-api. The first call creates the Railyard org bound to the
Scribase org id (deterministic owner id, no second sign-up) and mirrors the
add-on into the entitlement (plan "addon"); a lapsed add-on pauses the org
and keeps its data. Keys go only to the Scribase CLI: a claim is a one-time,
pre-approved device code (120 s) exchanged at /api/v1/device/token; a grep
test keeps every bridge handler free of stdout, stderr and log writes.
3. proof answers {slot, state, live, proof:{kind,id,at}, next}; slots whose
package has not landed answer "arriving".
Contract: tests/contract/fixtures/scribase-bridge/contract.json, run by
tests/contract/scribase-bridge.test.ts; the Scribase carve-out tests the same file.
Migration: apps/console/db/migrations/0002_scribase_bridge.sql (idempotent;
applied twice to a scratch local cluster only by test/migration.test.ts).
Env: RAILYARD_SCRIBASE_BRIDGE_SECRET (secret; unset keeps the bridge off).
Tests: console npm test 48/48, tests/contract 2/2, policy 11/11, cli 9/9, mcp 9/9,
tsc --noEmit clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
3.8 KiB
TypeScript
89 lines
3.8 KiB
TypeScript
// CLI sign-in (device flow, RFC 8628 shape) and org-scoped bearer tokens.
|
|
// Only SHA-256 hashes of device codes and tokens are stored. A token is
|
|
// shown once, to the CLI that polled for it, never on a web page.
|
|
|
|
import { randomToken, sha256Hex } from "./crypto.ts";
|
|
import type { Repo } from "./repo.ts";
|
|
|
|
export const DEVICE_TTL_SECONDS = 600;
|
|
export const POLL_INTERVAL_SECONDS = 3;
|
|
const USER_CODE_ALPHABET = "BCDFGHJKLMNPQRSTVWXZ";
|
|
|
|
function userCode(): string {
|
|
const buf = new Uint8Array(8);
|
|
crypto.getRandomValues(buf);
|
|
const chars = [...buf].map((b) => USER_CODE_ALPHABET[b % USER_CODE_ALPHABET.length]);
|
|
return `${chars.slice(0, 4).join("")}-${chars.slice(4).join("")}`;
|
|
}
|
|
|
|
export async function startDevice(repo: Repo, consoleUrl: string, now: Date) {
|
|
const deviceCode = randomToken(32);
|
|
const code = userCode();
|
|
await repo.createDeviceCode({
|
|
device_code_hash: await sha256Hex(deviceCode),
|
|
user_code: code,
|
|
org_id: null,
|
|
user_id: null,
|
|
approved_at: null,
|
|
consumed_at: null,
|
|
expires_at: new Date(now.getTime() + DEVICE_TTL_SECONDS * 1000).toISOString(),
|
|
});
|
|
return {
|
|
device_code: deviceCode,
|
|
user_code: code,
|
|
verification_uri: `${consoleUrl}/device`,
|
|
verification_uri_complete: `${consoleUrl}/device?code=${code}`,
|
|
expires_in: DEVICE_TTL_SECONDS,
|
|
interval: POLL_INTERVAL_SECONDS,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* A device code that is approved at birth, for a server that already knows the
|
|
* org (the Scribase bridge, PY1.0). The caller hands the raw code to the CLI
|
|
* that will poll `/api/v1/device/token` once; only its hash is stored.
|
|
*/
|
|
export async function createApprovedDevice(repo: Repo, orgId: string, userId: string, ttlSeconds: number, now: Date): Promise<string> {
|
|
const deviceCode = randomToken(32);
|
|
await repo.createDeviceCode({
|
|
device_code_hash: await sha256Hex(deviceCode),
|
|
user_code: userCode(),
|
|
org_id: orgId,
|
|
user_id: userId,
|
|
approved_at: now.toISOString(),
|
|
consumed_at: null,
|
|
expires_at: new Date(now.getTime() + ttlSeconds * 1000).toISOString(),
|
|
});
|
|
return deviceCode;
|
|
}
|
|
|
|
export async function approveDevice(repo: Repo, rawCode: string, orgId: string, userId: string, now: Date): Promise<boolean> {
|
|
const code = rawCode.trim().toUpperCase();
|
|
if (!/^[A-Z]{4}-[A-Z]{4}$/.test(code)) return false;
|
|
return repo.approveDevice(code, orgId, userId, now.toISOString());
|
|
}
|
|
|
|
export type PollResult =
|
|
| { status: 200; body: { access_token: string; token_type: "bearer"; org_id: string } }
|
|
| { status: 400; body: { error: "authorization_pending" | "expired_token" | "invalid_grant" } };
|
|
|
|
export async function pollDevice(repo: Repo, deviceCode: string, now: Date): Promise<PollResult> {
|
|
const hash = await sha256Hex(deviceCode);
|
|
const dc = await repo.findDeviceByHash(hash);
|
|
if (!dc || dc.consumed_at) return { status: 400, body: { error: "invalid_grant" } };
|
|
if (Date.parse(dc.expires_at) <= now.getTime()) return { status: 400, body: { error: "expired_token" } };
|
|
if (!dc.approved_at || !dc.org_id || !dc.user_id) return { status: 400, body: { error: "authorization_pending" } };
|
|
if (!(await repo.consumeDevice(hash, now.toISOString()))) return { status: 400, body: { error: "invalid_grant" } };
|
|
const token = `ry_${randomToken(32)}`;
|
|
await repo.createToken({ org_id: dc.org_id, user_id: dc.user_id, token_hash: await sha256Hex(token), label: "cli" });
|
|
return { status: 200, body: { access_token: token, token_type: "bearer", org_id: dc.org_id } };
|
|
}
|
|
|
|
/** The org a bearer token acts for, or null. A token only ever reads its own org. */
|
|
export async function orgForBearer(repo: Repo, authorization: string | null): Promise<{ orgId: string; userId: string } | null> {
|
|
const m = /^Bearer\s+(ry_[A-Za-z0-9_-]{20,})$/.exec(authorization ?? "");
|
|
if (!m) return null;
|
|
const t = await repo.findTokenByHash(await sha256Hex(m[1]));
|
|
return t ? { orgId: t.org_id, userId: t.user_id } : null;
|
|
}
|