Skip to content

Commit

Permalink
feat(browser-extension): store encrypted email
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Jul 24, 2022
1 parent 492edce commit 87bfc77
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
10 changes: 7 additions & 3 deletions browser-extension/api-server/api.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
GET http://localhost:3000/auth/register

###

GET http://localhost:3000/auth/login?username=denny&password=denny_pw

###
Expand All @@ -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": "[email protected],[email protected],[email protected],swastika@tattle.co.in",
"email": "dog@tattle.co.in",
"slurList": "apple,dog,cat"
}

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions browser-extension/api-server/encryption.js
Original file line number Diff line number Diff line change
@@ -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,
};
24 changes: 21 additions & 3 deletions browser-extension/api-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) => {
Expand All @@ -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),
});
}
});

Expand Down Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions browser-extension/plugin/src/ui-components/pages/Debug.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ export function Debug() {
</Box>

{/* {localStorageData ? (
<Box>
<Text weight={500}>Local Storage</Text>
<Text>{JSON.stringify(localStorageData, null, 2)}</Text>
</Box>
) : (
<Text color={"status-critical"}>
{t("message_error_local_storage")}
</Text>
)} */}
<Box>
<Text weight={500}>Local Storage</Text>
<Text>
{JSON.stringify(localStorageData, null, 2)}
</Text>
</Box>
) : (
<Text color={'status-critical'}>
{t('message_error_local_storage')}
</Text>
)} */}

<Box
pad={'small'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function Preferences() {
useEffect(async () => {
try {
const preference = await getPreferenceData();
// console.log({ preference });
setLocalPreferences(preference);
if (
preference != undefined &&
Expand Down Expand Up @@ -184,7 +185,7 @@ export function Preferences() {
component={TextInput}
/>

<FormField
{/* <FormField
name="friends"
htmlFor="friendsId"
label={
Expand All @@ -202,7 +203,7 @@ export function Preferences() {
}
disabled={!enable}
component={TextArea}
/>
/> */}

<FormField
name="slurList"
Expand Down

0 comments on commit 87bfc77

Please sign in to comment.