From e9e874c62d0c99bf9664dc0ecdc998684cd0c31a Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Sat, 9 Jan 2021 13:09:47 +0530 Subject: [PATCH 1/2] fix email --- .env.local.example | 3 ++- pages/poll/[id].tsx | 2 +- pages/poll/create.tsx | 2 +- src/components/PollInfo.tsx | 5 +++-- src/helpers/helpers.ts | 14 +++++--------- src/models/poll.ts | 8 ++++---- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/.env.local.example b/.env.local.example index 1be737d..846dcf7 100644 --- a/.env.local.example +++ b/.env.local.example @@ -1 +1,2 @@ -NEXT_PUBLIC_ENCRYPTION_KEY=your_32_bit_key_here \ No newline at end of file +NEXT_PUBLIC_ENCRYPTION_KEY=your key of length 32 characters here +NEXT_PUBLIC_ENCRYPTION_IV=your key of length 16 characters here \ No newline at end of file diff --git a/pages/poll/[id].tsx b/pages/poll/[id].tsx index 7713a19..529cf37 100644 --- a/pages/poll/[id].tsx +++ b/pages/poll/[id].tsx @@ -30,7 +30,7 @@ const Poll = (props: { pollid: string; }): JSX.Element => { const { pollFromDB, pollid } = props; - const pollCreatorEmailID = decrypt(pollFromDB.emailID); + const pollCreatorEmailID = decrypt(pollFromDB.encryptedEmailID); const loggedInUserEmailID = useSelector( (state: RootState) => state.authReducer.username ); diff --git a/pages/poll/create.tsx b/pages/poll/create.tsx index f90af5c..041dbe4 100644 --- a/pages/poll/create.tsx +++ b/pages/poll/create.tsx @@ -52,7 +52,7 @@ const Create = (): JSX.Element => { const poll: RocketMeetPoll = { title: pollTitle, description: pollDescription, - emailID: encryptedEmailID, + encryptedEmailID, choices: pollChoices, }; const payload = JSON.stringify(poll); diff --git a/src/components/PollInfo.tsx b/src/components/PollInfo.tsx index 9f45172..f08a889 100644 --- a/src/components/PollInfo.tsx +++ b/src/components/PollInfo.tsx @@ -8,7 +8,8 @@ dayjs.extend(localizedFormat); const PollInfo = (props: { poll: RocketMeetPollFromDB }): JSX.Element => { const { poll } = props; - + console.log("lol"); + console.log(poll); return (

{poll.title}

@@ -16,7 +17,7 @@ const PollInfo = (props: { poll: RocketMeetPollFromDB }): JSX.Element => { {poll.open ? "OPEN" : "CLOSED"} - By {decrypt(poll.emailID)} | Created on{" "} + By {decrypt(poll.encryptedEmailID)} | Created on{" "} {dayjs(poll.createdAt).format("DD/MM/YYYY")}
); diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index 035e23b..83e8ff5 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -12,29 +12,25 @@ export const isChoicePresentInPollChoices = ( }; const ENCRYPTION_KEY = process.env.NEXT_PUBLIC_ENCRYPTION_KEY || ""; +const ENCRYPTION_IV = process.env.NEXT_PUBLIC_ENCRYPTION_IV || ""; export const encrypt = (text: string): string => { - const iv = crypto.randomBytes(16); let cipher = crypto.createCipheriv( "aes-256-cbc", Buffer.from(ENCRYPTION_KEY), - iv + ENCRYPTION_IV ); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); - return JSON.stringify({ - iv: iv.toString("hex"), - encryptedText: encrypted.toString("hex"), - }); + return encrypted.toString("hex"); }; export const decrypt = (text: string): string => { - const iv = Buffer.from(JSON.parse(text).iv, "hex"); - const encryptedText = Buffer.from(JSON.parse(text).encryptedText, "hex"); + const encryptedText = Buffer.from(text, "hex"); const decipher = crypto.createDecipheriv( "aes-256-cbc", Buffer.from(ENCRYPTION_KEY), - iv + ENCRYPTION_IV ); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); diff --git a/src/models/poll.ts b/src/models/poll.ts index bffbce2..7752600 100644 --- a/src/models/poll.ts +++ b/src/models/poll.ts @@ -24,7 +24,7 @@ export interface RocketMeetPoll { title: string; description?: string; open?: boolean; - emailID: string; // encrypted email ID + encryptedEmailID: string; choices: Choice[]; finalChoice?: Choice; votes?: Vote[]; @@ -35,7 +35,7 @@ export interface RocketMeetPollFromDB { title: string; description?: string; open?: boolean; - emailID: string; // encrypted email ID + encryptedEmailID: string; choices: ChoiceFromDB[]; finalChoice?: ChoiceFromDB; votes?: VoteFromDB[]; @@ -46,6 +46,6 @@ export interface RocketMeetPollFromDB { export interface MailerArgs { pollid: string; - receiverIDs: string[], - senderID: string + receiverIDs: string[]; + senderID: string; } From b0caaf987a0f21d62227c0c7ea2f368ecb2d975c Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Sat, 9 Jan 2021 13:14:16 +0530 Subject: [PATCH 2/2] removes console log statements --- src/components/PollInfo.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/PollInfo.tsx b/src/components/PollInfo.tsx index f08a889..c528f4d 100644 --- a/src/components/PollInfo.tsx +++ b/src/components/PollInfo.tsx @@ -8,8 +8,6 @@ dayjs.extend(localizedFormat); const PollInfo = (props: { poll: RocketMeetPollFromDB }): JSX.Element => { const { poll } = props; - console.log("lol"); - console.log(poll); return (

{poll.title}