-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsetTokenInCalCom.ts
More file actions
67 lines (60 loc) · 2.06 KB
/
setTokenInCalCom.ts
File metadata and controls
67 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { NextApiRequest } from "next";
import { symmetricEncrypt } from "../../lib/crypto";
import {
CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY,
CALCOM_CREDENTIAL_SYNC_SECRET,
CALCOM_CREDENTIAL_SYNC_HEADER_NAME,
CALCOM_ADMIN_API_KEY,
} from "../../constants";
import { generateGoogleCalendarAccessToken, generateZoomAccessToken } from "../../lib/integrations";
export default async function handler(req: NextApiRequest, res) {
const isInvalid = req.query.invalid === "1";
const userId = parseInt(req.query.userId as string, 10);
const appSlug = req.query.appSlug;
try {
let accessToken;
if (appSlug === "google-calendar") {
accessToken = await generateGoogleCalendarAccessToken();
} else if (appSlug === "zoom") {
accessToken = await generateZoomAccessToken();
} else {
throw new Error(`Unhandled appSlug: ${appSlug}`);
}
if (!accessToken) {
return res.status(500).json({ error: "Could not get access token" });
}
const result = await fetch(
`http://localhost:3002/api/v1/credential-sync?apiKey=${CALCOM_ADMIN_API_KEY}&userId=${userId}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
[CALCOM_CREDENTIAL_SYNC_HEADER_NAME]: CALCOM_CREDENTIAL_SYNC_SECRET,
},
body: JSON.stringify({
appSlug,
encryptedKey: symmetricEncrypt(
JSON.stringify({
access_token: isInvalid ? "1233231231231" : accessToken,
}),
CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY
),
}),
}
);
const clonedResult = result.clone();
try {
if (result.ok) {
const json = await result.json();
return res.status(200).json(json);
} else {
return res.status(400).json({ error: await clonedResult.text() });
}
} catch (e) {
return res.status(400).json({ error: await clonedResult.text() });
}
} catch (error) {
console.error(error);
return res.status(400).json({ message: "Internal Server Error", error: error.message });
}
}