From d7935d30ce93455c27946450581dfc7d14e57e8f Mon Sep 17 00:00:00 2001 From: = Date: Fri, 21 Feb 2025 14:47:04 +0530 Subject: [PATCH] feat: made the function shared one --- .../secret-sharing/secret-sharing-service.ts | 52 +++++++------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/backend/src/services/secret-sharing/secret-sharing-service.ts b/backend/src/services/secret-sharing/secret-sharing-service.ts index f907649e7..98ccc988b 100644 --- a/backend/src/services/secret-sharing/secret-sharing-service.ts +++ b/backend/src/services/secret-sharing/secret-sharing-service.ts @@ -34,22 +34,7 @@ export const secretSharingServiceFactory = ({ orgDAL, kmsService }: TSecretSharingServiceFactoryDep) => { - const createSharedSecret = async ({ - actor, - actorId, - orgId, - actorAuthMethod, - actorOrgId, - secretValue, - name, - password, - accessType, - expiresAt, - expiresAfterViews - }: TCreateSharedSecretDTO) => { - const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorAuthMethod, actorOrgId); - if (!permission) throw new ForbiddenRequestError({ name: "User is not a part of the specified organization" }); - + const $validateSharedSecretExpiry = (expiresAt: string) => { if (new Date(expiresAt) < new Date()) { throw new BadRequestError({ message: "Expiration date cannot be in the past" }); } @@ -66,6 +51,24 @@ export const secretSharingServiceFactory = ({ if (expiryTime - currentTime < fiveMins) { throw new BadRequestError({ message: "Expiration time cannot be less than 5 mins" }); } + }; + + const createSharedSecret = async ({ + actor, + actorId, + orgId, + actorAuthMethod, + actorOrgId, + secretValue, + name, + password, + accessType, + expiresAt, + expiresAfterViews + }: TCreateSharedSecretDTO) => { + const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorAuthMethod, actorOrgId); + if (!permission) throw new ForbiddenRequestError({ name: "User is not a part of the specified organization" }); + $validateSharedSecretExpiry(expiresAt); if (secretValue.length > 10_000) { throw new BadRequestError({ message: "Shared secret value too long" }); @@ -105,22 +108,7 @@ export const secretSharingServiceFactory = ({ expiresAfterViews, accessType }: TCreatePublicSharedSecretDTO) => { - if (new Date(expiresAt) < new Date()) { - throw new BadRequestError({ message: "Expiration date cannot be in the past" }); - } - - // Limit Expiry Time to 1 month - const expiryTime = new Date(expiresAt).getTime(); - const currentTime = new Date().getTime(); - const thirtyDays = 30 * 24 * 60 * 60 * 1000; - if (expiryTime - currentTime > thirtyDays) { - throw new BadRequestError({ message: "Expiration date cannot exceed more than 30 days" }); - } - - const fiveMins = 5 * 60 * 1000; - if (expiryTime - currentTime < fiveMins) { - throw new BadRequestError({ message: "Expiration time cannot be less than 5 mins" }); - } + $validateSharedSecretExpiry(expiresAt); const encryptWithRoot = kmsService.encryptWithRootKey(); const encryptedSecret = encryptWithRoot(Buffer.from(secretValue));