2017-11-14 21:54:12 -05:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-09 23:25:23 -05:00
|
|
|
const utils = require('./utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const dataEncryptionService = require('./data_encryption');
|
2018-03-31 08:53:52 -04:00
|
|
|
const cls = require('./cls');
|
2017-11-09 23:25:23 -05:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
const dataKeyMap = {};
|
|
|
|
|
2018-04-20 00:12:01 -04:00
|
|
|
function setDataKey(decryptedDataKey) {
|
2018-03-31 08:53:52 -04:00
|
|
|
const protectedSessionId = utils.randomSecureToken(32);
|
2017-11-09 23:25:23 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
dataKeyMap[protectedSessionId] = Array.from(decryptedDataKey); // can't store buffer in session
|
|
|
|
|
|
|
|
return protectedSessionId;
|
2017-11-09 23:25:23 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function setProtectedSessionId(req) {
|
2019-03-31 12:49:42 +02:00
|
|
|
cls.namespace.set('protectedSessionId', req.cookies.protectedSessionId);
|
2017-11-14 21:54:12 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function getProtectedSessionId() {
|
|
|
|
return cls.namespace.get('protectedSessionId');
|
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function getDataKey() {
|
|
|
|
const protectedSessionId = getProtectedSessionId();
|
2017-11-10 22:55:19 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
return dataKeyMap[protectedSessionId];
|
2018-02-23 22:58:24 -05:00
|
|
|
}
|
|
|
|
|
2018-04-20 00:12:01 -04:00
|
|
|
function isProtectedSessionAvailable() {
|
|
|
|
const protectedSessionId = getProtectedSessionId();
|
2017-11-14 21:54:12 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
return !!dataKeyMap[protectedSessionId];
|
2017-11-14 21:54:12 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function decryptNotes(notes) {
|
2018-01-24 22:13:41 -05:00
|
|
|
for (const note of notes) {
|
2019-10-31 21:58:34 +01:00
|
|
|
if (note.isProtected) {
|
2019-11-26 19:49:52 +01:00
|
|
|
note.title = decryptString(note.title);
|
2019-03-30 19:24:53 +01:00
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:58:34 +01:00
|
|
|
function encrypt(plainText) {
|
|
|
|
return dataEncryptionService.encrypt(getDataKey(), plainText);
|
2019-02-06 20:19:25 +01:00
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
|
2019-10-31 21:58:34 +01:00
|
|
|
function decrypt(cipherText) {
|
2019-11-01 23:05:33 +01:00
|
|
|
return dataEncryptionService.decrypt(getDataKey(), cipherText);
|
2018-01-24 22:13:41 -05:00
|
|
|
}
|
|
|
|
|
2019-11-02 07:50:23 +01:00
|
|
|
function decryptString(cipherText) {
|
|
|
|
return dataEncryptionService.decryptString(getDataKey(), cipherText);
|
|
|
|
}
|
|
|
|
|
2017-11-09 23:25:23 -05:00
|
|
|
module.exports = {
|
|
|
|
setDataKey,
|
2017-11-14 21:54:12 -05:00
|
|
|
getDataKey,
|
2018-01-24 22:13:41 -05:00
|
|
|
isProtectedSessionAvailable,
|
2019-10-31 21:58:34 +01:00
|
|
|
encrypt,
|
|
|
|
decrypt,
|
2019-11-02 07:50:23 +01:00
|
|
|
decryptString,
|
2018-01-24 22:13:41 -05:00
|
|
|
decryptNotes,
|
2018-03-31 08:53:52 -04:00
|
|
|
setProtectedSessionId
|
2017-11-09 23:25:23 -05:00
|
|
|
};
|