Update Subscriber model to use EmailHash association

This commit is contained in:
Nihanth Subramanya 2018-03-17 17:14:05 +05:30
Родитель 79168f25d4
Коммит 9a3bb81a91
4 изменённых файлов: 9 добавлений и 10 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -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;

Просмотреть файл

@ -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.",
});

2
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");
}