From 9a3bb81a91059d56d3847e99b0de72164e369ed5 Mon Sep 17 00:00:00 2001 From: Nihanth Subramanya Date: Sat, 17 Mar 2018 17:14:05 +0530 Subject: [PATCH] Update Subscriber model to use EmailHash association --- db/models/subscriber.js | 9 ++++----- routes/home.js | 2 +- routes/user.js | 6 +++--- tests/fixtures/make-breach-with-emails.js | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/db/models/subscriber.js b/db/models/subscriber.js index 4b0f9fd29..8cb792851 100644 --- a/db/models/subscriber.js +++ b/db/models/subscriber.js @@ -15,17 +15,16 @@ module.exports = (sequelize, DataTypes) => { return crypto.randomBytes(40).toString("hex"); }, }, - sha1: DataTypes.STRING, }, {}); Subscriber.associate = function(models) { Subscriber.hasOne(models.EmailHash); }; - Subscriber.prototype.saveSha1 = async function() { - this.sha1 = crypto.createHash("sha1").update(this.email).digest("hex"); - await this.save(); - return this.sha1; + Subscriber.prototype.saveSha1 = async function(models) { + const sha1 = crypto.createHash("sha1").update(this.email).digest("hex"); + const emailHash = await models.EmailHash.findOrCreate( { where: { sha1 }}); + await this.setEmailHash(emailHash); }; return Subscriber; diff --git a/routes/home.js b/routes/home.js index 707276442..0d71baceb 100644 --- a/routes/home.js +++ b/routes/home.js @@ -25,7 +25,7 @@ router.post("/scan", async (req, res) => { }); function getSha1(email) { - return crypto.createHash("sha1").update(email.toLowerCase()).digest("hex"); + return crypto.createHash("sha1").update(email).digest("hex"); } module.exports = router; diff --git a/routes/user.js b/routes/user.js index f02ad7f20..99c0cf0c7 100644 --- a/routes/user.js +++ b/routes/user.js @@ -16,7 +16,7 @@ const ResponseCodes = Object.freeze({ }); router.post("/add", async (req, res) => { - const user = await models.User.create({ email: req.body.email }); + const user = await models.Subscriber.create({ email: req.body.email }); const url = `${AppConstants.SERVER_URL}/user/verify?state=${encodeURIComponent(user.verificationToken)}&email=${encodeURIComponent(user.email)}`; try { @@ -39,7 +39,7 @@ router.post("/add", async (req, res) => { }); router.get("/verify", async (req, res) => { - const user = await models.User.findOne({ where: { email: req.query.email, verificationToken: req.query.state } }); + const user = await models.Subscriber.findOne({ where: { email: req.query.email, verificationToken: req.query.state } }); if (user === null) { res.status(400).json({ error_code: ResponseCodes.EmailNotFound, @@ -56,7 +56,7 @@ router.get("/verify", async (req, res) => { }); router.post("/remove", async (req, res) => { - models.User.destroy({ where: { email: req.query.email } }); + models.Subscriber.destroy({ where: { email: req.query.email } }); res.status(200).json({ info: "Deleted user.", }); diff --git a/tests/fixtures/make-breach-with-emails.js b/tests/fixtures/make-breach-with-emails.js index ca796a233..81a3680ed 100644 --- a/tests/fixtures/make-breach-with-emails.js +++ b/tests/fixtures/make-breach-with-emails.js @@ -38,5 +38,5 @@ models.sequelize.sync().then(async () => { }); function getSha1(email) { - return crypto.createHash("sha1").update(email.toLowerCase()).digest("hex"); + return crypto.createHash("sha1").update(email).digest("hex"); }