mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
Adds encrypted credential storage using a new keyring system: - New `encryptedKey` column with AES-256-GCM encryption - Decryption in getCalendarsEvents with fallback to legacy `key` - buildCredentialCreateData service for credential creation - Phase 1: Google Calendar only, other integrations follow
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { z } from "zod";
|
|
|
|
import { symmetricEncrypt } from "@calcom/lib/crypto";
|
|
import logger from "@calcom/lib/logger";
|
|
import { defaultHandler } from "@calcom/lib/server/defaultHandler";
|
|
import { defaultResponder } from "@calcom/lib/server/defaultResponder";
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
|
import { BuildCalendarService } from "../lib";
|
|
|
|
const bodySchema = z
|
|
.object({
|
|
username: z.string(),
|
|
password: z.string(),
|
|
url: z.string().url(),
|
|
})
|
|
.strict();
|
|
|
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|
const body = bodySchema.parse(req.body);
|
|
// Get user
|
|
const user = await prisma.user.findFirstOrThrow({
|
|
where: {
|
|
id: req.session?.user?.id,
|
|
},
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
},
|
|
});
|
|
|
|
const data = {
|
|
type: "exchange2016_calendar",
|
|
key: symmetricEncrypt(JSON.stringify(body), process.env.CALENDSO_ENCRYPTION_KEY || ""),
|
|
userId: user.id,
|
|
teamId: null,
|
|
appId: "exchange2016-calendar",
|
|
invalid: false,
|
|
delegationCredentialId: null,
|
|
};
|
|
|
|
try {
|
|
const dav = BuildCalendarService({
|
|
id: 0,
|
|
user: { email: user.email },
|
|
...data,
|
|
encryptedKey: null,
|
|
});
|
|
await dav?.listCalendars();
|
|
await prisma.credential.create({
|
|
data,
|
|
});
|
|
} catch (reason) {
|
|
logger.error("Could not add this exchange account", reason);
|
|
return res.status(500).json({ message: "Could not add this exchange account" });
|
|
}
|
|
|
|
return { url: getInstalledAppPath({ variant: "calendar", slug: "exchange2016-calendar" }) };
|
|
}
|
|
|
|
async function getHandler() {
|
|
return { url: "/apps/exchange2016-calendar/setup" };
|
|
}
|
|
|
|
export default defaultHandler({
|
|
GET: Promise.resolve({ default: defaultResponder(getHandler) }),
|
|
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
|
|
});
|