|
| 1 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 2 | +import z from "zod"; |
| 3 | + |
| 4 | +import { appStoreMetadata } from "@calcom/app-store/appStoreMetaData"; |
| 5 | +import { APP_CREDENTIAL_SHARING_ENABLED } from "@calcom/lib/constants"; |
| 6 | +import { symmetricDecrypt } from "@calcom/lib/crypto"; |
| 7 | +import prisma from "@calcom/prisma"; |
| 8 | + |
| 9 | +const appCredentialWebhookRequestBodySchema = z.object({ |
| 10 | + // UserId of the cal.com user |
| 11 | + userId: z.number().int(), |
| 12 | + appSlug: z.string(), |
| 13 | + // Keys should be AES256 encrypted with the CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY |
| 14 | + keys: z.string(), |
| 15 | +}); |
| 16 | +/** */ |
| 17 | +export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 18 | + // Check that credential sharing is enabled |
| 19 | + if (!APP_CREDENTIAL_SHARING_ENABLED) { |
| 20 | + return res.status(403).json({ message: "Credential sharing is not enabled" }); |
| 21 | + } |
| 22 | + |
| 23 | + // Check that the webhook secret matches |
| 24 | + if ( |
| 25 | + req.headers[process.env.CALCOM_WEBHOOK_HEADER_NAME || "calcom-webhook-secret"] !== |
| 26 | + process.env.CALCOM_WEBHOOK_SECRET |
| 27 | + ) { |
| 28 | + return res.status(403).json({ message: "Invalid webhook secret" }); |
| 29 | + } |
| 30 | + |
| 31 | + const reqBody = appCredentialWebhookRequestBodySchema.parse(req.body); |
| 32 | + |
| 33 | + // Check that the user exists |
| 34 | + const user = await prisma.user.findUnique({ where: { id: reqBody.userId } }); |
| 35 | + |
| 36 | + if (!user) { |
| 37 | + return res.status(404).json({ message: "User not found" }); |
| 38 | + } |
| 39 | + |
| 40 | + const app = await prisma.app.findUnique({ |
| 41 | + where: { slug: reqBody.appSlug }, |
| 42 | + select: { slug: true }, |
| 43 | + }); |
| 44 | + |
| 45 | + if (!app) { |
| 46 | + return res.status(404).json({ message: "App not found" }); |
| 47 | + } |
| 48 | + |
| 49 | + // Search for the app's slug and type |
| 50 | + const appMetadata = appStoreMetadata[app.slug as keyof typeof appStoreMetadata]; |
| 51 | + |
| 52 | + if (!appMetadata) { |
| 53 | + return res.status(404).json({ message: "App not found. Ensure that you have the correct app slug" }); |
| 54 | + } |
| 55 | + |
| 56 | + // Decrypt the keys |
| 57 | + const keys = JSON.parse( |
| 58 | + symmetricDecrypt(reqBody.keys, process.env.CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY || "") |
| 59 | + ); |
| 60 | + |
| 61 | + // Can't use prisma upsert as we don't know the id of the credential |
| 62 | + const appCredential = await prisma.credential.findFirst({ |
| 63 | + where: { |
| 64 | + userId: reqBody.userId, |
| 65 | + appId: appMetadata.slug, |
| 66 | + }, |
| 67 | + select: { |
| 68 | + id: true, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + if (appCredential) { |
| 73 | + await prisma.credential.update({ |
| 74 | + where: { |
| 75 | + id: appCredential.id, |
| 76 | + }, |
| 77 | + data: { |
| 78 | + key: keys, |
| 79 | + }, |
| 80 | + }); |
| 81 | + return res.status(200).json({ message: `Credentials updated for userId: ${reqBody.userId}` }); |
| 82 | + } else { |
| 83 | + await prisma.credential.create({ |
| 84 | + data: { |
| 85 | + key: keys, |
| 86 | + userId: reqBody.userId, |
| 87 | + appId: appMetadata.slug, |
| 88 | + type: appMetadata.type, |
| 89 | + }, |
| 90 | + }); |
| 91 | + return res.status(200).json({ message: `Credentials created for userId: ${reqBody.userId}` }); |
| 92 | + } |
| 93 | +} |
0 commit comments