From 069651bdb438d2b88ccae16338e513fd7e89768f Mon Sep 17 00:00:00 2001 From: lemmyMwaura Date: Wed, 7 Aug 2024 23:26:24 +0300 Subject: [PATCH] fix: fix lint errors --- .../server/routes/v1/secret-sharing-router.ts | 2 +- .../secret-sharing/secret-sharing-service.ts | 81 ++++++++++--------- .../secret-sharing/secret-sharing-types.ts | 2 + .../components/PasswordContainer.tsx | 2 +- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/backend/src/server/routes/v1/secret-sharing-router.ts b/backend/src/server/routes/v1/secret-sharing-router.ts index e13107131..21bcf013c 100644 --- a/backend/src/server/routes/v1/secret-sharing-router.ts +++ b/backend/src/server/routes/v1/secret-sharing-router.ts @@ -115,7 +115,7 @@ export const registerSecretSharingRouter = async (server: FastifyZodProvider) => tag: true, expiresAt: true, expiresAfterViews: true, - accessType: true, + accessType: true }).extend({ orgName: z.string().optional() }) diff --git a/backend/src/services/secret-sharing/secret-sharing-service.ts b/backend/src/services/secret-sharing/secret-sharing-service.ts index 80d661b40..08dbdc91f 100644 --- a/backend/src/services/secret-sharing/secret-sharing-service.ts +++ b/backend/src/services/secret-sharing/secret-sharing-service.ts @@ -1,12 +1,19 @@ import bcrypt from "bcrypt"; import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service"; -import { BadRequestError, ForbiddenRequestError, InternalServerError, NotFoundError, UnauthorizedError } from "@app/lib/errors"; +import { + BadRequestError, + ForbiddenRequestError, + InternalServerError, + NotFoundError, + UnauthorizedError +} from "@app/lib/errors"; import { SecretSharingAccessType } from "@app/lib/types"; import { TOrgDALFactory } from "../org/org-dal"; import { TSecretSharingDALFactory } from "./secret-sharing-dal"; import { + SharedSecretWithDate, TCreatePublicSharedSecretDTO, TCreateSharedSecretDTO, TDeleteSharedSecretDTO, @@ -162,6 +169,40 @@ export const secretSharingServiceFactory = ({ }; }; + /** Checks if secret is expired and throws error if true */ + const checkIfExpired = async (sharedSecret: SharedSecretWithDate, sharedSecretId: string) => { + const { expiresAt, expiresAfterViews } = sharedSecret; + + if (expiresAt !== null && expiresAt < new Date()) { + // check lifetime expiry + await secretSharingDAL.softDeleteById(sharedSecretId); + throw new ForbiddenRequestError({ + message: "Access denied: Secret has expired by lifetime" + }); + } + + if (expiresAfterViews !== null && expiresAfterViews === 0) { + // check view count expiry + await secretSharingDAL.softDeleteById(sharedSecretId); + throw new ForbiddenRequestError({ + message: "Access denied: Secret has expired by view count" + }); + } + }; + + const decrementSecretViewCount = async (sharedSecret: SharedSecretWithDate, sharedSecretId: string) => { + const { expiresAfterViews } = sharedSecret; + + if (expiresAfterViews) { + // decrement view count if view count expiry set + await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } }); + } + + await secretSharingDAL.updateById(sharedSecretId, { + lastViewedAt: new Date() + }); + }; + const getActiveSharedSecretById = async ({ sharedSecretId, hashedHex, orgId }: TGetActiveSharedSecretByIdDTO) => { const sharedSecret = await secretSharingDAL.findOne({ id: sharedSecretId, @@ -180,7 +221,7 @@ export const secretSharingServiceFactory = ({ throw new UnauthorizedError(); await checkIfExpired(sharedSecret, sharedSecretId); - + if (sharedSecret.password !== null) return undefined; // decrement when we are sure the user will view secret. @@ -223,7 +264,7 @@ export const secretSharingServiceFactory = ({ }); const isMatch = await bcrypt.compare(password, sharedSecret.password as string); - if (!isMatch) return undefined + if (!isMatch) return undefined; // we reduce the view count when we are sure the password matches. await decrementSecretViewCount(sharedSecret, sharedSecretId); @@ -245,40 +286,6 @@ export const secretSharingServiceFactory = ({ return deletedSharedSecret; }; - /** Checks if secret is expired and throws error if true */ - const checkIfExpired = async (sharedSecret: any, sharedSecretId: string) => { - const { expiresAt, expiresAfterViews } = sharedSecret; - - if (expiresAt !== null && expiresAt < new Date()) { - // check lifetime expiry - await secretSharingDAL.softDeleteById(sharedSecretId); - throw new ForbiddenRequestError({ - message: "Access denied: Secret has expired by lifetime" - }); - } - - if (expiresAfterViews !== null && expiresAfterViews === 0) { - // check view count expiry - await secretSharingDAL.softDeleteById(sharedSecretId); - throw new ForbiddenRequestError({ - message: "Access denied: Secret has expired by view count" - }); - } - } - - const decrementSecretViewCount = async (sharedSecret: any, sharedSecretId: string) => { - const { expiresAfterViews } = sharedSecret; - - if (expiresAfterViews) { - // decrement view count if view count expiry set - await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } }); - } - - await secretSharingDAL.updateById(sharedSecretId, { - lastViewedAt: new Date() - }); - } - return { createSharedSecret, createPublicSharedSecret, diff --git a/backend/src/services/secret-sharing/secret-sharing-types.ts b/backend/src/services/secret-sharing/secret-sharing-types.ts index e96a68e64..6720a7bee 100644 --- a/backend/src/services/secret-sharing/secret-sharing-types.ts +++ b/backend/src/services/secret-sharing/secret-sharing-types.ts @@ -41,6 +41,8 @@ export type TValidateActiveSharedSecretDTO = TGetActiveSharedSecretByIdDTO & { export type TCreateSharedSecretDTO = TSharedSecretPermission & TCreatePublicSharedSecretDTO; +export type SharedSecretWithDate = Omit & { expiresAt: Date }; + export type TDeleteSharedSecretDTO = { sharedSecretId: string; } & TSharedSecretPermission; diff --git a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx index e79a3b5b1..00876225d 100644 --- a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx +++ b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx @@ -42,11 +42,11 @@ export const PasswordContainer = ({ secretId, hashedHex, handleSecret }: Props) if (secret) { handleSecret(secret); } else { + reset({ password: "" }); createNotification({ text: "Password is Invalid. Try again", type: "error" }) - reset(); } } catch (error) { console.error("Failed to validate password:", error);