2017-03-04 20:50:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-22 18:24:24 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
2017-03-04 20:50:44 +00:00
|
|
|
module.exports = {
|
2018-01-28 22:59:05 +00:00
|
|
|
enforce,
|
2018-02-25 19:54:15 +00:00
|
|
|
cleanupFromPost,
|
2018-09-29 11:30:29 +00:00
|
|
|
filterObject,
|
2019-07-22 18:24:24 +00:00
|
|
|
castToInteger,
|
|
|
|
normalizeEmail,
|
|
|
|
hashEmail
|
2017-03-04 20:50:44 +00:00
|
|
|
};
|
|
|
|
|
2018-02-25 19:54:15 +00:00
|
|
|
function enforce(condition, message) {
|
|
|
|
if (!condition) {
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
2017-03-04 20:50:44 +00:00
|
|
|
}
|
2017-03-19 12:36:57 +00:00
|
|
|
|
2018-02-25 19:54:15 +00:00
|
|
|
function cleanupFromPost(value) {
|
|
|
|
return (value || '').toString().trim();
|
2017-05-03 19:46:49 +00:00
|
|
|
}
|
2017-06-04 11:16:29 +00:00
|
|
|
|
|
|
|
function filterObject(obj, allowedKeys) {
|
|
|
|
const result = {};
|
|
|
|
for (const key in obj) {
|
|
|
|
if (allowedKeys.has(key)) {
|
|
|
|
result[key] = obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-09-29 11:30:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 05:18:06 +00:00
|
|
|
function castToInteger(id, msg) {
|
2018-09-29 11:30:29 +00:00
|
|
|
const val = parseInt(id);
|
|
|
|
|
|
|
|
if (!Number.isInteger(val)) {
|
2019-06-25 05:18:06 +00:00
|
|
|
throw new Error(msg || 'Invalid id');
|
2018-09-29 11:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
2019-07-22 18:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeEmail(email) {
|
|
|
|
const emailParts = email.split(/@/);
|
|
|
|
|
|
|
|
if (emailParts.length !== 2) {
|
|
|
|
return email;
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = emailParts[0];
|
|
|
|
const domain = emailParts[1].toLowerCase();
|
|
|
|
|
|
|
|
return username + '@' + domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hashEmail(email) {
|
|
|
|
return crypto.createHash('sha512').update(normalizeEmail(email)).digest("base64");
|
|
|
|
}
|
|
|
|
|