/** * The connect links' last step (ROUTES-AUDIT 16, 23 Nov 2026): Google's and Spotify's answer comes back in a browser, * carrying a state that names the database that asked, not the browser that consented. Someone could start a connect * on their own account and send the link to another person, whose consent would connect THEIR calendar into the * sender's database. So the answer is never traded on arrival: the page first names the Cotton account it goes into * (its number, all but the last four hidden) and trades the code only when the person taps Connect, a POST that * carries the same state and code. A person who reads a number not theirs closes the page, and nothing is connected. */import { esc, page } from"./oauth.ts";/** The callback's parameters: the query on Google's GET, the form on the Connect tap's POST. */exportasyncfunction callbackUrl(request: Request, url: URL): Promise<URL> {if (request.method !== "POST") return url;const form = new URLSearchParams(await request.text());const out = new URL(url.toString()); out.search = "";for (const key of ["state", "code", "error"]) {const value = form.get(key);if (value !== null) out.searchParams.set(key, value); }return out;}/** A number with all but its last four digits hidden: "+91 ••••••0009". */exportfunction masked(phone: string | null): string {if (!phone) return"this Cotton account";const digits = phone.replace(/\D/g, "");return`the Cotton account of ${"•".repeat(Math.max(0, digits.length - 4))}${digits.slice(-4)}`;}/** Whether this answer must be confirmed first: Google's arrival with a code (not a refusal, not the tap itself). */exportfunction wantsConfirming(request: Request, url: URL): boolean {return request.method === "GET" && Boolean(url.searchParams.get("code")) && !url.searchParams.get("error");}/** The page that names where the connection goes, with one Connect button (words: drafts for the owner). */exportfunction confirmPage(service: string, account: string, action: string, url: URL): Response {const hidden = ["state", "code"].map((k) => `<input type="hidden" name="${k}" value="${esc(url.searchParams.get(k) ?? "")}">`).join("");return page(`Connect ${service}?`,`<p>This connects ${esc(service)} to ${esc(account)}.</p><p>If that is not your number, close this page: someone else may have sent you this link, and nothing is connected.</p><form method="post" action="${esc(action)}">${hidden}<button type="submit">Connect</button></form>`, );}