trilium/services/protected_session.js

37 lines
943 B
JavaScript
Raw Normal View History

"use strict";
const utils = require('./utils');
function setDataKey(req, decryptedDataKey) {
req.session.decryptedDataKey = Array.from(decryptedDataKey); // can't store buffer in session
req.session.protectedSessionId = utils.randomSecureToken(32);
return req.session.protectedSessionId;
}
function getProtectedSessionId(req) {
return req.headers['x-protected-session-id'];
}
2017-11-11 11:55:19 +08:00
function getDataKey(req) {
const protectedSessionId = getProtectedSessionId(req);
2017-11-11 11:55:19 +08:00
if (protectedSessionId && req.session.protectedSessionId === protectedSessionId) {
return req.session.decryptedDataKey;
}
else {
return null;
}
}
function isProtectedSessionAvailable(req) {
const protectedSessionId = getProtectedSessionId(req);
return protectedSessionId && req.session.protectedSessionId === protectedSessionId;
}
module.exports = {
setDataKey,
getDataKey,
isProtectedSessionAvailable
};