From 2caba90a63e1eb0155ca43496ca6e8eed895e217 Mon Sep 17 00:00:00 2001 From: Shoubhit Dash Date: Thu, 24 Sep 2026 18:28:48 +0530 Subject: [PATCH] feat(app): replace password pairing with one-time links (#50972) --- packages/app/README.md | 9 +- packages/app/e2e/regression/pairing.spec.ts | 54 ---------- packages/app/src/app.tsx | 40 +++---- .../app/src/runtime/platform/platform.tsx | 4 +- .../app/src/runtime/server/health.test.ts | 11 ++ packages/app/src/runtime/server/health.ts | 12 ++- packages/app/src/runtime/server/runtime.tsx | 5 +- packages/app/src/servers/connect/dialog.tsx | 16 +-- .../app/src/servers/connect/pairing.test.ts | 95 +---------------- packages/app/src/servers/connect/pairing.ts | 54 +--------- packages/app/src/servers/connect/scanner.tsx | 13 +-- packages/app/src/servers/connect/screen.tsx | 28 +---- packages/app/src/settings/pairing/pairing.tsx | 32 +++--- packages/app/src/shell/routes/routes.tsx | 80 ++++++-------- packages/cli/src/services/web-ui.ts | 2 +- packages/cli/test/web-ui.test.ts | 2 +- packages/desktop/src/main/ipc-handlers/app.ts | 1 + packages/desktop/src/main/service/pairing.ts | 15 ++- packages/desktop/src/renderer/api-types.ts | 1 + packages/desktop/src/renderer/api.ts | 1 + .../desktop/src/renderer/platform/index.ts | 1 + packages/desktop/src/shared/ipc-rpc/app.ts | 4 +- .../server/src/middleware/authorization.ts | 26 ++++- packages/server/src/process.ts | 13 +-- packages/server/test/process.test.ts | 13 ++- packages/tui/src/app.tsx | 17 +-- packages/tui/src/component/dialog-pair.tsx | 100 +++++++----------- 27 files changed, 201 insertions(+), 448 deletions(-) delete mode 100644 packages/app/e2e/regression/pairing.spec.ts diff --git a/packages/app/README.md b/packages/app/README.md index 7d865ee66f4..ff0d5563d1a 100644 --- a/packages/app/README.md +++ b/packages/app/README.md @@ -94,9 +94,10 @@ In Vite development mode, `origin` uses `VITE_OPENCODE_SERVER_HOST` / `VITE_OPEN (default: `http://localhost:4096`) instead of the frontend origin. Both modes restore user-added servers from storage. Desktop provides the local server it discovers or starts through native initialization. -With no configured servers, the app shows a full-screen connection form. Enter a server address and password, -or choose **Scan QR code** to open the camera and read the pairing code from `opencode pair`. -Scanning fills the form and immediately attempts to connect. Failed connections leave the details available +With no configured servers, or when the only server rejects the saved credentials, the app shows a full-screen +connection form. Enter a server address and password, paste a link from `opencode pair`, or choose +**Scan QR code** to read its QR code. Pairing links are single-use; the app exchanges them for a session token +and immediately attempts to connect. Failed connections leave the details available to edit and retry with **Connect**. Credentials are checked before saving the server. Camera access requires HTTPS (or localhost) and browser permission. Saved offline servers continue to use the normal app UI. @@ -106,7 +107,7 @@ When the service is exposed through an HTTPS reverse proxy, advertise its extern opencode pair --url https://opencode.example.com ``` -This replaces the addresses printed and encoded in the QR code while retaining the local service password. +This replaces the addresses in the printed links and QR code. The proxy URL must reach the OpenCode API, not just the frontend. For separate frontend and API processes, route `/api` to the service while preserving the `/api` prefix. No machine-specific app or CLI build is required. diff --git a/packages/app/e2e/regression/pairing.spec.ts b/packages/app/e2e/regression/pairing.spec.ts deleted file mode 100644 index 661aba2ec02..00000000000 --- a/packages/app/e2e/regression/pairing.spec.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { expect, test } from "@playwright/test" - -test("pairs locally without checking the server and authenticates subsequent requests", async ({ page, baseURL }) => { - const origin = new URL(baseURL ?? "http://127.0.0.1:3000").origin - const password = "pairing-secret" - const authorization = `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}` - const requests: { origin: string; authorization: string | undefined }[] = [] - await page.addInitScript((origin) => { - if (localStorage.getItem("opencode.global.dat:server")) return - localStorage.setItem( - "opencode.global.dat:server", - JSON.stringify({ list: [{ type: "http", http: { url: origin, password: "old-password" } }] }), - ) - }, origin) - await page.route("**/api/**", async (route) => { - requests.push({ - origin: new URL(route.request().url()).origin, - authorization: route.request().headers().authorization, - }) - // Pairing must succeed even when the API is unavailable. - await route.fulfill({ status: 503, contentType: "application/json", body: "{}" }) - }) - - await page.goto(`/connect#${Buffer.from(JSON.stringify({ username: "opencode", password })).toString("base64url")}`) - await expect(page).toHaveURL(`${origin}/`) - await expect(page.getByRole("button", { name: "Home", exact: true })).toBeVisible() - await expect - .poll(() => page.evaluate(() => JSON.parse(localStorage.getItem("opencode.global.dat:server") ?? "{}").list)) - .toEqual([{ type: "http", http: { url: origin, password } }]) - await expect.poll(() => requests.filter((request) => request.origin === origin).length).toBeGreaterThan(0) - expect( - requests.filter((request) => request.origin === origin).every((request) => request.authorization === authorization), - ).toBe(true) - - requests.length = 0 - await page.reload() - await expect(page.getByRole("button", { name: "Home", exact: true })).toBeVisible() - await expect.poll(() => requests.filter((request) => request.origin === origin).length).toBeGreaterThan(0) - expect( - requests.filter((request) => request.origin === origin).every((request) => request.authorization === authorization), - ).toBe(true) -}) - -test("the unpaired page loads without starting server requests", async ({ page }) => { - const requests: string[] = [] - await page.route("**/api/**", async (route) => { - requests.push(route.request().url()) - await route.abort() - }) - await page.goto("/connect") - await expect(page.getByRole("heading", { name: "Connect to a server" })).toBeVisible() - await expect(page.getByLabel("Password", { exact: true })).toBeEditable() - expect(requests).toEqual([]) -}) diff --git a/packages/app/src/app.tsx b/packages/app/src/app.tsx index fdb7780cf56..8e0286a7522 100644 --- a/packages/app/src/app.tsx +++ b/packages/app/src/app.tsx @@ -4,9 +4,9 @@ import { FileComponentProvider } from "@opencode/ui/context/file" import { Font } from "@opencode/ui/font" import { ThemeProvider } from "@opencode/ui/theme/context" import { MetaProvider } from "@solidjs/meta" -import { type BaseRouterProps, Router, useLocation } from "@solidjs/router" +import { type BaseRouterProps, Router } from "@solidjs/router" import { QueryClient, QueryClientProvider } from "@tanstack/solid-query" -import { type Component, createRenderEffect, ErrorBoundary, type JSX, type ParentProps, Show } from "solid-js" +import { type Component, createRenderEffect, ErrorBoundary, type JSX, type ParentProps } from "solid-js" import { Dynamic } from "solid-js/web" import { CommandProvider } from "@/shell/commands/command" import { DesktopCommands } from "@/shell/commands/desktop" @@ -107,29 +107,21 @@ export function AppInterface(props: { // The visual layout lives in the router root so it remains mounted across // route changes. Draft and session routes override only their server-bound data // providers beneath it. - const Root = (rootProps: ParentProps) => { - const location = useLocation() - // Pairing saves credentials before mounting any server connections or health checks. - return ( - <> + const Root = (rootProps: ParentProps) => ( + + - - - - - - - - {props.children} - {rootProps.children} - - - - - - - ) - } + + + + + {props.children} + {rootProps.children} + + + + + ) return ( + /** Single-use code for an `/auth/connect/:code` link. */ + code(): Promise } } diff --git a/packages/app/src/runtime/server/health.test.ts b/packages/app/src/runtime/server/health.test.ts index cf81cbe9a43..2febd583f8c 100644 --- a/packages/app/src/runtime/server/health.test.ts +++ b/packages/app/src/runtime/server/health.test.ts @@ -29,6 +29,17 @@ describe("checkServerHealth", () => { expect(headers).toEqual([password ? `Basic ${btoa(`opencode:${password}`)}` : null]) }) + test("reports rejected credentials without retrying", async () => { + let calls = 0 + const fetch = (async () => { + calls++ + return Response.json({ _tag: "UnauthorizedError", message: "Authentication required" }, { status: 401 }) + }) as unknown as typeof globalThis.fetch + + expect(await checkServerHealth(server, fetch)).toEqual({ healthy: false, unauthorized: true }) + expect(calls).toBe(1) + }) + test("returns healthy response with version", async () => { let request: URL | undefined const fetch = (async (input: RequestInfo | URL) => { diff --git a/packages/app/src/runtime/server/health.ts b/packages/app/src/runtime/server/health.ts index 4a994fa785c..05aa3ddfdd6 100644 --- a/packages/app/src/runtime/server/health.ts +++ b/packages/app/src/runtime/server/health.ts @@ -1,11 +1,17 @@ import { usePlatform } from "@/runtime/platform/platform" import { ServerConnection } from "@/runtime/server/registry" import { authTokenFromCredentials } from "./api" -import { ClientError, OpenCode } from "@opencode/client" +import { ClientError, isUnauthorizedError, OpenCode } from "@opencode/client" import { Accessor, createEffect, onCleanup } from "solid-js" import { createStore, reconcile } from "solid-js/store" -export type ServerHealth = { healthy: boolean; version?: string; incompatible?: boolean; checking?: boolean } +export type ServerHealth = { + healthy: boolean + version?: string + incompatible?: boolean + checking?: boolean + unauthorized?: boolean +} interface CheckServerHealthOptions { timeoutMs?: number @@ -100,7 +106,7 @@ export async function checkServerHealth( .catch((error) => ({ error })) if ("data" in current) return current.data if (signal?.aborted) return { healthy: false } - + if (isUnauthorizedError(current.error)) return { healthy: false, unauthorized: true } return next(count, current.error) } return attempt(0).finally(() => timeout?.clear?.()) diff --git a/packages/app/src/runtime/server/runtime.tsx b/packages/app/src/runtime/server/runtime.tsx index 9b1c154c10d..e8e83533f7f 100644 --- a/packages/app/src/runtime/server/runtime.tsx +++ b/packages/app/src/runtime/server/runtime.tsx @@ -48,15 +48,18 @@ export const { use: useGlobal, provider: GlobalProvider } = createSimpleContext( return serverCtx } + // A server that rejects our credentials would retry its event stream every second with the same + // credentials, so its controller waits until health recovers and then starts with the current ones. createMemo(() => { for (const conn of server.list) { + if (serverHealth[ServerConnection.key(conn)]?.unauthorized) continue ensureServerCtx(conn) } }) createEffect(() => { for (const [key] of serverCtxs) { - if (!server.list.find((conn) => ServerConnection.key(conn) === key)) { + if (serverHealth[key]?.unauthorized || !server.list.find((conn) => ServerConnection.key(conn) === key)) { serverCtxDisposers.get(key)?.() serverCtxDisposers.delete(key) serverCtxs.delete(key) diff --git a/packages/app/src/servers/connect/dialog.tsx b/packages/app/src/servers/connect/dialog.tsx index 4f3bc415735..4324da29fd5 100644 --- a/packages/app/src/servers/connect/dialog.tsx +++ b/packages/app/src/servers/connect/dialog.tsx @@ -29,7 +29,7 @@ import { useCheckServerHealth } from "@/runtime/server/health" import { usePlatform } from "@/runtime/platform/platform" import { isMixedContent } from "./browser" import { createCameraAvailability } from "./camera" -import { decodePairingCode } from "./pairing" +import type { Pairing } from "./pairing" import "@/settings/settings.css" const PairingScanner = lazy(() => import("./scanner").then((module) => ({ default: module.PairingScanner }))) @@ -117,16 +117,10 @@ export const DialogServer: Component<{ invalid={!!form.state.error()} disabled={form.state.busy()} autofocus - list="dialog-server-addresses" aria-describedby={form.state.error() ? "dialog-server-error" : undefined} onInput={(event) => form.change.value(event.currentTarget.value)} onKeyDown={keyDown} /> - - {form.state.urls().map((url) => ( - {form.state.error()} @@ -213,7 +207,6 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht mode: "list" as FormMode, originalUrl: undefined as string | undefined, values: { url: "", name: "", password: "" }, - urls: [] as string[], scanning: false, error: "", status: undefined as boolean | undefined, @@ -227,7 +220,6 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht mode: "list", originalUrl: undefined, values: { url: "", name: "", password: "" }, - urls: [], scanning: false, error: "", status: undefined, @@ -333,11 +325,10 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht setStore("error", "") request.mutate() } - const pair = (pairing: NonNullable>) => { + const pair = (pairing: Pairing) => { healthPreview.cancel() setStore({ - values: { ...store.values, url: pairing.urls[0], password: pairing.password }, - urls: pairing.urls, + values: { ...store.values, url: pairing.url, password: pairing.password }, scanning: false, error: "", }) @@ -359,7 +350,6 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht value: () => store.values.url, name: () => store.values.name, password: () => store.values.password, - urls: () => store.urls, scanning: () => store.scanning, error: () => store.error, status: () => store.status, diff --git a/packages/app/src/servers/connect/pairing.test.ts b/packages/app/src/servers/connect/pairing.test.ts index c6b3201c75b..8b7593834c6 100644 --- a/packages/app/src/servers/connect/pairing.test.ts +++ b/packages/app/src/servers/connect/pairing.test.ts @@ -1,98 +1,5 @@ import { describe, expect, test } from "bun:test" -import { decodePairingCode, decodePairingScan, decodePairingUrl, pairingLink, pairingUrl } from "./pairing" - -describe("pairing URL", () => { - test("pairs with the current origin using credentials without server URLs", () => { - const info = { username: "opencode" as const, password: "a+b & café" } - const origin = "https://opencode.example.com:49709" - const url = new URL(pairingUrl(info, origin)) - - expect(url.origin).toBe(origin) - expect(url.pathname).toBe("/connect") - expect(url.search).toBe("") - expect(url.hash).not.toBe("") - expect(decodePairingUrl(url.hash, origin)).toEqual({ urls: [origin], password: info.password }) - expect(decodePairingCode(JSON.stringify(info))).toBeUndefined() - }) - - test("keeps accepting query pairing data", () => { - const info = { urls: ["http://192.168.1.2:4096"], username: "opencode", password: "a+b & café" } - expect(decodePairingUrl(`?data=${encodeURIComponent(JSON.stringify(info))}`)).toEqual({ - urls: info.urls, - password: info.password, - }) - }) - - test("accepts CLI base64url fragments", () => { - const value = { urls: ["http://localhost:4096"], username: "opencode", password: "a+b & café" } - expect(decodePairingUrl(`#${Buffer.from(JSON.stringify(value)).toString("base64url")}`)).toEqual({ - urls: value.urls, - password: value.password, - }) - }) - - test("rejects invalid query data", () => { - expect(decodePairingUrl("?data=invalid")).toBeUndefined() - expect(decodePairingUrl("?other=value")).toBeUndefined() - }) - - test("accepts legacy JSON fragments", () => { - const value = { urls: ["http://localhost:4096"], username: "opencode", password: "secret" } - expect(decodePairingUrl(`#${encodeURIComponent(JSON.stringify(value))}`)).toEqual({ - urls: value.urls, - password: value.password, - }) - }) - - test("rejects an invalid fragment", () => { - expect(decodePairingUrl("#not-a-pairing-code")).toBeUndefined() - }) -}) - -describe("pairing scan", () => { - const info = { - urls: ["http://192.168.1.2:49374", "http://127.0.0.1:49374"], - username: "opencode" as const, - password: "a+b & café", - } - - test("decodes the raw JSON code", () => { - expect(decodePairingScan(JSON.stringify(info))).toEqual({ urls: info.urls, password: info.password }) - }) - - test("decodes a direct /connect URL", () => { - expect( - decodePairingScan(pairingUrl({ username: info.username, password: info.password }, "http://192.168.1.2:49374")), - ).toEqual({ - urls: ["http://192.168.1.2:49374"], - password: info.password, - }) - }) - - test("keeps accepting /connect URLs with query data", () => { - expect( - decodePairingScan(`http://192.168.1.2:49374/connect?data=${encodeURIComponent(JSON.stringify(info))}`), - ).toEqual({ - urls: info.urls, - password: info.password, - }) - }) - - test("falls back to the URL origin when the payload omits server URLs", () => { - const origin = "https://opencode.example.com:49709" - expect(decodePairingScan(pairingUrl({ username: "opencode", password: "secret" }, origin))).toEqual({ - urls: [origin], - password: "secret", - }) - }) - - test("rejects URLs without pairing data and non-http schemes", () => { - expect(decodePairingScan("http://192.168.1.2:49374/connect")).toBeUndefined() - expect(decodePairingScan("https://example.com/?data=invalid")).toBeUndefined() - expect(decodePairingScan("opencode-ios://connect?password=secret")).toBeUndefined() - expect(decodePairingScan("not a code")).toBeUndefined() - }) -}) +import { pairingLink } from "./pairing" describe("pairing link", () => { test("reads the server address and code from opencode pair links", () => { diff --git a/packages/app/src/servers/connect/pairing.ts b/packages/app/src/servers/connect/pairing.ts index 219c81e5137..f40a7f94b95 100644 --- a/packages/app/src/servers/connect/pairing.ts +++ b/packages/app/src/servers/connect/pairing.ts @@ -1,16 +1,6 @@ -import { Option, Schema } from "effect" import { OpenCode } from "@opencode/client/promise" -import { base64Encode } from "@opencode/util/encode" import { normalizeServerUrl } from "@/runtime/server/registry" -const pairing = Schema.fromJsonString( - Schema.Struct({ - urls: Schema.optional(Schema.Array(Schema.String)), - username: Schema.Literal("opencode"), - password: Schema.String, - }), -) - export function serverAddress(value: string) { if (value.includes("://") && !/^https?:\/\//.test(value.trim())) return const normalized = normalizeServerUrl(value) @@ -21,46 +11,6 @@ export function serverAddress(value: string) { return normalized } -export function decodePairingCode(value: string, origin?: string) { - const result = Schema.decodeUnknownOption(pairing)(value) - if (Option.isNone(result)) return - const urls = [ - ...new Set((result.value.urls ?? (origin ? [origin] : [])).map(serverAddress).filter((url) => url !== undefined)), - ] - if (!urls.length) return - return { urls, password: result.value.password } -} - -export function pairingUrl(value: { username: "opencode"; password: string }, host: string) { - return `${new URL("/connect", host)}#${base64Encode(JSON.stringify(value))}` -} - -// Scanned codes are either the raw pairing JSON or a full /connect URL from `opencode pair` or desktop. -export function decodePairingScan(value: string) { - const url = URL.parse(value.trim()) - if (!url || (url.protocol !== "http:" && url.protocol !== "https:")) return decodePairingCode(value) - return decodePairingUrl(url.search, url.origin) ?? decodePairingUrl(url.hash, url.origin) -} - -export function decodePairingUrl(value: string, origin?: string) { - if (value.startsWith("?")) { - const data = new URLSearchParams(value).get("data") - return data === null ? undefined : decodePairingCode(data, origin) - } - const encoded = value.startsWith("#") ? value.slice(1) : value - if (!encoded) return - const legacy = new URLSearchParams(`value=${encoded}`).get("value") ?? "" - if (legacy.startsWith("{")) return decodePairingCode(legacy) - if (!/^[A-Za-z0-9_-]+$/.test(encoded) || encoded.length % 4 === 1) return - const binary = atob( - encoded - .replaceAll("-", "+") - .replaceAll("_", "/") - .padEnd(Math.ceil(encoded.length / 4) * 4, "="), - ) - return decodePairingCode(new TextDecoder().decode(Uint8Array.from(binary, (char) => char.charCodeAt(0))), origin) -} - // Links printed by `opencode pair` carry a single-use code that the server exchanges for a session token. export function pairingLink(value: string) { const url = URL.parse(value.trim()) @@ -71,11 +21,13 @@ export function pairingLink(value: string) { return { url: address, code } } +export type Pairing = { readonly url: string; readonly password: string } + export function redeemPairingLink(link: { url: string; code: string }) { return OpenCode.make({ baseUrl: link.url }) .server.connect({ code: link.code }) .then( - (session) => ({ urls: [link.url], password: session.token }), + (session): Pairing => ({ url: link.url, password: session.token }), () => undefined, ) } diff --git a/packages/app/src/servers/connect/scanner.tsx b/packages/app/src/servers/connect/scanner.tsx index 8ffd0d99f6a..89035c803ed 100644 --- a/packages/app/src/servers/connect/scanner.tsx +++ b/packages/app/src/servers/connect/scanner.tsx @@ -3,13 +3,10 @@ import { onCleanup, onMount, Show } from "solid-js" import { createStore } from "solid-js/store" import { Button } from "@opencode/ui/button" import { useLanguage } from "@/runtime/i18n/language" -import { decodePairingScan, pairingLink, redeemPairingLink } from "./pairing" +import { pairingLink, redeemPairingLink, type Pairing } from "./pairing" import "./scanner.css" -export function PairingScanner(props: { - onScan: (value: NonNullable>) => void - onCancel: () => void -}) { +export function PairingScanner(props: { onScan: (value: Pairing) => void; onCancel: () => void }) { const language = useLanguage() const [state, setState] = createStore({ error: "", ready: false }) const video = document.createElement("video") @@ -22,12 +19,6 @@ export function PairingScanner(props: { const scanner = new QrScanner( video, (result) => { - const pairing = decodePairingScan(result.data) - if (pairing) { - scanner.stop() - props.onScan(pairing) - return - } const link = pairingLink(result.data) if (!link) { setState("error", language.t("server.connect.scan.invalid")) diff --git a/packages/app/src/servers/connect/screen.tsx b/packages/app/src/servers/connect/screen.tsx index bb21c771b84..6e25d61a6ff 100644 --- a/packages/app/src/servers/connect/screen.tsx +++ b/packages/app/src/servers/connect/screen.tsx @@ -9,28 +9,19 @@ import { usePlatform } from "@/runtime/platform/platform" import { useCheckServerHealth } from "@/runtime/server/health" import { useServers } from "@/runtime/server/registry" import { pairingLink, redeemPairingLink, serverAddress } from "./pairing" -import type { decodePairingCode } from "./pairing" import { isMixedContent } from "./browser" import { createCameraAvailability } from "./camera" import "./screen.css" const PairingScanner = lazy(() => import("./scanner").then((module) => ({ default: module.PairingScanner }))) -export function ConnectServerScreen( - props: { pairing?: NonNullable>; onConnect?: () => void } = {}, -) { +export function ConnectServerScreen(props: { url?: string } = {}) { const language = useLanguage() const platform = usePlatform() const servers = useServers() const check = useCheckServerHealth() const camera = createCameraAvailability() - const [state, setState] = createStore({ - url: props.pairing?.urls[0] ?? "", - password: props.pairing?.password ?? "", - urls: props.pairing?.urls ?? ([] as string[]), - error: "", - scanning: false, - }) + const [state, setState] = createStore({ url: props.url ?? "", password: "", error: "", scanning: false }) const connectionError = () => language.t( platform.platform === "web" && isMixedContent(location.href, state.url) @@ -61,7 +52,6 @@ export function ConnectServerScreen( return } servers.add({ type: "http", http }) - props.onConnect?.() }, onError: () => setState("error", connectionError()), })) @@ -86,13 +76,7 @@ export function ConnectServerScreen( void camera.refetch() }} onScan={(pairing) => { - setState({ - url: pairing.urls[0], - urls: pairing.urls, - password: pairing.password, - error: "", - scanning: false, - }) + setState({ url: pairing.url, password: pairing.password, error: "", scanning: false }) request.mutate() }} /> @@ -120,18 +104,12 @@ export function ConnectServerScreen( spellcheck={false} required appearance="large" - list="server-connect-addresses" placeholder={language.t("dialog.server.add.placeholder")} value={state.url} disabled={request.isPending} aria-describedby={state.error ? "server-connect-error" : undefined} onInput={(event) => setState({ url: event.currentTarget.value, error: "" })} /> - - {state.urls.map((url) => ( -
diff --git a/packages/app/src/settings/pairing/pairing.tsx b/packages/app/src/settings/pairing/pairing.tsx index 4f2ad52ea13..ecfc2f23d6c 100644 --- a/packages/app/src/settings/pairing/pairing.tsx +++ b/packages/app/src/settings/pairing/pairing.tsx @@ -8,8 +8,7 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/solid-query" import { createEffect, createMemo, onCleanup, Show } from "solid-js" import { renderSVG } from "uqr" import { useLanguage } from "@/runtime/i18n/language" -import { usePlatform, type PairingInfo } from "@/runtime/platform/platform" -import { pairingUrl } from "@/servers/connect/pairing" +import { usePlatform } from "@/runtime/platform/platform" import { SettingsList } from "@/settings/list" import { SettingsRow } from "@/settings/row" @@ -74,8 +73,8 @@ export function SettingsPairing() { dialog.push(() => ( )) } @@ -117,17 +116,19 @@ export function SettingsPairing() { ) } -function DialogPairing(props: { title: string; info: PairingInfo | null | undefined; host: string }) { +function DialogPairing(props: { title: string; host: string; code: () => Promise }) { const language = useLanguage() const platform = usePlatform() + // Codes are single-use, so keep replacing the link while the dialog is open. + const code = useQuery(() => ({ + queryKey: ["pairing", "code"], + queryFn: props.code, + gcTime: 0, + refetchInterval: 60_000, + })) const url = createMemo(() => { - if (!props.info) return - return pairingUrl({ username: props.info.username, password: props.info.password }, props.host) - }) - const origin = createMemo(() => { - const value = url() - if (!value) return - return new URL(value).origin + if (!code.isSuccess) return + return new URL(`/auth/connect/${code.data}`, props.host).href }) const copy = useMutation(() => ({ mutationFn: async () => { @@ -153,7 +154,7 @@ function DialogPairing(props: { title: string; info: PairingInfo | null | undefi - + + + +
- } - > - - - - - - )} - /> - - - - ) -} - -function ConnectRoute() { - const navigate = useNavigate() - const servers = useServers() - const pairing = decodePairingUrl(location.search, location.origin) ?? decodePairingUrl(location.hash, location.origin) - onMount(() => { - if (!pairing) return - servers.add({ type: "http", http: { url: pairing.urls[0], password: pairing.password } }) - navigate("/", { replace: true }) - }) - return ( - - navigate("/", { replace: true })} /> - + + + + ( + + + + + } + > + + + + + + )} + /> + + ) } @@ -97,8 +76,15 @@ function TargetServerRoute(props: ParentProps) { function AppLayout(props: ParentProps) { const servers = useServers() + const global = useGlobal() + // A lone server that rejects our credentials (e.g. the web app before pairing) has nothing else to show. + const signedOut = () => { + const only = servers.list.length === 1 ? servers.list[0] : undefined + if (only?.type !== "http") return + return global.servers.health[ServerConnection.key(only)]?.unauthorized ? only : undefined + } return ( - 0} fallback={}> + 0 && !signedOut()} fallback={}> diff --git a/packages/cli/src/services/web-ui.ts b/packages/cli/src/services/web-ui.ts index 8ddd6eafd83..732e505f0b1 100644 --- a/packages/cli/src/services/web-ui.ts +++ b/packages/cli/src/services/web-ui.ts @@ -13,7 +13,7 @@ export const handler = Effect.fn("cli.web-ui.handler")(function* (options?: { re Effect.gen(function* () { const request = yield* HttpServerRequest.HttpServerRequest const url = new URL(request.url, "http://localhost") - // Serve the web shell before API authentication so /connect can load credentials in JavaScript. + // Serve the web shell before API authentication so a signed-out browser gets the app's sign-in screen. if ( url.pathname === "/api" || url.pathname.startsWith("/api/") || diff --git a/packages/cli/test/web-ui.test.ts b/packages/cli/test/web-ui.test.ts index 8568b6ab1ae..4bc4b59b5ed 100644 --- a/packages/cli/test/web-ui.test.ts +++ b/packages/cli/test/web-ui.test.ts @@ -35,7 +35,7 @@ describe("web UI", () => { yield* Effect.forEach( [ "/", - "/connect?data=%7B%7D", + "/settings", "/workspace/example", "/_assets/app.js", "/_assets/app.css", diff --git a/packages/desktop/src/main/ipc-handlers/app.ts b/packages/desktop/src/main/ipc-handlers/app.ts index dcfeb85c11c..5674c980e59 100644 --- a/packages/desktop/src/main/ipc-handlers/app.ts +++ b/packages/desktop/src/main/ipc-handlers/app.ts @@ -76,6 +76,7 @@ export const appHandlers = AppRpcs.toLayer( }), AppRelaunch: () => Effect.sync(lifecycle.relaunch), AppPairInfo: () => pair(pairing.info), + AppPairCode: () => pair(pairing.code), AppGetKeepScreenActive: () => Effect.sync(screenActivity.get), AppSetKeepScreenActive: ({ enabled }) => Effect.try(() => screenActivity.set(enabled)).pipe(Effect.mapError(String)), diff --git a/packages/desktop/src/main/service/pairing.ts b/packages/desktop/src/main/service/pairing.ts index b682360dc07..bcbd940c41e 100644 --- a/packages/desktop/src/main/service/pairing.ts +++ b/packages/desktop/src/main/service/pairing.ts @@ -1,21 +1,18 @@ import { SidecarCredentials } from "./sidecar-credentials" export function createPairing() { - const requireCredentials = () => { + const client = async () => { const credentials = SidecarCredentials.get() if (!credentials) throw new Error("The local desktop server is not ready") - return credentials - } - const readInfo = async (credentials: ReturnType) => { const { OpenCode } = await import("@opencode/client/promise") - const info = await OpenCode.make({ + return OpenCode.make({ baseUrl: credentials.url, headers: credentials.password ? { Authorization: `Basic ${Buffer.from(`opencode:${credentials.password}`).toString("base64")}` } : undefined, - }).server.info() - return { urls: info.urls, username: "opencode" as const, password: credentials.password ?? "" } + }) } - const info = () => readInfo(requireCredentials()) - return { info } + const info = async () => ({ urls: (await (await client()).server.info()).urls }) + const code = async () => (await (await client()).server.pair()).code + return { info, code } } diff --git a/packages/desktop/src/renderer/api-types.ts b/packages/desktop/src/renderer/api-types.ts index 29335dbe177..ec2dbdfc1ad 100644 --- a/packages/desktop/src/renderer/api-types.ts +++ b/packages/desktop/src/renderer/api-types.ts @@ -92,6 +92,7 @@ export type ElectronAPI = { recordFatalRendererError(error: FatalRendererError): Promise setNativeTranslations(bundle: DesktopNativeBundle): Promise pairInfo(): Promise + pairCode(): Promise getKeepScreenActive(): Promise setKeepScreenActive(enabled: boolean): Promise } diff --git a/packages/desktop/src/renderer/api.ts b/packages/desktop/src/renderer/api.ts index ca09acb8941..fab93eafc9d 100644 --- a/packages/desktop/src/renderer/api.ts +++ b/packages/desktop/src/renderer/api.ts @@ -163,6 +163,7 @@ export const api: ElectronAPI = { recordFatalRendererError: (error) => invoke("AppRecordFatalRendererError", { error }), setNativeTranslations: (bundle) => invoke("AppSetNativeTranslations", { value: bundle }), pairInfo: () => invoke("AppPairInfo").then(mutable), + pairCode: () => invoke("AppPairCode"), getKeepScreenActive: () => invoke("AppGetKeepScreenActive"), setKeepScreenActive: (enabled) => invoke("AppSetKeepScreenActive", { enabled }), } diff --git a/packages/desktop/src/renderer/platform/index.ts b/packages/desktop/src/renderer/platform/index.ts index 2dca8f9715c..9a3c03c45dd 100644 --- a/packages/desktop/src/renderer/platform/index.ts +++ b/packages/desktop/src/renderer/platform/index.ts @@ -91,6 +91,7 @@ export function createDesktopPlatform( }, pair: { info: () => api.pairInfo(), + code: () => api.pairCode(), }, } } diff --git a/packages/desktop/src/shared/ipc-rpc/app.ts b/packages/desktop/src/shared/ipc-rpc/app.ts index 10b5d056d75..1f9b62bdf9c 100644 --- a/packages/desktop/src/shared/ipc-rpc/app.ts +++ b/packages/desktop/src/shared/ipc-rpc/app.ts @@ -7,8 +7,6 @@ const ServerReadyData = Schema.Struct({ export const PairingInfo = Schema.Struct({ urls: Schema.Array(Schema.String), - username: Schema.Literal("opencode"), - password: Schema.String, }) export const AppAwaitInitialization = Rpc.make("AppAwaitInitialization", { success: ServerReadyData }) @@ -60,6 +58,7 @@ export const AppSetNativeTranslations = Rpc.make("AppSetNativeTranslations", { }) export const AppRelaunch = Rpc.make("AppRelaunch") export const AppPairInfo = Rpc.make("AppPairInfo", { success: PairingInfo, error: Schema.String }) +export const AppPairCode = Rpc.make("AppPairCode", { success: Schema.String, error: Schema.String }) export const AppGetKeepScreenActive = Rpc.make("AppGetKeepScreenActive", { success: Schema.Boolean }) export const AppSetKeepScreenActive = Rpc.make("AppSetKeepScreenActive", { payload: { enabled: Schema.Boolean }, @@ -82,6 +81,7 @@ export const AppRpcs = RpcGroup.make( AppSetNativeTranslations, AppRelaunch, AppPairInfo, + AppPairCode, AppGetKeepScreenActive, AppSetKeepScreenActive, ) diff --git a/packages/server/src/middleware/authorization.ts b/packages/server/src/middleware/authorization.ts index d9ae944df50..84906429d77 100644 --- a/packages/server/src/middleware/authorization.ts +++ b/packages/server/src/middleware/authorization.ts @@ -37,6 +37,23 @@ function credentialFromRequest(request: HttpServerRequest.HttpServerRequest) { return Effect.succeed(emptyCredential()) } +const UNAUTHORIZED_MESSAGE = "Authentication required" + +// Browsers show a native credentials prompt for a Basic challenge even on fetch, which would stall the web app +// before it can show its own sign-in screen. Only non-browser clients and page navigations get the challenge. +function challengeRequest(request: HttpServerRequest.HttpServerRequest) { + const mode = request.headers["sec-fetch-mode"] + return mode === undefined || mode === "navigate" +} + +// Matches what the Authorization middleware encodes, for requests rejected before the HttpApi runs. +export function unauthorizedResponse(request: HttpServerRequest.HttpServerRequest) { + return HttpServerResponse.jsonUnsafe( + { _tag: "UnauthorizedError", message: UNAUTHORIZED_MESSAGE }, + { status: 401, headers: challengeRequest(request) ? { "www-authenticate": WWW_AUTHENTICATE } : undefined }, + ) +} + export function authorizedRequest(request: HttpServerRequest.HttpServerRequest, config: ServerAuth.Info) { return credentialFromRequest(request).pipe( Effect.map((credential) => ServerAuth.authorized(credential, config) || authorizedSessionCookie(request, config)), @@ -66,10 +83,11 @@ export const authorizationLayer = Layer.effect( if (hasPtyConnectTicketURL(url) || hasPersistentPtyConnectTicketURL(url) || isPairingConnectURL(url)) return yield* effect if (yield* authorizedRequest(request, config)) return yield* effect - yield* HttpEffect.appendPreResponseHandler((_request, response) => - Effect.succeed(HttpServerResponse.setHeader(response, "www-authenticate", WWW_AUTHENTICATE)), - ) - return yield* new UnauthorizedError({ message: "Authentication required" }) + if (challengeRequest(request)) + yield* HttpEffect.appendPreResponseHandler((_request, response) => + Effect.succeed(HttpServerResponse.setHeader(response, "www-authenticate", WWW_AUTHENTICATE)), + ) + return yield* new UnauthorizedError({ message: UNAUTHORIZED_MESSAGE }) }), ) }), diff --git a/packages/server/src/process.ts b/packages/server/src/process.ts index 2c9acb4d2d2..1c11586d630 100644 --- a/packages/server/src/process.ts +++ b/packages/server/src/process.ts @@ -13,7 +13,7 @@ import { HttpMiddleware, HttpRouter, HttpServer, HttpServerRequest, HttpServerRe import { createServer } from "node:http" import { ServerAuth } from "./auth" import { isAllowedCorsOrigin } from "./cors" -import { authorizedRequest } from "./middleware/authorization" +import { authorizedRequest, unauthorizedResponse } from "./middleware/authorization" import { withoutParentSpan } from "./request-tracing" import { createRoutes } from "./routes" import { ServerInfo } from "./server-info" @@ -183,7 +183,7 @@ function dispatch( const app = yield* Ref.get(application) const ready = state.type === "ready" && Option.isSome(app) if (request.method === "GET" && url.pathname === "/api/info" && !ready) { - if (!(yield* authorizedRequest(request, auth))) return unauthorized() + if (!(yield* authorizedRequest(request, auth))) return unauthorizedResponse(request) return yield* infoResponse(status, version, urls, tmp) } if ( @@ -191,19 +191,12 @@ function dispatch( (!ready || (!hasPtyConnectTicketURL(url) && !hasPersistentPtyConnectTicketURL(url))) && !(yield* authorizedRequest(request, auth)) ) - return unauthorized() + return unauthorizedResponse(request) if (ready) return yield* app.value return unavailable(state) }) } -function unauthorized() { - return HttpServerResponse.empty({ - status: 401, - headers: { "www-authenticate": 'Basic realm="Secure Area"' }, - }) -} - const infoResponse = Effect.fnUntraced(function* ( status: Status.Interface, version: string, diff --git a/packages/server/test/process.test.ts b/packages/server/test/process.test.ts index b6af09bd915..f5d4071a73b 100644 --- a/packages/server/test/process.test.ts +++ b/packages/server/test/process.test.ts @@ -128,7 +128,10 @@ it.live("authenticates API requests behind the frontend transform while allowing Effect.gen(function* () { const response = yield* Effect.promise(() => fetch(new URL(pathname, HttpServer.formatAddress(server.address)))) expect(response.status).toBe(401) - expect(yield* Effect.promise(() => response.text())).toBe("") + expect(yield* Effect.promise(() => response.json())).toEqual({ + _tag: "UnauthorizedError", + message: "Authentication required", + }) }), ) @@ -185,7 +188,13 @@ it.live("pairing links sign in browsers with a cookie and API clients with a tok return (yield* Effect.promise(() => response.json())) as { code: string; expires_in: number } }) - expect((yield* request("/api/pair", { method: "POST" })).status).toBe(401) + const rejected = yield* request("/api/pair", { method: "POST" }) + expect(rejected.status).toBe(401) + expect(rejected.headers.get("www-authenticate")).toBe('Basic realm="Secure Area"') + // A Basic challenge on fetch makes browsers show a native prompt instead of the app's sign-in screen. + const fetched = yield* request("/api/info", { headers: { "sec-fetch-mode": "cors" } }) + expect(fetched.status).toBe(401) + expect(fetched.headers.get("www-authenticate")).toBeNull() const browser = yield* pair expect(browser.expires_in).toBe(300) diff --git a/packages/tui/src/app.tsx b/packages/tui/src/app.tsx index b4a196f5c2a..9fe99daf595 100644 --- a/packages/tui/src/app.tsx +++ b/packages/tui/src/app.tsx @@ -63,7 +63,7 @@ import { DialogMcp } from "./component/dialog-mcp" import { DialogStatus } from "./component/dialog-status" import { DialogConfig } from "./component/dialog-config" import { DialogDebug } from "./component/dialog-debug" -import { DialogPair, type DialogPairCredentials } from "./component/dialog-pair" +import { DialogPair } from "./component/dialog-pair" import { DialogThemeList } from "./component/dialog-theme-list" import { DialogHelp } from "./ui/dialog-help" import { DialogAgent } from "./component/dialog-agent" @@ -403,16 +403,7 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) { packages={input.packages} directories={pluginDirectories} > - + @@ -465,7 +456,7 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) { }) }) -function App(props: { pair?: DialogPairCredentials }) { +function App() { const log = useLog({ component: "app" }) const app = useTuiApp() const startup = useTuiStartup() @@ -1007,7 +998,7 @@ function App(props: { pair?: DialogPairCredentials }) { title: "Pair device", slash: { name: "pair", aliases: ["web"] }, run: () => { - dialog.replace(() => ) + dialog.replace(() => ) }, category: "System", }, diff --git a/packages/tui/src/component/dialog-pair.tsx b/packages/tui/src/component/dialog-pair.tsx index d6e11437056..b39da7f74be 100644 --- a/packages/tui/src/component/dialog-pair.tsx +++ b/packages/tui/src/component/dialog-pair.tsx @@ -8,95 +8,65 @@ import { useDialog } from "../ui/dialog" import { Link } from "../ui/link" import { errorMessage } from "../util/error" -export type DialogPairCredentials = { - readonly username: string - readonly password: string -} - -export function DialogPair(props: { credentials?: DialogPairCredentials }) { +export function DialogPair() { const client = useClient() const dialog = useDialog() const dimensions = useTerminalDimensions() const theme = useTheme().surface("dialog") const [loadError, setLoadError] = createSignal() - const [showPassword, setShowPassword] = createSignal(false) - const [passwordHover, setPasswordHover] = createSignal(false) dialog.setSize("large") dialog.setCentered(true) - const [server] = createResource(() => - client.api.server.info().catch((error) => { - setLoadError(error) - return undefined - }), + const [info] = createResource(() => + Promise.all([client.api.server.info(), client.api.server.pair()]) + .then(([server, pairing]) => { + const link = (url: string) => new URL(`/auth/connect/${pairing.code}`, url).href + const local = server.urls[0] ? new URL(server.urls[0]) : undefined + if (local) local.hostname = "localhost" + return { + links: server.urls.map(link), + localhost: local ? link(local.href) : undefined, + minutes: Math.round(pairing.expires_in / 60), + loopback: server.urls.some((url) => ["localhost", "127.0.0.1", "[::1]"].includes(new URL(url).hostname)), + } + }) + .catch((error) => { + setLoadError(error) + return undefined + }), ) - const info = createMemo(() => { - const current = server() - if (!current) return - return { - urls: current.urls, - username: props.credentials?.username ?? "opencode", - password: props.credentials?.password ?? "", - } - }) - const localhost = createMemo(() => { - const value = info()?.urls[0] - if (!value) return "" - const url = new URL(value) - url.hostname = "localhost" - return url.toString().replace(/\/$/, "") - }) const horizontal = createMemo(() => dimensions().width >= 96) const content = () => { const value = info() if (!value) return - const href = (input: string) => { - const url = new URL(input) - url.username = encodeURIComponent(value.username) - url.password = encodeURIComponent(value.password) - return url.toString() - } return ( - - This device - - {(url) => ( - + + Open a link to connect. Links work once and expire in {value.minutes} minutes. + + + {(url) => ( + + This device + {url()} - )} - - + + )} + - URLs - + Links + {(url) => ( - + {url} )} - - Username - {value.username} - - - Password - setPasswordHover(true)} - onMouseOut={() => setPasswordHover(false)} - onMouseUp={() => setShowPassword((current) => !current)} - > - {showPassword() ? value.password : "************"} - - - ["localhost", "127.0.0.1", "[::1]"].includes(new URL(url).hostname))}> + Run `opencode service set hostname 0.0.0.0` to access the service remotely. @@ -108,7 +78,9 @@ export function DialogPair(props: { credentials?: DialogPairCredentials }) { flexShrink={0} alignItems={horizontal() ? "flex-end" : "center"} > - {renderUnicodeCompact(JSON.stringify(value), { border: 1 })} + + {(url) => {renderUnicodeCompact(url(), { border: 1 })}} + )