From 5c5d9d34063ed7e04d6dcd6d5a6682ee7128c244 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Wed, 15 Dec 2021 15:02:39 +0100 Subject: [PATCH] Fixes zoom expiry date (#1315) * Fixes zoom expiry date * Ensure backwards compatibility with old zoom connections Co-authored-by: Bailey Pumfleet --- lib/integrations/Zoom/ZoomVideoApiAdapter.ts | 12 ++++++++---- pages/api/integrations/zoomvideo/callback.ts | 8 ++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/integrations/Zoom/ZoomVideoApiAdapter.ts b/lib/integrations/Zoom/ZoomVideoApiAdapter.ts index 18570bf9..8ac07b66 100644 --- a/lib/integrations/Zoom/ZoomVideoApiAdapter.ts +++ b/lib/integrations/Zoom/ZoomVideoApiAdapter.ts @@ -60,7 +60,8 @@ export interface ZoomEventResult { interface ZoomToken { scope: "meeting:write"; - expires_in: number; + expiry_date: number; + expires_in?: number; // deprecated, purely for backwards compatibility; superseeded by expiry_date. token_type: "bearer"; access_token: string; refresh_token: string; @@ -68,7 +69,7 @@ interface ZoomToken { const zoomAuth = (credential: Credential) => { const credentialKey = credential.key as unknown as ZoomToken; - const isExpired = (expiryDate: number) => expiryDate < +new Date(); + const isExpired = (expiryDate: number) => expiryDate < Date.now(); const authHeader = "Basic " + Buffer.from(process.env.ZOOM_CLIENT_ID + ":" + process.env.ZOOM_CLIENT_SECRET).toString("base64"); @@ -87,6 +88,9 @@ const zoomAuth = (credential: Credential) => { }) .then(handleErrorsJson) .then(async (responseBody) => { + // set expiry date as offset from current time. + responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000); + delete responseBody.expires_in; // Store new tokens in database. await prisma.credential.update({ where: { @@ -96,14 +100,14 @@ const zoomAuth = (credential: Credential) => { key: responseBody, }, }); + credentialKey.expiry_date = responseBody.expiry_date; credentialKey.access_token = responseBody.access_token; - credentialKey.expires_in = Math.round(+new Date() / 1000 + responseBody.expires_in); return credentialKey.access_token; }); return { getToken: () => - !isExpired(credentialKey.expires_in) + !isExpired(credentialKey.expires_in || credentialKey.expiry_date) ? Promise.resolve(credentialKey.access_token) : refreshAccessToken(credentialKey.refresh_token), }; diff --git a/pages/api/integrations/zoomvideo/callback.ts b/pages/api/integrations/zoomvideo/callback.ts index 458decec..2d6e7479 100644 --- a/pages/api/integrations/zoomvideo/callback.ts +++ b/pages/api/integrations/zoomvideo/callback.ts @@ -30,7 +30,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }, } ); - const json = await result.json(); + + const responseBody = await result.json(); + + responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000); + delete responseBody.expires_in; await prisma.user.update({ where: { @@ -40,7 +44,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) credentials: { create: { type: "zoom_video", - key: json, + key: responseBody, }, }, },