2017-11-14 21:54:12 -05:00
|
|
|
"use strict";
|
|
|
|
|
2020-09-25 20:55:45 +02:00
|
|
|
const log = require('./log');
|
2023-06-29 22:10:13 +02:00
|
|
|
const dataEncryptionService = require('./encryption/data_encryption');
|
2017-11-09 23:25:23 -05:00
|
|
|
|
2021-05-07 22:23:49 +02:00
|
|
|
let dataKey = null;
|
2018-04-01 21:27:46 -04:00
|
|
|
|
2018-04-20 00:12:01 -04:00
|
|
|
function setDataKey(decryptedDataKey) {
|
2021-05-07 22:23:49 +02:00
|
|
|
dataKey = Array.from(decryptedDataKey);
|
2018-03-31 08:53:52 -04:00
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function getDataKey() {
|
2021-05-07 22:23:49 +02:00
|
|
|
return dataKey;
|
2018-02-23 22:58:24 -05:00
|
|
|
}
|
|
|
|
|
2021-04-03 22:02:25 +02:00
|
|
|
function resetDataKey() {
|
2021-05-07 22:23:49 +02:00
|
|
|
dataKey = null;
|
2021-04-03 22:02:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 00:12:01 -04:00
|
|
|
function isProtectedSessionAvailable() {
|
2021-05-07 22:23:49 +02:00
|
|
|
return !!dataKey;
|
2017-11-14 21:54:12 -05:00
|
|
|
}
|
|
|
|
|
2019-10-31 21:58:34 +01:00
|
|
|
function encrypt(plainText) {
|
2020-12-21 22:08:55 +01:00
|
|
|
if (plainText === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:58:34 +01:00
|
|
|
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) {
|
2020-12-21 22:08:55 +01:00
|
|
|
if (cipherText === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:20:56 +02:00
|
|
|
let lastProtectedSessionOperationDate = null;
|
|
|
|
|
|
|
|
function touchProtectedSession() {
|
|
|
|
if (isProtectedSessionAvailable()) {
|
|
|
|
lastProtectedSessionOperationDate = Date.now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 20:39:21 +02:00
|
|
|
function checkProtectedSessionExpiration() {
|
2023-01-23 23:37:58 +01:00
|
|
|
const options = require("./options");
|
2022-05-17 20:39:21 +02:00
|
|
|
const protectedSessionTimeout = options.getOptionInt('protectedSessionTimeout');
|
|
|
|
if (isProtectedSessionAvailable()
|
|
|
|
&& lastProtectedSessionOperationDate
|
|
|
|
&& Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {
|
2022-05-13 23:20:56 +02:00
|
|
|
|
2022-05-17 20:39:21 +02:00
|
|
|
resetDataKey();
|
2022-05-13 23:20:56 +02:00
|
|
|
|
2022-05-17 20:39:21 +02:00
|
|
|
log.info("Expiring protected session");
|
2022-05-13 23:20:56 +02:00
|
|
|
|
2023-09-28 23:47:19 +02:00
|
|
|
require('./ws').reloadFrontend("leaving protected session");
|
2022-05-17 20:39:21 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-13 23:20:56 +02:00
|
|
|
|
2017-11-09 23:25:23 -05:00
|
|
|
module.exports = {
|
|
|
|
setDataKey,
|
2021-04-03 22:02:25 +02:00
|
|
|
resetDataKey,
|
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,
|
2022-05-17 20:39:21 +02:00
|
|
|
touchProtectedSession,
|
|
|
|
checkProtectedSessionExpiration
|
2020-06-15 17:56:53 +02:00
|
|
|
};
|