Files
opencode-manager/shared/src/utils/user-agent.ts
T
Chris Scott f8e4cfe136 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
2026-10-24 19:26:21 -04:00

33 lines
1.3 KiB
TypeScript

const UNKNOWN_DEVICE = 'Unknown device'
const RAW_USER_AGENT_MAX_LENGTH = 80
/** Describes a user agent as "browser on platform", falling back to the raw string. */
export function describeUserAgent(userAgent: string | null | undefined): string {
const value = userAgent?.trim()
if (!value) return UNKNOWN_DEVICE
const platform = detectPlatform(value)
const browser = detectBrowser(value)
if (browser && platform) return `${browser} on ${platform}`
return browser ?? platform ?? value.slice(0, RAW_USER_AGENT_MAX_LENGTH)
}
function detectPlatform(userAgent: string): string | null {
if (userAgent.includes('iPhone')) return 'iPhone'
if (userAgent.includes('iPad')) return 'iPad'
if (userAgent.includes('Android')) return 'Android'
if (userAgent.includes('Macintosh')) return 'macOS'
if (userAgent.includes('Windows')) return 'Windows'
if (userAgent.includes('Linux')) return 'Linux'
return null
}
function detectBrowser(userAgent: string): string | null {
if (userAgent.includes('Edg/')) return 'Edge'
if (userAgent.includes('OPR/')) return 'Opera'
if (userAgent.includes('Firefox/') || userAgent.includes('FxiOS/')) return 'Firefox'
if (userAgent.includes('CriOS/') || userAgent.includes('Chrome/')) return 'Chrome'
if (userAgent.includes('Safari/') && userAgent.includes('Version/')) return 'Safari'
return null
}