// Copyright 2026 The KCP Authors. // SPDX-License-Identifier: Apache-2.0 /** * Browser helpers for the socialweb OAuth popup shell. The popup only carries * the authorization code back to the page; token exchange, DPoP keys and * session storage stay outside this module. */ export const OAUTH_CALLBACK_MESSAGE = "socialweb-oauth-callback"; export const OAUTH_CHANNEL = "socialweb-oauth"; export const OAUTH_POPUP_TIMEOUT_MS = 5 * 60 * 1000; /** Validate a BroadcastChannel callback payload (same-origin by construction): a code or an error. */ export function isOAuthCallbackMessage(data) { return Boolean( data && data.type === OAUTH_CALLBACK_MESSAGE && (typeof data.code === "string" || typeof data.error === "string"), ); } /** @type {Window | null} */ let currentPopup = null; let cancelCurrent = null; /** Reject the pending openOAuthPopup, e.g. when its tab was left open. */ export function cancelOAuthPopup() { cancelCurrent?.(new Error("OAuth popup was cancelled")); } /** * A callback answers this URL only when it carries the URL's state: the * popup window is reused by name, so a late answer to an earlier URL can * arrive while this one waits. * * @param {Record} data * @param {string} url */ export function callbackMatchesURL(data, url) { let state = null; try { state = new URL(url).searchParams.get("state"); } catch { // Not a URL; nothing to match. } return !state || data?.state === state; } /** * @param {string} url * @param {{ width?: number, height?: number }} [options] * @returns {Window | null} */ function openPopupWindow(url, options = {}) { const width = options.width ?? 520; const height = options.height ?? 720; const left = Math.round(window.screenX + (window.outerWidth - width) / 2); const top = Math.round(window.screenY + (window.outerHeight - height) / 2); return window.open( url, "socialweb-oauth", `popup=yes,width=${width},height=${height},left=${left},top=${top}`, ); } /** * Validate a postMessage event as the callback from the OAuth popup. The * message must come from this origin and from the popup we opened. * * @param {MessageEvent} event * @param {Window | null} popup * @param {string} origin * @returns {boolean} */ export function isValidOAuthCallback(event, popup, origin) { return Boolean( event && popup && event.origin === origin && event.source === popup && event.data && event.data.type === OAUTH_CALLBACK_MESSAGE, ); } /** * Open the authorization URL in a popup. Resolves with the validated callback * message, rejects if the popup is blocked, closed by the user, or times out. * * @param {string} url * @param {{ width?: number, height?: number, timeoutMs?: number }} [options] * @returns {Promise>} */ export function openOAuthPopup(url, options = {}) { const timeoutMs = options.timeoutMs ?? OAUTH_POPUP_TIMEOUT_MS; const popup = openPopupWindow(url, options); if (!popup) { return Promise.reject(new Error("OAuth popup was blocked")); } currentPopup = popup; return new Promise((resolve, reject) => { // Do not fail when the WindowProxy reports closed: COOP can flip that // while the callback page is still loading and broadcasting its message. const timeout = setTimeout( () => fail(new Error("OAuth popup timed out")), timeoutMs, ); const onMessage = (event) => { if (isValidOAuthCallback(event, popup, location.origin) && callbackMatchesURL(event.data, url)) { succeed(event.data); } }; let channel = null; try { channel = new BroadcastChannel(OAUTH_CHANNEL); channel.onmessage = (event) => { if (isOAuthCallbackMessage(event.data) && callbackMatchesURL(event.data, url)) succeed(event.data); }; } catch { channel = null; } function cleanup() { clearTimeout(timeout); window.removeEventListener("message", onMessage); channel?.close(); if (currentPopup === popup) { currentPopup = null; cancelCurrent = null; } } function finish(message, error) { cleanup(); try { popup.close(); } catch { // Already closed. } if (error) { reject(error); } else { resolve(message); } } function succeed(message) { finish(message, null); } function fail(error) { finish(null, error); } window.addEventListener("message", onMessage); cancelCurrent = fail; }); } /** * Subscribe to validated callback messages from the current popup. * * @param {(message: Record) => void} fn * @returns {() => void} unsubscribe */ export function onOAuthCallback(fn) { const handler = (event) => { if (isValidOAuthCallback(event, currentPopup, location.origin)) { fn(event.data); } }; window.addEventListener("message", handler); return () => window.removeEventListener("message", handler); }