diff --git a/auth-worker/PAGES_STAGING.md b/auth-worker/PAGES_STAGING.md new file mode 100644 index 0000000..158ffde --- /dev/null +++ b/auth-worker/PAGES_STAGING.md @@ -0,0 +1,31 @@ +# Cloudflare Pages staging + +The CLI Hub staging host is isolated from the existing OCodeJS production services: + +- Pages project: `ocodejs-cli-hub-staging` +- Host: https://ocodejs-cli-hub-staging.pages.dev +- D1 database: `ocodejs-cli-auth-staging` (APAC) +- D1 binding: `DB` +- No custom domain or DNS changes +- The Pages project is not connected to a production branch deployment. + +## Same-origin auth + +`functions/[[path]].js` sends `/health`, `/auth/*`, and `/api/*` to the existing auth Worker module. Other requests continue to the static Pages asset pipeline. HTML responses set `window.OCODE_AUTH_API` to the current Pages origin. This keeps the HttpOnly session cookie first-party in Safari. + +The staging D1 migration `auth-worker/migrations/0001_init.sql` has been applied and the Pages deployment configuration has the `DB` binding in both preview and production environments. + +## Required Pages environment variables + +Already configured for both Preview and Production: + +- `SESSION_SECRET` — generated cryptographically and stored as an encrypted Pages secret. +- `TOKEN_ENCRYPTION_KEY` — generated as a base64-encoded 32-byte key and stored as an encrypted Pages secret. +- `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` — not configured; only required to enable GitHub OAuth. + +The Pages Function sets `APP_ORIGIN` from the exact active Pages request origin at runtime. This supports both the stable staging hostname and branch-preview hostnames without allowing cross-origin requests. PAT login is backend-configured. GitHub OAuth remains disabled until its dedicated OAuth App credentials are configured. GitHub OAuth additionally requires a dedicated OAuth App whose callback URL is `https://ocodejs-cli-hub-staging.pages.dev/auth/github/callback`. + +Do not copy secrets into GitHub files, commits, issue comments, or chat. Do not attach a custom domain or alter existing `app`, `mcp`, or auth Workers. + + +Cloudflare Pages is connected to this repository for the isolated staging project. Only the `feat/cloudflare-pages-same-origin-auth` branch is included for Preview deployments; automatic deployments from `main` are disabled. diff --git a/auth-worker/test/pages-function.test.mjs b/auth-worker/test/pages-function.test.mjs new file mode 100644 index 0000000..2172fdc --- /dev/null +++ b/auth-worker/test/pages-function.test.mjs @@ -0,0 +1,16 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { readFile } from "node:fs/promises"; + +const source = await readFile(new URL("../../functions/[[path]].js", import.meta.url), "utf8"); + +test("Pages Functions routes auth endpoints through the same-origin Worker", () => { + assert.match(source, /AUTH_ROUTE/); + assert.match(source, /authWorker\.fetch\(request, \{ \.\.\.env, APP_ORIGIN: url\.origin \}\)/); + assert.match(source, /context\.next\(\)/); +}); + +test("Pages HTML configures the auth API to use the current origin", () => { + assert.match(source, /window\.OCODE_AUTH_API = location\.origin/); + assert.match(source, /contentType\.includes\("text\/html"\)/); +}); diff --git a/functions/[[path]].js b/functions/[[path]].js new file mode 100644 index 0000000..95adb0d --- /dev/null +++ b/functions/[[path]].js @@ -0,0 +1,35 @@ +import authWorker from "../auth-worker/src/index.js"; + +const AUTH_ROUTE = /^\/(?:health|auth(?:\/|$)|api(?:\/|$))/; + +export async function onRequest(context) { + const { request, env } = context; + const url = new URL(request.url); + + // Keep authentication and API requests on the Pages origin so Safari treats + // the HttpOnly session cookie as first-party rather than a third-party cookie. + if (AUTH_ROUTE.test(url.pathname)) { + return authWorker.fetch(request, { ...env, APP_ORIGIN: url.origin }); + } + + // Pass static assets through to the Pages asset pipeline. + const response = await context.next(); + const contentType = response.headers.get("content-type") || ""; + if (!contentType.includes("text/html")) return response; + + const html = await response.text(); + const configured = html.replace( + "", + '' + ); + const headers = new Headers(response.headers); + headers.delete("content-length"); + headers.delete("content-encoding"); + headers.delete("etag"); + headers.set("cache-control", "no-store"); + return new Response(configured, { + status: response.status, + statusText: response.statusText, + headers + }); +}