Files
calendar/packages/lib/videoTokens.ts
T
d9df9b3d01 feat: recording url with 6 months expiry time (#18707)
* feat: store cal video recording on s3

* feat: create new route to fetch download link

* tests: add unit test

* fix: unit test

* fix: type error

* chore: add env var

* fix: open in new tab

* fix: type err

* chore: update test

* fix: import

* chore: use cal video recording token secret

---------

Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
2025-05-23 13:01:33 +01:00

36 lines
1.1 KiB
TypeScript

import { createHmac } from "crypto";
// 262992 minutes is 6 months
export function generateVideoToken(recordingId: string, expiresInMinutes = 262992) {
const secret = process.env.CAL_VIDEO_RECORDING_TOKEN_SECRET || "default-secret-change-me";
const expires = Date.now() + expiresInMinutes * 60 * 1000;
const payload = `${recordingId}:${expires}`;
const hmac = createHmac("sha256", secret).update(payload).digest("hex");
return `${payload}:${hmac}`;
}
export function verifyVideoToken(token: string): { valid: boolean; recordingId?: string } {
try {
const [recordingId, expires, receivedHmac] = token.split(":");
const secret = process.env.CAL_VIDEO_RECORDING_TOKEN_SECRET || "default-secret-change-me";
if (Date.now() > parseInt(expires)) {
return { valid: false };
}
// Verify HMAC
const payload = `${recordingId}:${expires}`;
const expectedHmac = createHmac("sha256", secret).update(payload).digest("hex");
if (receivedHmac !== expectedHmac) {
return { valid: false };
}
return { valid: true, recordingId };
} catch {
return { valid: false };
}
}