refactor: fix lint issues and refactor code

This commit is contained in:
lemmyMwaura
2024-08-08 09:56:18 +03:00
parent 069651bdb4
commit 8e0b4254b1
3 changed files with 16 additions and 14 deletions
@@ -74,13 +74,13 @@ export const registerSecretSharingRouter = async (server: FastifyZodProvider) =>
} }
}, },
handler: async (req) => { handler: async (req) => {
const sharedSecret = await req.server.services.secretSharing.getActiveSharedSecretById({ const sharedSecret = await req.server.services.secretSharing.getPasswordlessSecretByID({
sharedSecretId: req.params.id, sharedSecretId: req.params.id,
hashedHex: req.query.hashedHex, hashedHex: req.query.hashedHex,
orgId: req.permission?.orgId orgId: req.permission?.orgId
}); });
if (!sharedSecret || sharedSecret.password) return undefined; if (!sharedSecret) return undefined;
return { return {
encryptedValue: sharedSecret.encryptedValue, encryptedValue: sharedSecret.encryptedValue,
@@ -125,7 +125,7 @@ export const registerSecretSharingRouter = async (server: FastifyZodProvider) =>
const { id } = req.params; const { id } = req.params;
const { password, hashedHex } = req.body; const { password, hashedHex } = req.body;
const sharedSecret = await req.server.services.secretSharing.validateSecretPassword({ const sharedSecret = await req.server.services.secretSharing.getValidatedSecretByID({
sharedSecretId: id, sharedSecretId: id,
hashedHex, hashedHex,
orgId: req.permission?.orgId, orgId: req.permission?.orgId,
@@ -9,11 +9,11 @@ import {
UnauthorizedError UnauthorizedError
} from "@app/lib/errors"; } from "@app/lib/errors";
import { SecretSharingAccessType } from "@app/lib/types"; import { SecretSharingAccessType } from "@app/lib/types";
import { TSecretSharing } from "@app/db/schemas";
import { TOrgDALFactory } from "../org/org-dal"; import { TOrgDALFactory } from "../org/org-dal";
import { TSecretSharingDALFactory } from "./secret-sharing-dal"; import { TSecretSharingDALFactory } from "./secret-sharing-dal";
import { import {
SharedSecretWithDate,
TCreatePublicSharedSecretDTO, TCreatePublicSharedSecretDTO,
TCreateSharedSecretDTO, TCreateSharedSecretDTO,
TDeleteSharedSecretDTO, TDeleteSharedSecretDTO,
@@ -170,7 +170,7 @@ export const secretSharingServiceFactory = ({
}; };
/** Checks if secret is expired and throws error if true */ /** Checks if secret is expired and throws error if true */
const checkIfExpired = async (sharedSecret: SharedSecretWithDate, sharedSecretId: string) => { const checkIfSecretIsExpired = async (sharedSecret: TSecretSharing, sharedSecretId: string) => {
const { expiresAt, expiresAfterViews } = sharedSecret; const { expiresAt, expiresAfterViews } = sharedSecret;
if (expiresAt !== null && expiresAt < new Date()) { if (expiresAt !== null && expiresAt < new Date()) {
@@ -190,7 +190,7 @@ export const secretSharingServiceFactory = ({
} }
}; };
const decrementSecretViewCount = async (sharedSecret: SharedSecretWithDate, sharedSecretId: string) => { const decrementSecretViewCount = async (sharedSecret: TSecretSharing, sharedSecretId: string) => {
const { expiresAfterViews } = sharedSecret; const { expiresAfterViews } = sharedSecret;
if (expiresAfterViews) { if (expiresAfterViews) {
@@ -203,7 +203,8 @@ export const secretSharingServiceFactory = ({
}); });
}; };
const getActiveSharedSecretById = async ({ sharedSecretId, hashedHex, orgId }: TGetActiveSharedSecretByIdDTO) => { /** Get's passwordless secret. validates all secret's requested (must be fresh). */
const getPasswordlessSecretByID = async ({ sharedSecretId, hashedHex, orgId }: TGetActiveSharedSecretByIdDTO) => {
const sharedSecret = await secretSharingDAL.findOne({ const sharedSecret = await secretSharingDAL.findOne({
id: sharedSecretId, id: sharedSecretId,
hashedHex hashedHex
@@ -220,7 +221,9 @@ export const secretSharingServiceFactory = ({
if (accessType === SecretSharingAccessType.Organization && orgId !== sharedSecret.orgId) if (accessType === SecretSharingAccessType.Organization && orgId !== sharedSecret.orgId)
throw new UnauthorizedError(); throw new UnauthorizedError();
await checkIfExpired(sharedSecret, sharedSecretId); // all secrets pass through here, meaning we check if its expired first and then check if it needs verification
// or can be safely sent to the client.
await checkIfSecretIsExpired(sharedSecret, sharedSecretId);
if (sharedSecret.password !== null) return undefined; if (sharedSecret.password !== null) return undefined;
@@ -236,7 +239,8 @@ export const secretSharingServiceFactory = ({
}; };
}; };
const validateSecretPassword = async ({ /** Get's the requested secret if password passed is valid */
const getValidatedSecretByID = async ({
sharedSecretId, sharedSecretId,
hashedHex, hashedHex,
orgId, orgId,
@@ -266,7 +270,7 @@ export const secretSharingServiceFactory = ({
const isMatch = await bcrypt.compare(password, sharedSecret.password as string); 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. // reduce the view count when the password matches (will be returned to the client).
await decrementSecretViewCount(sharedSecret, sharedSecretId); await decrementSecretViewCount(sharedSecret, sharedSecretId);
return { return {
@@ -291,7 +295,7 @@ export const secretSharingServiceFactory = ({
createPublicSharedSecret, createPublicSharedSecret,
getSharedSecrets, getSharedSecrets,
deleteSharedSecretById, deleteSharedSecretById,
getActiveSharedSecretById, getPasswordlessSecretByID,
validateSecretPassword getValidatedSecretByID
}; };
}; };
@@ -41,8 +41,6 @@ export type TValidateActiveSharedSecretDTO = TGetActiveSharedSecretByIdDTO & {
export type TCreateSharedSecretDTO = TSharedSecretPermission & TCreatePublicSharedSecretDTO; export type TCreateSharedSecretDTO = TSharedSecretPermission & TCreatePublicSharedSecretDTO;
export type SharedSecretWithDate = Omit<TCreateSharedSecretDTO, 'expiresAt'> & { expiresAt: Date };
export type TDeleteSharedSecretDTO = { export type TDeleteSharedSecretDTO = {
sharedSecretId: string; sharedSecretId: string;
} & TSharedSecretPermission; } & TSharedSecretPermission;