feat(daemon)!: phone-first binding and a two-step pairing flow
Implements ADR 0001 (docs/adr/0001-phone-first-pairing.md): the console is
meant to be opened from a phone, so reachability stops being a mode and the
pairing link becomes the only access control that matters.
Binding / CORS
- `PI2_HOST` defaults to `0.0.0.0`; `PI2_ACCESS_MODE` (local|lan|tailscale) is
deleted, together with its parser, `AccessMode` type and LAN probe.
- `src/core/network.ts` is the single source of truth for reachable addresses:
kind classification, ordering (public → lan → other → tailscale → loopback,
physical NICs first, link-local skipped), bind-aware `networkBases()`,
`primaryBase()` and `localOrigins()`. Config, the pairing route, doctor and
the CLI all consume it, so the CORS allowlist always matches the pairing URL.
- `/api/v1/info` and `daemon.json` report `host` instead of `access`; doctor's
`access` check becomes `bind` (loopback ok, wildcard warns unless an `https:`
public URL is configured).
Pairing
- `POST /api/v1/pair` only mints, requires a token, and returns the code, the
chosen URL, the expiry, the nonce, the QR matrix and every usable base.
`base` may pin one of those bases; anything else is a 400.
- `POST /api/v1/pair/redeem` is the public half, rate limited per client
(5/min) and globally (50/h) → 429 `E_RATE_LIMITED` with `retry-after`.
- `GET /pair/:code` answers 302 to `/connect?code=…&auto=1`, so a scanned link
needs no JavaScript to reach the console.
- `src/core/qr.ts` (moved out of the CLI) is the one encoder: byte mode, EC
level M, versions 1–10, rows without a quiet zone, shared by the HTTP mint
response and the terminal renderer.
- `pi2 pairing` prints a human report plus a half-block terminal QR and keeps
`--base`; `--json` and `--public-url` are gone (the Neovim client calls the
HTTP mint directly).
Device scopes
- `Scope`, `SCOPES`, `DEFAULT_SCOPES`, `DeviceRecord.scopes` and the `scopes`
field in device/hello payloads are removed: nothing enforced them, and a
paired device now holds the same rights as the local browser. A permission
model, if wanted, gets its own ADR.
Breaking: `PI2_ACCESS_MODE`, `info.access`, `pi2 status --json`'s `access`,
`pi2 pairing --json`/`--public-url`, `POST /api/v1/pair {code}`, and the
`scopes` field. All in-repo callers and the e2e drivers are updated; the plugin
side (`:PiWebAccess`, `env.PI2_ACCESS_MODE`, text-format parsing) follows in
pi2.nvim.
Verification: root typecheck clean; 369/370 tests (the one failure is the
pre-existing supervisor timing flake, 24/24 in isolation ×3); `smoke.sh` green;
web suite 299/299; visual matrix 24/24.
This commit is contained in:
1 parent
3789394c85
commit
a57c874095
34 files changed
+2157
-488
No files matched your search
+14
-23
@@ -384,7 +384,6 @@ class Ctx {
|
||||
{ name: "pi2-endpoint", value: BASE },
|
||||
// Raw token (web/src/lib/api.ts strips an optional `Bearer ` prefix).
|
||||
{ name: "pi2-device-token", value: DEVICE_TOKEN },
|
||||
{ name: "pi2-device-scopes", value: JSON.stringify(["read", "steer", "apply"]) },
|
||||
{ name: "pi2-device", value: "Playwright" },
|
||||
{ name: "pi2-connected-at", value: String(Date.now()) },
|
||||
...Object.entries(localExtra).map(([name, value]) => ({ name, value: String(value) })),
|
||||
@@ -947,7 +946,7 @@ async function fillManualForm(page, code, device = "Playwright") {
|
||||
await page.locator('[data-testid="connect-submit"]').click();
|
||||
}
|
||||
|
||||
scenario("B2", "Manual pairing: host/port/pair-code/device → POST /api/v1/pair + redirect (wrong code → inline error)", async (ctx) => {
|
||||
scenario("B2", "Manual pairing: host/port/pair-code/device → POST /api/v1/pair/redeem + redirect (wrong code → inline error)", async (ctx) => {
|
||||
const { page } = await ctx.openPage("unpaired");
|
||||
await page.goto(`${BASE}/connect?next=%2Fworkspaces`, { waitUntil: "domcontentloaded" });
|
||||
await page.locator('[data-testid="connect-pair-code"]').waitFor({ state: "visible", timeout: 15000 });
|
||||
@@ -975,13 +974,13 @@ scenario("B2", "Manual pairing: host/port/pair-code/device → POST /api/v1/pair
|
||||
return null;
|
||||
}, 25000);
|
||||
|
||||
const pairCalls = ctx.apiCalls.filter((call) => call.method === "POST" && call.path === "/api/v1/pair");
|
||||
const pairCalls = ctx.apiCalls.filter((call) => call.method === "POST" && call.path === "/api/v1/pair/redeem");
|
||||
ctx.log(`redeem calls: ${JSON.stringify(pairCalls)}`);
|
||||
ctx.check("the redeem posts the public POST /api/v1/pair", pairCalls.length >= 1, {
|
||||
expected: "POST /api/v1/pair {code,deviceName,kind}",
|
||||
ctx.check("the redeem posts the public POST /api/v1/pair/redeem", pairCalls.length >= 1, {
|
||||
expected: "POST /api/v1/pair/redeem {code,deviceName,kind}",
|
||||
observed: { pairCalls, cmdMethods: ctx.cmdRequests.map((request) => request.method) },
|
||||
});
|
||||
ctx.check("POST /api/v1/pair answered 200", pairCalls.some((call) => call.status === 200), {
|
||||
ctx.check("POST /api/v1/pair/redeem answered 200", pairCalls.some((call) => call.status === 200), {
|
||||
expected: 200,
|
||||
observed: pairCalls,
|
||||
});
|
||||
@@ -1000,11 +999,10 @@ scenario("B2", "Manual pairing: host/port/pair-code/device → POST /api/v1/pair
|
||||
const storage = await page.evaluate(() => ({
|
||||
connection: localStorage.getItem("pi2-connection"),
|
||||
token: localStorage.getItem("pi2-device-token"),
|
||||
scopes: localStorage.getItem("pi2-device-scopes"),
|
||||
endpoint: localStorage.getItem("pi2-endpoint"),
|
||||
}));
|
||||
const connWrites = await page.evaluate(() =>
|
||||
(window.__pi2Writes ?? []).filter((write) => ["pi2-connection", "pi2-device-token", "pi2-device-scopes"].includes(write.key)),
|
||||
(window.__pi2Writes ?? []).filter((write) => ["pi2-connection", "pi2-device-token"].includes(write.key)),
|
||||
);
|
||||
ctx.note(`pi2-* storage writes: ${short(connWrites, 700)}`);
|
||||
ctx.note(`navigations: ${short(ctx.navigations, 400)}`);
|
||||
@@ -1014,7 +1012,6 @@ scenario("B2", "Manual pairing: host/port/pair-code/device → POST /api/v1/pair
|
||||
/^Bearer\s+/i,
|
||||
"",
|
||||
);
|
||||
const mintedScopes = (connWrites.find((write) => write.key === "pi2-device-scopes" && write.op === "set")?.value ?? "").trim();
|
||||
|
||||
const wsErrors = socketErrors(ctx.wsFrames);
|
||||
const wsAuthErrors = wsErrors.filter((entry) => entry.code === "E_UNAUTHORIZED");
|
||||
@@ -1036,22 +1033,17 @@ scenario("B2", "Manual pairing: host/port/pair-code/device → POST /api/v1/pair
|
||||
expected: "paired",
|
||||
observed: { connection: storage.connection, setToken: Boolean(mintedRaw), writes: connWrites, navigations: ctx.navigations },
|
||||
cause:
|
||||
"the flow writes pi2-connection=paired (see the write log), then ~100 ms later the app removes the token + scopes and writes 'unpaired' " +
|
||||
"the flow writes pi2-connection=paired (see the write log), then ~100 ms later the app removes the token and writes 'unpaired' " +
|
||||
"because the socket auth of that same token fails with E_UNAUTHORIZED",
|
||||
});
|
||||
ctx.softCheck("localStorage pi2-device-token is still set", Boolean(storage.token), {
|
||||
expected: "<device token>",
|
||||
observed: storage.token ? "set" : { removedAfterMs: connWrites.at(-1)?.at, writes: connWrites },
|
||||
});
|
||||
let scopes = null;
|
||||
try {
|
||||
scopes = JSON.parse(storage.scopes ?? mintedScopes ?? "null");
|
||||
} catch {
|
||||
scopes = null;
|
||||
}
|
||||
ctx.softCheck("the paired scopes are a non-empty array", Array.isArray(scopes) && scopes.length > 0, {
|
||||
expected: '["read", …]',
|
||||
observed: storage.scopes ?? mintedScopes,
|
||||
const legacyScopes = await page.evaluate(() => localStorage.getItem("pi2-device-scopes"));
|
||||
ctx.softCheck("the browser writes no legacy pi2-device-scopes key (ADR 0001)", legacyScopes === null, {
|
||||
expected: null,
|
||||
observed: { legacyScopes },
|
||||
});
|
||||
|
||||
// A paired browser shows the summary instead of the form.
|
||||
@@ -1178,10 +1170,9 @@ scenario("B3", "Paste-link pairing: pi2:// and /pair/<code> links reach the conf
|
||||
const linkStorage = await page.evaluate(() => ({
|
||||
connection: localStorage.getItem("pi2-connection"),
|
||||
token: localStorage.getItem("pi2-device-token"),
|
||||
scopes: localStorage.getItem("pi2-device-scopes"),
|
||||
}));
|
||||
const linkWrites = await page.evaluate(() =>
|
||||
(window.__pi2Writes ?? []).filter((write) => ["pi2-connection", "pi2-device-token", "pi2-device-scopes"].includes(write.key)),
|
||||
(window.__pi2Writes ?? []).filter((write) => ["pi2-connection", "pi2-device-token"].includes(write.key)),
|
||||
);
|
||||
ctx.note(`link-flow pi2-* storage writes: ${short(linkWrites, 700)}`);
|
||||
const linkWsErrors = socketErrors(ctx.wsFrames);
|
||||
@@ -1194,7 +1185,7 @@ scenario("B3", "Paste-link pairing: pi2:// and /pair/<code> links reach the conf
|
||||
cause: "same stored 'Bearer <token>' passed verbatim to the WS auth frame (web/src/lib/api.ts + web/src/lib/ws.ts:219)",
|
||||
},
|
||||
);
|
||||
ctx.softCheck("the link flow keeps paired + token + scopes", linkStorage.connection === "paired" && Boolean(linkStorage.token), {
|
||||
ctx.softCheck("the link flow keeps paired + token", linkStorage.connection === "paired" && Boolean(linkStorage.token), {
|
||||
expected: { connection: "paired", token: "set" },
|
||||
observed: { ...linkStorage, token: linkStorage.token ? "set" : null, writes: linkWrites },
|
||||
cause: "the same write-then-clear sequence as B2 (socket auth of the fresh token is rejected → app-shell disconnects)",
|
||||
@@ -2253,7 +2244,7 @@ scenario("B11", "Status: six API-backed cards + metrics strip, devices list/revo
|
||||
const created = await cmd("pair.create", { ttlSec: 600 });
|
||||
const code = created.json?.code;
|
||||
ctx.check("pair.create returns a code", typeof code === "string" && code.length > 0, { observed: created.json });
|
||||
const redeemed = await apiRaw("POST", "/api/v1/pair", { token: null, body: { code, deviceName: "e2e-second-device", kind: "phone" } });
|
||||
const redeemed = await apiRaw("POST", "/api/v1/pair/redeem", { token: null, body: { code, deviceName: "e2e-second-device", kind: "phone" } });
|
||||
ctx.check("second device pairs", redeemed.status === 200 && Boolean(redeemed.json?.token), {
|
||||
expected: "200 with a device token",
|
||||
observed: { status: redeemed.status, body: short(redeemed.json) },
|
||||
|
||||
Reference in new issue
Block a user