ebe0c77f7e
CI / build (push) Has been cancelled
Reopen advanced edits from their saved recipe, seed or take over the quick editor from them, refresh thumbnails after a save, and add an edited-only download. Also fixes the Lumenroom library collapsing to slivers on large shoots and documents deploying both containers on a new host. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
129 lines
3.8 KiB
TypeScript
129 lines
3.8 KiB
TypeScript
/**
|
|
* Signed hand-off tokens for the advanced editor bridge.
|
|
*
|
|
* The app never hands Lumenroom a password. Instead it mints a short-lived
|
|
* token scoped to one user and one event; Lumenroom verifies it with the same
|
|
* `BRIDGE_SECRET`, exchanges it for a longer service token, and uses that token
|
|
* for the server-to-server photo and save-back calls.
|
|
*/
|
|
|
|
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
|
|
const VERSION = "g1";
|
|
|
|
export interface BridgeTokenPayload {
|
|
/** Bridge session row id. */
|
|
sid: string;
|
|
/** Event id the token is limited to. */
|
|
eid: string;
|
|
/** Glenloch user id. */
|
|
uid: string;
|
|
email: string;
|
|
name: string;
|
|
/** `login` is the one browser hop; `service` is used server-to-server. */
|
|
kind: "login" | "service";
|
|
/** Unix seconds. */
|
|
exp: number;
|
|
/**
|
|
* Browser-facing URL to return to after editing. Captured from the request
|
|
* that opened the editor, because the server-to-server origin is not the
|
|
* address the user's browser used.
|
|
*/
|
|
ret?: string;
|
|
}
|
|
|
|
export const BRIDGE_LOGIN_TTL_SECONDS = 10 * 60;
|
|
export const BRIDGE_SESSION_TTL_SECONDS = 12 * 60 * 60;
|
|
|
|
function secret(): string | null {
|
|
return process.env.BRIDGE_SECRET?.trim() || null;
|
|
}
|
|
|
|
/** Advanced editing needs both a shared secret and somewhere to send the user. */
|
|
export function bridgeConfigured(): boolean {
|
|
return Boolean(secret() && advancedEditorUrl());
|
|
}
|
|
|
|
export function advancedEditorUrl(): string | null {
|
|
const url = process.env.LUMENROOM_URL?.trim();
|
|
return url ? url.replace(/\/+$/, "") : null;
|
|
}
|
|
|
|
function base64url(value: Buffer | string): string {
|
|
return Buffer.from(value)
|
|
.toString("base64")
|
|
.replace(/\+/g, "-")
|
|
.replace(/\//g, "_")
|
|
.replace(/=+$/, "");
|
|
}
|
|
|
|
function fromBase64url(value: string): Buffer {
|
|
return Buffer.from(value.replace(/-/g, "+").replace(/_/g, "/"), "base64");
|
|
}
|
|
|
|
function sign(body: string): Buffer {
|
|
return createHmac("sha256", secret() ?? "").update(body).digest();
|
|
}
|
|
|
|
export function signBridgeToken(payload: BridgeTokenPayload): string {
|
|
const body = base64url(JSON.stringify(payload));
|
|
return `${VERSION}.${body}.${base64url(sign(body))}`;
|
|
}
|
|
|
|
/** Null when the token is malformed, signed with another secret, or expired. */
|
|
export function verifyBridgeToken(
|
|
token: string | null | undefined,
|
|
now: number = Date.now(),
|
|
): BridgeTokenPayload | null {
|
|
if (!token) return null;
|
|
const parts = token.split(".");
|
|
if (parts.length !== 3 || parts[0] !== VERSION) return null;
|
|
const [, body, signature] = parts;
|
|
|
|
const expected = sign(body);
|
|
let supplied: Buffer;
|
|
try {
|
|
supplied = fromBase64url(signature);
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (supplied.length !== expected.length) return null;
|
|
if (!timingSafeEqual(supplied, expected)) return null;
|
|
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(fromBase64url(body).toString("utf8"));
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (!parsed || typeof parsed !== "object") return null;
|
|
|
|
const payload = parsed as Partial<BridgeTokenPayload>;
|
|
if (
|
|
typeof payload.sid !== "string" ||
|
|
typeof payload.eid !== "string" ||
|
|
typeof payload.uid !== "string" ||
|
|
typeof payload.email !== "string" ||
|
|
typeof payload.name !== "string" ||
|
|
(payload.kind !== "login" && payload.kind !== "service") ||
|
|
typeof payload.exp !== "number"
|
|
) {
|
|
return null;
|
|
}
|
|
if (payload.exp * 1000 <= now) return null;
|
|
return payload as BridgeTokenPayload;
|
|
}
|
|
|
|
/**
|
|
* Where the advanced editor sends a user back to after finishing. Prefer the
|
|
* configured public origin; `NEXTAUTH_URL` is only a fallback because compose
|
|
* defaults it to localhost.
|
|
*/
|
|
export function bridgeReturnUrl(eventId: string): string | null {
|
|
const origin = (
|
|
process.env.PUBLIC_ORIGIN?.trim() || process.env.NEXTAUTH_URL?.trim()
|
|
);
|
|
if (!origin) return null;
|
|
return `${origin.replace(/\/+$/, "")}/events/${eventId}`;
|
|
}
|