// Cloudflare Pages Middleware — Server-Side Passcode & Magic Link Gate // Set an Environment Variable in Cloudflare Pages named: RESUME_PASSCODES // You can put one code or multiple comma-separated codes, e.g.: // bri2026,acme-corp,design-assist-2026 // Share either the code itself OR a magic link: https://resume.bybri.ca/?key=acme-corp const COOKIE_NAME = "resume_access_token"; async function sha256Hex(str) { const data = new TextEncoder().encode(str); const hashBuffer = await crypto.subtle.digest("SHA-256", data); return Array.from(new Uint8Array(hashBuffer)) .map((b) => b.toString(16).padStart(2, "0")) .join(""); } function parseCookies(cookieHeader) { const cookies = {}; if (!cookieHeader) return cookies; for (const part of cookieHeader.split(";")) { const [k, ...v] = part.trim().split("="); if (k) cookies[k] = decodeURIComponent(v.join("=")); } return cookies; } function getValidCodes(env) { const raw = env.RESUME_PASSCODES || env.RESUME_PASSCODE || ""; return raw .split(",") .map((c) => c.trim()) .filter(Boolean); } function loginPageHtml(errorMsg = "") { return ` Private Resume Access

Private Resume

This document is shared by invitation only. Enter your access code below to continue.

${errorMsg ? `
${errorMsg}
` : ""}
`; } export async function onRequest(context) { const { request, env, next } = context; const url = new URL(request.url); // Always allow robots.txt if (url.pathname === "/robots.txt") { return next(); } const validCodes = getValidCodes(env); // If no passcode has been configured in Cloudflare yet, lock by default if (validCodes.length === 0) { return new Response( loginPageHtml("Access codes are not configured yet. Add RESUME_PASSCODES in Cloudflare Pages settings."), { status: 503, headers: { "Content-Type": "text/html; charset=UTF-8", "X-Robots-Tag": "noindex, nofollow, noarchive", }, } ); } // Handle manual lock / logout if (url.searchParams.get("logout") === "1") { return new Response(null, { status: 302, headers: { Location: "/", "Set-Cookie": `${COOKIE_NAME}=; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=0`, "X-Robots-Tag": "noindex, nofollow, noarchive", }, }); } // Precompute valid token hashes const validHashes = new Set(await Promise.all(validCodes.map((c) => sha256Hex(c)))); // 1. Check Magic Link (?key=...) const queryKey = url.searchParams.get("key"); if (queryKey) { const hashed = await sha256Hex(queryKey.trim()); if (validHashes.has(hashed)) { url.searchParams.delete("key"); return new Response(null, { status: 302, headers: { Location: url.pathname + url.search, "Set-Cookie": `${COOKIE_NAME}=${hashed}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=86400`, "X-Robots-Tag": "noindex, nofollow, noarchive", }, }); } else { return new Response(loginPageHtml("That access link is invalid or has been revoked."), { status: 401, headers: { "Content-Type": "text/html; charset=UTF-8", "X-Robots-Tag": "noindex, nofollow, noarchive", }, }); } } // 2. Check Passcode Form Submission (POST) if (request.method === "POST") { const formData = await request.formData(); const submitted = (formData.get("code") || "").toString().trim(); const hashed = await sha256Hex(submitted); if (validHashes.has(hashed)) { return new Response(null, { status: 302, headers: { Location: "/", "Set-Cookie": `${COOKIE_NAME}=${hashed}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=86400`, "X-Robots-Tag": "noindex, nofollow, noarchive", }, }); } else { return new Response(loginPageHtml("Incorrect access code. Please try again."), { status: 401, headers: { "Content-Type": "text/html; charset=UTF-8", "X-Robots-Tag": "noindex, nofollow, noarchive", }, }); } } // 3. Check Existing Session Cookie const cookies = parseCookies(request.headers.get("Cookie")); if (cookies[COOKIE_NAME] && validHashes.has(cookies[COOKIE_NAME])) { const response = await next(); const newHeaders = new Headers(response.headers); newHeaders.set("X-Robots-Tag", "noindex, nofollow, noarchive"); newHeaders.set("Cache-Control", "private, no-store"); return new Response(response.body, { status: response.status, statusText: response.statusText, headers: newHeaders, }); } // 4. Otherwise, serve the Passcode Gate return new Response(loginPageHtml(), { status: 401, headers: { "Content-Type": "text/html; charset=UTF-8", "X-Robots-Tag": "noindex, nofollow, noarchive", "Cache-Control": "private, no-store", }, }); }