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 <[email protected]>
This commit is contained in:
1 parent
fc1d6ae402
commit
d9df9b3d01
8 files changed
+160
-21
No files matched your search
@@ -1,3 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Suspense, useEffect, useState } from "react";
|
||||
|
||||
@@ -74,21 +76,19 @@ const useRecordingDownload = () => {
|
||||
useEffect(
|
||||
function refactorMeWithoutEffect() {
|
||||
if (data && data.download_link) {
|
||||
window.location.href = data.download_link;
|
||||
window.open(data.download_link, "_blank", "noopener,noreferrer");
|
||||
}
|
||||
},
|
||||
[data]
|
||||
);
|
||||
return {
|
||||
setRecordingId: (newRecordingId: string) => {
|
||||
// may be a way to do this by default, but this is easy enough.
|
||||
if (recordingId === newRecordingId && data) {
|
||||
window.location.href = data.download_link;
|
||||
window.open(data.download_link, "_blank", "noopener,noreferrer");
|
||||
}
|
||||
if (!isFetching) {
|
||||
setRecordingId(newRecordingId);
|
||||
}
|
||||
// assume it is still fetching, do nothing.
|
||||
},
|
||||
isFetching,
|
||||
recordingId,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
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 };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user