95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
import { createCookie } from "react-router";
|
|
|
|
import { loadRequiredWebConfig, resolveHostOnlyCookiePolicy } from "@hivegrid/config";
|
|
import { isSignupCreationRequestKey } from "~/domain/tenant-bootstrap/signupCreationRequestKey.server";
|
|
|
|
export const REGISTRATION_RESEND_RECOVERY_TTL_MS = 24 * 60 * 60 * 1000;
|
|
export const REGISTRATION_RESEND_RECOVERY_COOKIE = "__Host-hivegrid_registration_resend";
|
|
export const LOCAL_REGISTRATION_RESEND_RECOVERY_COOKIE = "hivegrid_registration_resend";
|
|
const CLOCK_SKEW_MS = 60 * 1000;
|
|
const MAX_EMAIL_LENGTH = 320;
|
|
|
|
export type RegistrationResendRecovery = {
|
|
email: string;
|
|
creationRequestKey: string;
|
|
issuedAt: number;
|
|
};
|
|
|
|
function getRecoveryCookie() {
|
|
const config = loadRequiredWebConfig();
|
|
if (!config.sessionSecret) {
|
|
throw new Error("SESSION_SECRET is required for registration resend recovery cookies");
|
|
}
|
|
const policy = resolveHostOnlyCookiePolicy({
|
|
nodeEnv: config.nodeEnv,
|
|
routing: config.routing,
|
|
secureName: REGISTRATION_RESEND_RECOVERY_COOKIE,
|
|
localName: LOCAL_REGISTRATION_RESEND_RECOVERY_COOKIE,
|
|
maxAge: Math.ceil(REGISTRATION_RESEND_RECOVERY_TTL_MS / 1000),
|
|
});
|
|
return createCookie(policy.name, {
|
|
httpOnly: policy.httpOnly,
|
|
sameSite: policy.sameSite,
|
|
path: policy.path,
|
|
secure: policy.secure,
|
|
maxAge: policy.maxAge,
|
|
secrets: [config.sessionSecret],
|
|
});
|
|
}
|
|
|
|
export async function readRegistrationResendRecovery(request: Request, now = Date.now()): Promise<RegistrationResendRecovery | null> {
|
|
try {
|
|
const parsed: unknown = await getRecoveryCookie().parse(request.headers.get("Cookie"));
|
|
if (!isRecoveryPayload(parsed, now)) return null;
|
|
return {
|
|
email: normalizeEmail(parsed.email),
|
|
creationRequestKey: parsed.creationRequestKey,
|
|
issuedAt: parsed.issuedAt,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function commitRegistrationResendRecovery(
|
|
input: Pick<RegistrationResendRecovery, "email" | "creationRequestKey">,
|
|
now = Date.now()
|
|
): Promise<string> {
|
|
const email = normalizeEmail(input.email);
|
|
if (!isValidEmailBinding(email) || !isSignupCreationRequestKey(input.creationRequestKey)) {
|
|
throw new Error("Registration resend recovery payload is invalid");
|
|
}
|
|
return await getRecoveryCookie().serialize({
|
|
email,
|
|
creationRequestKey: input.creationRequestKey,
|
|
issuedAt: now,
|
|
} satisfies RegistrationResendRecovery);
|
|
}
|
|
|
|
export async function clearRegistrationResendRecovery(): Promise<string> {
|
|
return await getRecoveryCookie().serialize("", { maxAge: 0 });
|
|
}
|
|
|
|
function isRecoveryPayload(value: unknown, now: number): value is RegistrationResendRecovery {
|
|
if (!value || typeof value !== "object") return false;
|
|
const payload = value as Partial<RegistrationResendRecovery>;
|
|
const email = typeof payload.email === "string" ? normalizeEmail(payload.email) : "";
|
|
return (
|
|
isValidEmailBinding(email) &&
|
|
typeof payload.creationRequestKey === "string" &&
|
|
isSignupCreationRequestKey(payload.creationRequestKey) &&
|
|
typeof payload.issuedAt === "number" &&
|
|
Number.isFinite(payload.issuedAt) &&
|
|
payload.issuedAt <= now + CLOCK_SKEW_MS &&
|
|
now - payload.issuedAt < REGISTRATION_RESEND_RECOVERY_TTL_MS
|
|
);
|
|
}
|
|
|
|
function normalizeEmail(value: string): string {
|
|
return value.trim().toLowerCase();
|
|
}
|
|
|
|
function isValidEmailBinding(value: string): boolean {
|
|
return value.length > 0 && value.length <= MAX_EMAIL_LENGTH && value.includes("@");
|
|
}
|