From 87bfc7748edb824eb9cd5d5857c4c34f6057ee69 Mon Sep 17 00:00:00 2001 From: dennyabrain Date: Sun, 24 Jul 2022 21:06:32 +0530 Subject: [PATCH] feat(browser-extension): store encrypted email --- browser-extension/api-server/api.http | 10 +++++--- browser-extension/api-server/encryption.js | 21 ++++++++++++++++ browser-extension/api-server/index.js | 24 ++++++++++++++++--- .../plugin/src/ui-components/pages/Debug.jsx | 20 +++++++++------- .../src/ui-components/pages/Preferences.jsx | 5 ++-- 5 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 browser-extension/api-server/encryption.js diff --git a/browser-extension/api-server/api.http b/browser-extension/api-server/api.http index 4da50695..f5583379 100644 --- a/browser-extension/api-server/api.http +++ b/browser-extension/api-server/api.http @@ -1,3 +1,7 @@ +GET http://localhost:3000/auth/register + +### + GET http://localhost:3000/auth/login?username=denny&password=denny_pw ### @@ -9,12 +13,12 @@ GET http://localhost:3000/user/e3be8f99-7ec7-11ec-a714-0242ac140002 # IF id is sent, it updates the post else creates a new one POST http://localhost:3000/preference content-type : application/json -Authorization: token e3be8fa9-7ec7-11ec-a714-0242ac140002 +Authorization: token f88f7ab3-c428-40a9-90fb-3cee9e820f90 { "id" : "8066828c-4928-4bc2-8b98-c79f0b5e2bb3", "language":"ta", - "friends": "denny@tattle.co.in,tarunima@tattle.co.in,rahul@tattle.co.in,swastika@tattle.co.in", + "email": "dog@tattle.co.in", "slurList": "apple,dog,cat" } @@ -33,7 +37,7 @@ Authorization: token e3be8fa9-7ec7-11ec-a714-0242ac140002 ### GET http://localhost:3000/preference -Authorization: token e3be8fa9-7ec7-11ec-a714-0242ac140002 +Authorization: token f88f7ab3-c428-40a9-90fb-3cee9e820f90 ### GET http://localhost:3000/auth/register diff --git a/browser-extension/api-server/encryption.js b/browser-extension/api-server/encryption.js new file mode 100644 index 00000000..72200ce7 --- /dev/null +++ b/browser-extension/api-server/encryption.js @@ -0,0 +1,21 @@ +const Cryptr = require("cryptr"); + +let crypter; +try { + crypter = new Cryptr(process.env.DB_FIELD_ENCRYPTION_KEY); +} catch { + console.log("could not initialize crytper"); +} + +function encrypt(value) { + return crypter.encrypt(value); +} + +function decrypt(encryptedValue) { + return crypter.decrypt(encryptedValue); +} + +module.exports = { + encrypt, + decrypt, +}; diff --git a/browser-extension/api-server/index.js b/browser-extension/api-server/index.js index 0c86c304..ff75b4aa 100644 --- a/browser-extension/api-server/index.js +++ b/browser-extension/api-server/index.js @@ -14,6 +14,7 @@ const { sendAskFriendsForHelpEmail, } = require("./controller-email"); const { authentication } = require("./middlewares"); +const { encrypt, decrypt } = require("./encryption"); app.use(cors()); app.use(express.json()); @@ -39,11 +40,19 @@ app.post("/preference/", async (req, res) => { const result = await preference.upsert({ id, userId: user.id, - email, + email: encrypt(email), language, slurList, }); - res.send({ ...result[0].get({ plain: true }) }); + + let plainResult = result[0].get({ plain: true }); + console.log({ plainResult }); + res.send({ + ...plainResult, + email, + }); + + // res.send({ ...result[0].get({ plain: true }) }); }); app.get("/preference/", async (req, res) => { @@ -56,7 +65,12 @@ app.get("/preference/", async (req, res) => { if (result == null) { res.status(404).send(); } else { - res.send({ preference: result.get({ plain: true }) }); + let plainResult = result.get({ plain: true }); + // console.log({ plainResult }); + res.send({ + ...plainResult, + email: decrypt(plainResult.email), + }); } }); @@ -92,6 +106,10 @@ app.post("/archive", upload.single("screenshot"), async (req, res) => { }, }); resultPlain = result.get({ plain: true }); + resultPlain = { ...resultPlain, email: decrypt(resultPlain.email) }; + + console.log({ resultPlain }); + if ( (result != null && resultPlain.email != undefined) || resultPlain.email != null diff --git a/browser-extension/plugin/src/ui-components/pages/Debug.jsx b/browser-extension/plugin/src/ui-components/pages/Debug.jsx index e7233d00..b2de6eda 100644 --- a/browser-extension/plugin/src/ui-components/pages/Debug.jsx +++ b/browser-extension/plugin/src/ui-components/pages/Debug.jsx @@ -53,15 +53,17 @@ export function Debug() { {/* {localStorageData ? ( - - Local Storage - {JSON.stringify(localStorageData, null, 2)} - - ) : ( - - {t("message_error_local_storage")} - - )} */} + + Local Storage + + {JSON.stringify(localStorageData, null, 2)} + + + ) : ( + + {t('message_error_local_storage')} + + )} */} { try { const preference = await getPreferenceData(); + // console.log({ preference }); setLocalPreferences(preference); if ( preference != undefined && @@ -184,7 +185,7 @@ export function Preferences() { component={TextInput} /> - + /> */}