An open-source, faster Devin alternative
TypeScript
feat(edge): proxy /api/dashboard/v3/* to sessions-api with a signed org-token
Igor Zalutskid1188fd15446
diff --git a/cloudflare-workers/api-edge/src/dashboard.ts b/cloudflare-workers/api-edge/src/dashboard.tsindex fd6e4155..9daa3475 100644--- a/cloudflare-workers/api-edge/src/dashboard.ts+++ b/cloudflare-workers/api-edge/src/dashboard.ts@@ -46,6 +46,13 @@ export interface DashboardEnv {// if unset the custom-domain endpoints return 503 (feature disabled).CF_API_TOKEN?: string;CF_ZONE_ID?: string;+ // Durable Agent Sessions (/v3) — the dashboard proxies /api/dashboard/v3/* to+ // this sessions-api host, asserting the logged-in OC org via a short-lived+ // signed org-token (V3_EDGE_SECRET, shared with sessions-api). No osb_ key in+ // the browser, no key custody. Both optional — if V3_EDGE_SECRET is unset the+ // proxy 503s.+ SESSIONS_API_URL?: string;+ V3_EDGE_SECRET?: string;}const SESSION_COOKIE = "oc_session";@@ -134,6 +141,26 @@ async function mintCellCapToken(secret: string, orgID: string, cellID: string, preturn signingInput + "." + b64url(sig);}+// Edge→/v3 org assertion: a short-lived HS256 token carrying the OC org id,+// signed with V3_EDGE_SECRET (shared with sessions-api). /v3 trusts it and sets+// owner = the asserted org — same "act for org X" shape as the cell cap-token,+// so no osb_ key reaches the browser and /v3 never custodies a customer key.+async function mintV3OrgToken(secret: string, orgID: string, userID: string | null): Promise<string> {+ const now = Math.floor(Date.now() / 1000);+ const header = { alg: "HS256", typ: "JWT" };+ const payload: Record<string, unknown> = {+ sub: orgID, iss: "opencomputer-edge", aud: "sessions-api-v3", iat: now, exp: now + 120,+ org_id: orgID,+ };+ if (userID) payload.user_id = userID;+ const enc = new TextEncoder();+ const signingInput =+ b64url(enc.encode(JSON.stringify(header))) + "." + b64url(enc.encode(JSON.stringify(payload)));+ const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);+ const sig = await crypto.subtle.sign("HMAC", key, enc.encode(signingInput));+ return signingInput + "." + b64url(sig);+}+function b64url(buf: ArrayBuffer | Uint8Array): string {const bytes = buf instanceof Uint8Array ? buf : new Uint8Array(buf);let s = "";@@ -785,6 +812,43 @@ async function workosUpdateOrg(env: DashboardEnv, workosOrgID: string, name: str});}+// Proxy /api/dashboard/v3/* → sessions-api /v3/*, asserting the logged-in OC org+// via a signed org-token in X-OC-Org-Token (a distinct header so /v3 doesn't+// mistake it for a bearer client token). Forwards only what /v3 needs (never the+// dashboard cookie); streams the response through unchanged so SSE passes back.+async function proxyToV3(+ req: Request,+ env: DashboardEnv,+ caller: Caller,+ sub: string,+): Promise<Response> {+ if (!env.V3_EDGE_SECRET) {+ return json({ error: "agent sessions not configured" }, 503);+ }+ const base = (env.SESSIONS_API_URL ?? "https://api.opencomputer.dev").replace(+ /\/$/,+ "",+ );+ const url = new URL(req.url);+ const target = base + sub + url.search;+ const orgToken = await mintV3OrgToken(env.V3_EDGE_SECRET, caller.orgID, caller.userID);++ const headers = new Headers();+ headers.set("x-oc-org-token", orgToken);+ const ct = req.headers.get("content-type");+ if (ct) headers.set("content-type", ct);+ const accept = req.headers.get("accept");+ if (accept) headers.set("accept", accept);+ const lastEventID = req.headers.get("last-event-id");+ if (lastEventID) headers.set("last-event-id", lastEventID);++ const init: RequestInit = { method: req.method, headers, redirect: "manual" };+ if (req.method !== "GET" && req.method !== "HEAD") {+ init.body = await req.text();+ }+ return fetch(target, init);+}+// ── dispatch ─────────────────────────────────────────────────────────────export async function handleDashboard(@@ -800,6 +864,14 @@ export async function handleDashboard(const sub = path.replace(/^\/api\/dashboard/, "");const method = req.method.toUpperCase();+ // ── Durable Agent Sessions (/v3) — proxy to sessions-api ────────────────+ // Cookie-gated above. We inject OC_V3_KEY downstream so the osb_ key never+ // reaches the browser. (/v3 is per-key today, so this is that key's+ // workspace; org-scoping is a later refinement.)+ if (sub === "/v3" || sub.startsWith("/v3/")) {+ return proxyToV3(req, env, caller, sub);+ }+// ── identity / org ─────────────────────────────────────────────────────if (sub === "/me" && method === "GET") return handleMe(req, env, caller);if (sub === "/orgs" && method === "GET") return handleListOrgs(req, env, caller);