trilium/src/public/javascripts/protected_session.js

189 lines
5.3 KiB
JavaScript
Raw Normal View History

"use strict";
const protected_session = (function() {
2018-02-15 12:31:20 +08:00
const $dialog = $("#protected-session-password-dialog");
const $passwordForm = $("#protected-session-password-form");
const $password = $("#protected-session-password");
const $noteDetailWrapper = $("#note-detail-wrapper");
const $protectButton = $("#protect-button");
const $unprotectButton = $("#unprotect-button");
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()) {
protectedSessionDeferred = dfd;
2018-03-25 09:39:15 +08:00
if (treeService.getCurrentNode().data.isProtected) {
$noteDetailWrapper.hide();
}
2018-02-15 12:31:20 +08:00
$dialog.dialog({
2017-11-05 06:18:55 +08:00
modal: modal,
width: 400,
open: () => {
if (!modal) {
// dialog steals focus for itself, which is not what we want for non-modal (viewing)
2018-03-25 09:39:15 +08:00
treeService.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() {
2018-02-15 12:31:20 +08:00
const password = $password.val();
$password.val("");
2017-11-11 11:55:19 +08:00
const response = await enterProtectedSession(password);
if (!response.success) {
2018-03-25 11:37:55 +08:00
utils.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;
2018-02-15 12:31:20 +08:00
$dialog.dialog("close");
2017-11-13 10:40:26 +08:00
noteEditor.reload();
2018-03-25 09:39:15 +08:00
treeService.reload();
2017-11-15 11:34:33 +08:00
if (protectedSessionDeferred !== null) {
2018-02-15 12:31:20 +08:00
ensureDialogIsClosed($dialog, $password);
2017-11-15 12:01:23 +08:00
2018-02-15 12:31:20 +08:00
$noteDetailWrapper.show();
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 {
2018-02-15 12:31:20 +08:00
$dialog.dialog('close');
}
catch (e) {}
2018-02-15 12:31:20 +08:00
$password.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
2018-03-25 11:37:55 +08:00
utils.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);
2018-01-29 08:30:14 +08:00
note.detail.isProtected = true;
2017-11-05 06:18:55 +08:00
await noteEditor.saveNoteToServer(note);
2018-03-25 09:39:15 +08:00
treeService.setProtected(note.detail.noteId, note.detail.isProtected);
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);
2018-01-29 08:30:14 +08:00
note.detail.isProtected = false;
2017-11-05 06:18:55 +08:00
await noteEditor.saveNoteToServer(note);
2018-03-25 09:39:15 +08:00
treeService.setProtected(note.detail.noteId, note.detail.isProtected);
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);
await server.put('notes/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0));
2017-11-15 13:04:26 +08:00
2018-03-25 11:37:55 +08:00
utils.showMessage("Request to un/protect sub tree has finished successfully");
2017-11-15 13:04:26 +08:00
2018-03-25 09:39:15 +08:00
treeService.reload();
2017-11-15 13:04:26 +08:00
noteEditor.reload();
}
2018-02-15 12:31:20 +08:00
$passwordForm.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);
$protectButton.click(protectNoteAndSendToServer);
$unprotectButton.click(unprotectNoteAndSendToServer);
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
};
})();