feat: add encryptedKey column to Credential table for calendar integrations (#27154)

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
This commit is contained in:
Alex van Andel authored and GitHub committed 2026-01-30 09:15:13 -03:00
1 parent 80e4b1af1f
commit fc9c26e8dd
36 files changed
+308 -34

No files matched your search

@@ -13,6 +13,7 @@ type CredentialCreateInput = {
userId: number;
appId: string;
delegationCredentialId?: string | null;
encryptedKey?: string | null;
};
type CredentialUpdateInput = {
@@ -45,7 +46,9 @@ export class CredentialRepository {
}
static async create(data: CredentialCreateInput) {
const credential = await prisma.credential.create({ data: { ...data } });
const credential = await prisma.credential.create({
data,
});
return buildNonDelegationCredential(credential);
}
static async findByAppIdAndUserId({
@@ -81,7 +84,7 @@ export class CredentialRepository {
static async findFirstByIdWithKeyAndUser({ id }: { id: number }) {
const credential = await prisma.credential.findUnique({
where: { id },
select: { ...safeCredentialSelect, key: true },
select: { ...safeCredentialSelect, key: true, encryptedKey: true },
});
return buildNonDelegationCredential(credential);
}
@@ -276,15 +279,17 @@ export class CredentialRepository {
type,
key,
appId,
encryptedKey,
}: {
userId: number;
delegationCredentialId: string;
type: string;
key: Prisma.InputJsonValue;
appId: string;
encryptedKey?: string | null;
}) {
return prisma.credential.create({
data: { userId, delegationCredentialId, type, key, appId },
data: { userId, delegationCredentialId, type, key, appId, ...(encryptedKey && { encryptedKey }) },
});
}
@@ -0,0 +1,43 @@
import { encryptSecret } from "@calcom/lib/crypto/keyring";
export type CredentialCreateData = {
type: string;
key: object;
userId: number;
appId: string;
delegationCredentialId?: string | null;
encryptedKey?: string | null;
};
/**
* Builds the data object for creating a credential, including the encrypted key.
* This service handles the encryption logic so the repository stays focused on data access.
*
* @param data The credential data without encryptedKey
* @returns The credential data with encryptedKey populated if encryption key is available
*/
export function buildCredentialCreateData(data: {
type: string;
key: object;
userId: number;
appId: string;
delegationCredentialId?: string | null;
}): CredentialCreateData {
const aad = {
type: data.type,
};
let encryptedKey: ReturnType<typeof encryptSecret> | null = null;
try {
encryptedKey = encryptSecret({ ring: "CREDENTIALS", plaintext: JSON.stringify(data.key), aad });
} catch {
// Encryption keyring not configured - this is expected in tests and some environments
// The encryptedKey will be null, which is fine for backwards compatibility
}
return {
...data,
key: data.key,
...(encryptedKey && { encryptedKey: JSON.stringify(encryptedKey) }),
};
}