trilium/services/protected_session.js

38 lines
934 B
JavaScript
Raw Normal View History

"use strict";
const utils = require('./utils');
const session = {};
function setDataKey(req, decryptedDataKey) {
session.decryptedDataKey = Array.from(decryptedDataKey); // can't store buffer in session
session.protectedSessionId = utils.randomSecureToken(32);
return session.protectedSessionId;
}
function getProtectedSessionId(req) {
return req.headers.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 && session.protectedSessionId === protectedSessionId) {
return session.decryptedDataKey;
}
else {
return null;
}
}
function isProtectedSessionAvailable(req) {
const protectedSessionId = getProtectedSessionId(req);
return protectedSessionId && session.protectedSessionId === protectedSessionId;
}
module.exports = {
setDataKey,
getDataKey,
isProtectedSessionAvailable
};