22 lines
827 B
TypeScript
22 lines
827 B
TypeScript
import { createHash, randomBytes } from "node:crypto";
|
|
|
|
/**
|
|
* Long-lived, random, rotatable secret for a personal calendar-subscription
|
|
* URL (/calendar/<token>.ics) - treated like an API key, not a
|
|
* short-lived link: only its hash is stored, so a database leak alone
|
|
* can't be used to subscribe to anyone's calendar, and rotating it
|
|
* (overwriting the stored hash) immediately invalidates the old URL.
|
|
*/
|
|
export function generateCalendarFeedToken(): string {
|
|
return randomBytes(32).toString("hex");
|
|
}
|
|
|
|
export function hashCalendarFeedToken(token: string): string {
|
|
return createHash("sha256").update(token).digest("hex");
|
|
}
|
|
|
|
export function calendarFeedUrl(token: string): string {
|
|
const serverUrl = process.env.NEXT_PUBLIC_SERVER_URL || "http://localhost:3000";
|
|
return `${serverUrl}/calendar/${token}.ics`;
|
|
}
|