// Sign in with Coinbase for the Bucksley Mac app, as a Cloudflare Worker. Coinbase needs a client // secret to swap a sign-in code for tokens, and an app can't keep a secret, so this Worker adds it // and passes the request through. It has no storage, keeps nothing and logs no tokens. interface Env { COINBASE_CLIENT_ID?: string; COINBASE_CLIENT_SECRET?: string; COINBASE_REDIRECT_URI?: string; } const SCOPE = "wallet:accounts:read,offline_access"; const APP_CALLBACK = "bucksley://coinbase/callback"; const TOKEN_URL = "https://login.coinbase.com/oauth2/token"; const noStore = { "Cache-Control": "no-store" }; const json = (body: unknown, status = 200) => Response.json(body, { status, headers: noStore }); const redirectURI = (env: Env) => env.COINBASE_REDIRECT_URI ?? "https://connect.bucksley.ai/oauth/callback"; const text = (value: unknown, max = 4096) => typeof value === "string" && value.length > 0 && value.length <= max ? value : undefined; // The public half of the sign-in: what the app needs to open Coinbase's page. function config(env: Env): Response { if (!env.COINBASE_CLIENT_ID) return json({ error: "not_configured" }, 503); return json({ client_id: env.COINBASE_CLIENT_ID, redirect_uri: redirectURI(env), scope: SCOPE }); } // Coinbase only redirects to https, so it lands here and goes straight on to the app's sign-in // sheet with the same code, state or error. function callback(url: URL): Response { const out = new URL(APP_CALLBACK); for (const name of ["code", "state", "error", "error_description"]) { const value = url.searchParams.get(name); if (value) out.searchParams.set(name, value.slice(0, 2048)); } if (!out.searchParams.has("code") && !out.searchParams.has("error")) out.searchParams.set("error", "missing_code"); return new Response(null, { status: 302, headers: { Location: out.toString(), ...noStore, "Referrer-Policy": "no-referrer" } }); } // Swaps a sign-in code (with its PKCE verifier) or a refresh token for new tokens. Only these two // grants, only to Coinbase's token endpoint, and only the tokens go back to the app. async function token(request: Request, env: Env): Promise { const id = env.COINBASE_CLIENT_ID, secret = env.COINBASE_CLIENT_SECRET; if (!id || !secret) return json({ error: "not_configured" }, 503); let body: Record; try { const raw = await request.text(); if (raw.length > 16_384) throw new Error("too large"); body = JSON.parse(raw); } catch { return json({ error: "invalid_request" }, 400); } const form = new URLSearchParams({ client_id: id, client_secret: secret }); if (body.grant_type === "authorization_code") { const code = text(body.code), verifier = text(body.code_verifier, 128); if (!code || !verifier) return json({ error: "invalid_request" }, 400); form.set("grant_type", "authorization_code"); form.set("code", code); form.set("code_verifier", verifier); form.set("redirect_uri", redirectURI(env)); } else if (body.grant_type === "refresh_token") { const refresh = text(body.refresh_token); if (!refresh) return json({ error: "invalid_request" }, 400); form.set("grant_type", "refresh_token"); form.set("refresh_token", refresh); } else { return json({ error: "unsupported_grant_type" }, 400); } const upstream = await fetch(TOKEN_URL, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" }, body: form, }); const answer = (await upstream.json().catch(() => ({}))) as Record; if (!upstream.ok || typeof answer.access_token !== "string" || typeof answer.refresh_token !== "string") { const error = typeof answer.error === "string" ? answer.error : "coinbase_error"; return json({ error }, upstream.status === 200 ? 502 : upstream.status); } return json({ access_token: answer.access_token, refresh_token: answer.refresh_token, expires_in: answer.expires_in, scope: answer.scope }); } const home = ` Bucksley Connect

Bucksley Connect

This is the sign-in helper for Bucksley. When you sign in with Coinbase, it hands Coinbase's one-time code back to the app on your Mac. It has no database and keeps nothing.

`; export default { async fetch(request: Request, env: Env): Promise { const url = new URL(request.url); const route = `${request.method} ${url.pathname}`; if (route === "GET /coinbase/config") return config(env); if (route === "GET /oauth/callback") return callback(url); if (route === "POST /coinbase/token") return token(request, env); if (route === "GET /") return new Response(home, { headers: { "Content-Type": "text/html; charset=utf-8" } }); return json({ error: "not_found" }, 404); }, };