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

@@ -83,6 +83,7 @@ type UserCredentialType = {
teamId: number | null;
key: Prisma.JsonValue;
invalid: boolean | null;
encryptedKey: string | null;
};
export async function patchHandler(req: NextApiRequest) {
@@ -120,6 +120,7 @@ export class AppleCalendarService implements CredentialSyncCalendarApp {
appId: APPLE_CALENDAR_ID,
invalid: false,
delegationCredentialId: null,
encryptedKey: null,
};
const dav = BuildCalendarService({
@@ -40,6 +40,7 @@ export class IcsFeedService implements ICSFeedCalendarApp {
appId: ICS_CALENDAR,
invalid: false,
delegationCredentialId: null,
encryptedKey: null,
};
try {
@@ -88,6 +88,7 @@ export class CredentialsRepository {
id: true,
type: true,
key: true,
encryptedKey: true,
userId: true,
teamId: true,
appId: true,
+15 -13
View File
@@ -7,6 +7,7 @@ import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
import { buildCredentialCreateData } from "@calcom/features/credentials/services/CredentialDataService";
import { DelegationCredentialRepository } from "@calcom/features/delegation-credentials/repositories/DelegationCredentialRepository";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
@@ -70,19 +71,20 @@ export async function handleCreateCredentials() {
`Creating credentials for ${toProcessMembers.length} members out of ${delegatedMembers.length} delegated members in organization ${organization.id}`
);
const credentialCreationPromises = toProcessMembers.map(async (member) => {
try {
await CredentialRepository.create({
type: "google_calendar",
key: {
// The in-memory credential that we create for Delegation Credential has access_token as "NOOP_UNUSED_DELEGATION_TOKEN. This is to mark it is in DB, in case we need to identify that.
access_token: "NOOP_UNUSED_DELEGATION_TOKEN_DB",
},
userId: member.userId,
appId: "google-calendar",
delegationCredentialId: delegationCredential.id,
});
log.info(`Created credential for member ${member.userId}`);
const credentialCreationPromises = toProcessMembers.map(async (member) => {
try {
const credentialData = buildCredentialCreateData({
type: "google_calendar",
key: {
// The in-memory credential that we create for Delegation Credential has access_token as "NOOP_UNUSED_DELEGATION_TOKEN. This is to mark it is in DB, in case we need to identify that.
access_token: "NOOP_UNUSED_DELEGATION_TOKEN_DB",
},
userId: member.userId,
appId: "google-calendar",
delegationCredentialId: delegationCredential.id,
});
await CredentialRepository.create(credentialData);
log.info(`Created credential for member ${member.userId}`);
} catch (error) {
log.error(`Error creating credential for member ${member.userId}:`, safeStringify(error));
throw error;
@@ -10,6 +10,7 @@ type MockCredential = {
userId: number | null;
user: { email: string; name: string | null } | null;
key: { access_token: string };
encryptedKey: string | null;
invalid: boolean | null;
teamId: number | null;
team: { name: string } | null;
@@ -28,6 +29,7 @@ const createMockCredential = (overrides: Partial<MockCredential> = {}): MockCred
team: null,
user: { email: "[email protected]", name: "Test User" },
key: { access_token: "mock_token_123" },
encryptedKey: null,
delegationCredentialId: "delegation-123",
appId: "google-calendar",
invalid: false,