trilium/public/javascripts/encryption.js

157 lines
4.3 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 06:18:55 +08:00
const encryption = (function() {
2017-11-15 11:34:33 +08:00
const dialogEl = $("#protected-session-password-dialog");
const passwordFormEl = $("#protected-session-password-form");
const passwordEl = $("#protected-session-password");
2017-11-05 06:18:55 +08:00
2017-11-15 11:34:33 +08:00
let protectedSessionDeferred = null;
let lastProtectedSessionOperationDate = null;
2017-11-05 06:18:55 +08:00
let encryptionSessionTimeout = null;
2017-11-11 11:55:19 +08:00
let protectedSessionId = null;
2017-11-05 06:18:55 +08:00
$.ajax({
url: baseApiUrl + 'settings/all',
type: 'GET',
2017-11-09 11:33:08 +08:00
error: () => showError("Error getting encryption settings.")
}).then(settings => {
encryptionSessionTimeout = settings.encryption_session_timeout;
});
2017-11-05 06:18:55 +08:00
function setEncryptionSessionTimeout(encSessTimeout) {
encryptionSessionTimeout = encSessTimeout;
}
2017-11-15 11:34:33 +08:00
function ensureProtectedSession(requireProtectedSession, modal) {
2017-11-05 06:18:55 +08:00
const dfd = $.Deferred();
2017-11-15 11:34:33 +08:00
if (requireProtectedSession && !isProtectedSessionAvailable()) {
2017-11-09 11:33:08 +08:00
// if this is entry point then we need to show the app even before the note is loaded
showAppIfHidden();
2017-11-15 11:34:33 +08:00
protectedSessionDeferred = dfd;
2017-11-05 06:18:55 +08:00
dialogEl.dialog({
modal: modal,
width: 400,
open: () => {
if (!modal) {
// dialog steals focus for itself, which is not what we want for non-modal (viewing)
2017-11-05 10:18:36 +08:00
treeUtils.getNodeByKey(noteEditor.getCurrentNoteId()).setFocus();
2017-11-05 06:18:55 +08:00
}
}
});
}
else {
dfd.resolve();
}
2017-11-05 06:18:55 +08:00
return dfd.promise();
}
async function setupProtectedSession() {
2017-11-15 11:34:33 +08:00
const password = passwordEl.val();
passwordEl.val("");
2017-11-11 11:55:19 +08:00
const response = await enterProtectedSession(password);
if (!response.success) {
showError("Wrong password.");
2017-11-09 11:33:08 +08:00
return;
}
2017-09-17 12:18:03 +08:00
2017-11-11 11:55:19 +08:00
protectedSessionId = response.protectedSessionId;
initAjax();
2017-11-11 11:55:19 +08:00
dialogEl.dialog("close");
2017-11-13 10:40:26 +08:00
noteEditor.reload();
2017-11-11 11:55:19 +08:00
noteTree.reload();
2017-11-15 11:34:33 +08:00
if (protectedSessionDeferred !== null) {
protectedSessionDeferred.resolve();
2017-11-15 11:34:33 +08:00
protectedSessionDeferred = null;
2017-11-09 11:33:08 +08:00
}
}
2017-11-11 11:55:19 +08:00
async function enterProtectedSession(password) {
return await $.ajax({
url: baseApiUrl + 'login/protected',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
password: password
}),
error: () => showError("Error entering protected session.")
});
}
function getProtectedSessionId() {
return protectedSessionId;
}
2017-11-15 11:34:33 +08:00
function resetProtectedSession() {
2017-11-11 11:55:19 +08:00
protectedSessionId = null;
initAjax();
// most secure solution - guarantees nothing remained in memory
// since this expires because user doesn't use the app, it shouldn't be disruptive
window.location.reload(true);
}
2017-11-15 11:34:33 +08:00
function isProtectedSessionAvailable() {
2017-11-13 10:40:26 +08:00
return protectedSessionId !== null;
2017-11-05 06:18:55 +08:00
}
async function protectNoteAndSendToServer() {
await ensureProtectedSession(true, true);
2017-11-05 06:18:55 +08:00
const note = noteEditor.getCurrentNote();
2017-11-05 06:18:55 +08:00
noteEditor.updateNoteFromInputs(note);
note.detail.is_protected = true;
2017-11-05 06:18:55 +08:00
await noteEditor.saveNoteToServer(note);
noteEditor.setNoteBackgroundIfProtected(note);
2017-11-05 06:18:55 +08:00
}
async function unprotectNoteAndSendToServer() {
await ensureProtectedSession(true, true);
2017-11-05 06:18:55 +08:00
const note = noteEditor.getCurrentNote();
2017-11-05 06:18:55 +08:00
noteEditor.updateNoteFromInputs(note);
note.detail.is_protected = false;
2017-11-05 06:18:55 +08:00
await noteEditor.saveNoteToServer(note);
noteEditor.setNoteBackgroundIfProtected(note);
2017-11-05 06:18:55 +08:00
}
2017-11-15 11:34:33 +08:00
passwordFormEl.submit(() => {
setupProtectedSession();
2017-11-09 11:33:08 +08:00
return false;
});
setInterval(() => {
2017-11-15 11:34:33 +08:00
if (lastProtectedSessionOperationDate !== null && new Date().getTime() - lastProtectedSessionOperationDate.getTime() > encryptionSessionTimeout * 1000) {
resetProtectedSession();
2017-11-09 11:33:08 +08:00
}
}, 5000);
2017-11-05 06:18:55 +08:00
return {
setEncryptionSessionTimeout,
ensureProtectedSession,
2017-11-15 11:34:33 +08:00
resetProtectedSession,
isProtectedSessionAvailable,
protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
2017-11-11 11:55:19 +08:00
getProtectedSessionId
2017-11-05 06:18:55 +08:00
};
})();