trilium/public/javascripts/protected_session.js

176 lines
4.8 KiB
JavaScript
Raw Normal View History

"use strict";
const protected_session = (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;
let protectedSessionTimeout = null;
2017-11-11 11:55:19 +08:00
let protectedSessionId = null;
2017-11-05 06:18:55 +08:00
$(document).ready(() => {
server.get('settings/all').then(settings => protectedSessionTimeout = settings.protected_session_timeout);
});
function setProtectedSessionTimeout(encSessTimeout) {
protectedSessionTimeout = 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)
noteTree.getCurrentNode().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;
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) {
ensureDialogIsClosed(dialogEl, passwordEl);
2017-11-15 12:01:23 +08:00
2017-11-15 11:34:33 +08:00
protectedSessionDeferred.resolve();
2017-11-15 11:34:33 +08:00
protectedSessionDeferred = null;
2017-11-09 11:33:08 +08:00
}
}
function ensureDialogIsClosed() {
// this may fal if the dialog has not been previously opened
try {
dialogEl.dialog('close');
}
catch (e) {}
passwordEl.val('');
}
2017-11-11 11:55:19 +08:00
async function enterProtectedSession(password) {
return await server.post('login/protected', {
password: password
2017-11-11 11:55:19 +08:00
});
}
function getProtectedSessionId() {
return protectedSessionId;
}
2017-11-15 11:34:33 +08:00
function resetProtectedSession() {
2017-11-11 11:55:19 +08:00
protectedSessionId = null;
// most secure solution - guarantees nothing remained in memory
// since this expires because user doesn't use the app, it shouldn't be disruptive
reloadApp();
}
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
}
function touchProtectedSession() {
if (isProtectedSessionAvailable()) {
lastProtectedSessionOperationDate = new Date();
}
}
2017-11-15 13:04:26 +08:00
async function protectSubTree(noteId, protect) {
2017-11-23 09:46:42 +08:00
await ensureProtectedSession(true, true);
2017-12-02 11:47:23 +08:00
await server.put('tree/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0));
2017-11-15 13:04:26 +08:00
showMessage("Request to un/protect sub tree has finished successfully");
noteTree.reload();
noteEditor.reload();
}
2017-11-15 11:34:33 +08:00
passwordFormEl.submit(() => {
setupProtectedSession();
2017-11-09 11:33:08 +08:00
return false;
});
setInterval(() => {
if (lastProtectedSessionOperationDate !== null && new Date().getTime() - lastProtectedSessionOperationDate.getTime() > protectedSessionTimeout * 1000) {
2017-11-15 11:34:33 +08:00
resetProtectedSession();
2017-11-09 11:33:08 +08:00
}
}, 5000);
2017-11-05 06:18:55 +08:00
return {
setProtectedSessionTimeout,
ensureProtectedSession,
2017-11-15 11:34:33 +08:00
resetProtectedSession,
isProtectedSessionAvailable,
protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
getProtectedSessionId,
2017-11-15 13:04:26 +08:00
touchProtectedSession,
protectSubTree,
ensureDialogIsClosed
2017-11-05 06:18:55 +08:00
};
})();