feat(device-pairing): pair phones and browsers with a QR code

Settings -> Account gets "Connect a device", a paired-devices list, and a
remote access headers editor, so the mobile app and other browsers can sign
in without typing a password and pass a reverse proxy such as Cloudflare
Access.

- one-time pairing codes (3 min TTL) minted by a Better Auth plugin and
  redeemed into a new session that records the device name
- POST /api/device-pairing/links builds the ocm://link app link with the
  stored headers as h= parameters and a browser /pair link; plain-HTTP and
  loopback origins are blocked or warned about
- remote access headers are stored encrypted, returned by name only, and
  validated (8 headers, 512-char values, 1800 encoded characters, reserved
  transport names blocked); a Cloudflare Access service token preset fills
  CF-Access-Client-Id and CF-Access-Client-Secret
- login offers "Sign in with QR code" and /pair redeems a scanned code,
  returning to the login redirect target
- revoking a device deletes its session, closes its live connections and
  removes its push subscriptions; the current session cannot be revoked
This commit is contained in:
Chris Scott committed 2026-10-24 19:26:21 -04:00
1 parent 5b0e629182
commit f8e4cfe136
79 files changed
+5398 -110

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
import type {
CreateDevicePairingLinksRequest,
DevicePairingLinksResponse,
PairedDevice,
PairedDevicesResponse,
RedeemDevicePairingCodeRequest,
RedeemDevicePairingCodeResponse,
RemoteAccessHeadersResponse,
ReplaceRemoteAccessHeadersRequest,
} from '@opencode-manager/shared/schemas'
import { API_BASE_URL } from '@/config'
import { getAuthBaseUrl } from '@/lib/auth-client'
import { fetchWrapper, fetchWrapperVoid } from './fetchWrapper'
const REMOTE_ACCESS_HEADERS_URL = `${API_BASE_URL}/api/settings/remote-access-headers`
/** Redeems a browser pairing code and starts a session for the paired user. */
export async function redeemDevicePairingCode(
body: RedeemDevicePairingCodeRequest,
): Promise<RedeemDevicePairingCodeResponse> {
return fetchWrapper<RedeemDevicePairingCodeResponse>(
`${getAuthBaseUrl()}/device-pairing/redeem`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
},
)
}
/** Reads the names of the stored remote-access headers without their values. */
export async function getRemoteAccessHeaders(): Promise<RemoteAccessHeadersResponse> {
return fetchWrapper<RemoteAccessHeadersResponse>(REMOTE_ACCESS_HEADERS_URL)
}
/** Replaces the stored remote-access headers, keeping stored values for blank entries. */
export async function replaceRemoteAccessHeaders(
body: ReplaceRemoteAccessHeadersRequest,
): Promise<RemoteAccessHeadersResponse> {
return fetchWrapper<RemoteAccessHeadersResponse>(REMOTE_ACCESS_HEADERS_URL, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
}
/** Removes every stored remote-access header. */
export async function clearRemoteAccessHeaders(): Promise<RemoteAccessHeadersResponse> {
return fetchWrapper<RemoteAccessHeadersResponse>(REMOTE_ACCESS_HEADERS_URL, {
method: 'DELETE',
})
}
/** Mints a pairing code and builds the app and browser links for this origin. */
export async function createDevicePairingLinks(
body: CreateDevicePairingLinksRequest = {},
): Promise<DevicePairingLinksResponse> {
return fetchWrapper<DevicePairingLinksResponse>(`${API_BASE_URL}/api/device-pairing/links`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
}
/** Lists the devices signed in to this account. */
export async function listPairedDevices(): Promise<PairedDevice[]> {
const response = await fetchWrapper<PairedDevicesResponse>(
`${getAuthBaseUrl()}/device-pairing/devices`,
)
return response.devices
}
/** Signs one paired device out. */
export async function revokePairedDevice(id: string): Promise<void> {
return fetchWrapperVoid(`${getAuthBaseUrl()}/device-pairing/devices/revoke`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id }),
})
}