From 23f605bda72804e7eaef1c99c6982f5d338bf005 Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Tue, 28 Jan 2025 22:37:27 +0800 Subject: [PATCH] misc: added credential hash --- backend/src/lib/crypto/encryption.ts | 2 +- backend/src/services/app-connection/app-connection-fns.ts | 4 +++- backend/src/services/app-connection/app-connection-schemas.ts | 2 ++ backend/src/services/app-connection/app-connection-service.ts | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/backend/src/lib/crypto/encryption.ts b/backend/src/lib/crypto/encryption.ts index 258a6d285..f495681f1 100644 --- a/backend/src/lib/crypto/encryption.ts +++ b/backend/src/lib/crypto/encryption.ts @@ -116,7 +116,7 @@ export const decryptAsymmetric = ({ ciphertext, nonce, publicKey, privateKey }: export const generateSymmetricKey = (size = 32) => crypto.randomBytes(size).toString("base64"); -export const generateHash = (value: string) => crypto.createHash("sha256").update(value).digest("hex"); +export const generateHash = (value: string | Buffer) => crypto.createHash("sha256").update(value).digest("hex"); export const generateAsymmetricKeyPair = () => { const pair = nacl.box.keyPair(); diff --git a/backend/src/services/app-connection/app-connection-fns.ts b/backend/src/services/app-connection/app-connection-fns.ts index 7d7428815..d4c9f97ad 100644 --- a/backend/src/services/app-connection/app-connection-fns.ts +++ b/backend/src/services/app-connection/app-connection-fns.ts @@ -1,4 +1,5 @@ import { TAppConnections } from "@app/db/schemas/app-connections"; +import { generateHash } from "@app/lib/crypto/encryption"; import { AppConnection } from "@app/services/app-connection/app-connection-enums"; import { TAppConnectionServiceFactoryDep } from "@app/services/app-connection/app-connection-service"; import { TAppConnection, TAppConnectionConfig } from "@app/services/app-connection/app-connection-types"; @@ -112,6 +113,7 @@ export const decryptAppConnection = async ( encryptedCredentials: appConnection.encryptedCredentials, orgId: appConnection.orgId, kmsService - }) + }), + credentialsHash: generateHash(appConnection.encryptedCredentials) } as TAppConnection; }; diff --git a/backend/src/services/app-connection/app-connection-schemas.ts b/backend/src/services/app-connection/app-connection-schemas.ts index ce5e877fd..ef3c16cf8 100644 --- a/backend/src/services/app-connection/app-connection-schemas.ts +++ b/backend/src/services/app-connection/app-connection-schemas.ts @@ -10,6 +10,8 @@ export const BaseAppConnectionSchema = AppConnectionsSchema.omit({ encryptedCredentials: true, app: true, method: true +}).extend({ + credentialsHash: z.string().optional() }); export const GenericCreateAppConnectionFieldsSchema = (app: AppConnection) => diff --git a/backend/src/services/app-connection/app-connection-service.ts b/backend/src/services/app-connection/app-connection-service.ts index 00006a71c..8e51caf30 100644 --- a/backend/src/services/app-connection/app-connection-service.ts +++ b/backend/src/services/app-connection/app-connection-service.ts @@ -2,6 +2,7 @@ import { ForbiddenError, subject } from "@casl/ability"; import { OrgPermissionAppConnectionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission"; import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service"; +import { generateHash } from "@app/lib/crypto/encryption"; import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors"; import { DiscriminativePick, OrgServiceActor } from "@app/lib/types"; import { AppConnection } from "@app/services/app-connection/app-connection-enums"; @@ -185,6 +186,7 @@ export const appConnectionServiceFactory = ({ return { ...connection, + credentialsHash: generateHash(connection.encryptedCredentials), credentials: validatedCredentials }; });