Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: refactor password show decrypted password for link

Varun879da643ad03
diff --git a/lib/team/helper.ts b/lib/team/helper.ts
index 63698732..bdc8fad9 100644
--- a/lib/team/helper.ts
+++ b/lib/team/helper.ts
@@ -1,7 +1,7 @@
import prisma from "@/lib/prisma";
import { DocumentError, TeamError } from "../errorHandler";
import { Document, DocumentVersion, Domain, Link, View } from "@prisma/client";
-
+import { decryptEncrpytedPassword} from "@/lib/utils";
interface ITeamUserAndDocument {
teamId: string;
userId: string;
@@ -71,7 +71,15 @@ export async function getTeamWithUsersAndDocument({
throw new TeamError("Document doesn't exists in the team");
}
}
-
+ if(document && document?.links){
+ document?.links?.forEach((res:Link)=>{
+ if(res?.password!=null)
+ {
+ let decryptedPassword:string=decryptEncrpytedPassword(res?.password)
+ res["password"]=decryptedPassword
+ }
+ })
+}
// Check that the user is owner of the document, otherwise return 401
// if (checkOwner) {
// const isUserOwnerOfDocument = document?.ownerId === userId;
diff --git a/lib/utils.ts b/lib/utils.ts
index c7c8ed74..0aee1ffb 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -424,7 +424,30 @@ export const generateGravatarHash = (email: string | null): string => {
return hash;
};
+export async function generateEncrpytedPassword(password: string): Promise<string> {
+ if(!password) return "";
+ const encryptedKey:string = crypto.createHash('sha256').update(String(process.env.Docs_Password_Key)).digest('base64').substring(0,32);
+ const IV:Buffer = crypto.randomBytes(16);
+ const cipher = crypto.createCipheriv("aes-256-ctr",encryptedKey, IV);
+ let encryptedText:string = cipher.update(password, "utf8", "hex");
+ encryptedText += cipher.final("hex");
+ return IV.toString('hex')+":"+ encryptedText;
+}
+export function decryptEncrpytedPassword(password:string): string{
+ if(!password) return "";
+ const encryptedKey:string = crypto.createHash('sha256').update(String(process.env.Docs_Password_Key)).digest('base64').substring(0,32);
+ const textParts:string[] = password.split(":");
+ if (!textParts || textParts.length !== 2) {
+ return "Please Update Your Password"
+ }
+ const IV:Buffer = Buffer.from(textParts[0], 'hex');
+ const encryptedText:string = textParts[1];
+ const decipher = crypto.createDecipheriv("aes-256-ctr",encryptedKey, IV);
+ let decrypted:string = decipher.update(encryptedText, "hex", "utf8");
+ decrypted += decipher.final("utf8");
+ return decrypted;
+}
export const sanitizeAllowDenyList = (list: string): string[] => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const domainRegex = /^@[^\s@]+\.[^\s@]+$/;
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 23759896..a6beda79 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
-import { hashPassword } from "@/lib/utils";
+import { generateEncrpytedPassword } from "@/lib/utils";
import { CustomUser } from "@/lib/types";
import { authOptions } from "../../auth/[...nextauth]";
@@ -114,7 +114,7 @@ export default async function handle(
const documentLink = linkType === "DOCUMENT_LINK";
const hashedPassword =
- password && password.length > 0 ? await hashPassword(password) : null;
+ password && password.length > 0 ? await generateEncrpytedPassword(password) : null;
const exat = expiresAt ? new Date(expiresAt) : null;
let { domain, slug, ...linkData } = linkDomainData;
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 165d399b..bd6dbdfd 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -2,7 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
-import { hashPassword } from "@/lib/utils";
+import { generateEncrpytedPassword} from "@/lib/utils";
import { CustomUser } from "@/lib/types";
import { errorhandler } from "@/lib/errorHandler";
import {
@@ -70,7 +70,7 @@ export default async function handler(
}
const hashedPassword =
- password && password.length > 0 ? await hashPassword(password) : null;
+ password && password.length > 0 ? await generateEncrpytedPassword(password) : null;
const exat = expiresAt ? new Date(expiresAt) : null;
let { domain, slug, ...linkData } = linkDomainData;