2017-11-05 07:38:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-15 11:36:36 +08:00
|
|
|
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;
|
2017-11-15 11:44:45 +08:00
|
|
|
let protectedSessionTimeout = null;
|
2017-11-11 11:55:19 +08:00
|
|
|
let protectedSessionId = null;
|
2017-11-05 06:18:55 +08:00
|
|
|
|
2017-11-30 12:30:35 +08:00
|
|
|
$(document).ready(() => {
|
|
|
|
server.get('settings/all').then(settings => protectedSessionTimeout = settings.protected_session_timeout);
|
2017-11-05 07:57:40 +08:00
|
|
|
});
|
|
|
|
|
2017-11-15 11:44:45 +08:00
|
|
|
function setProtectedSessionTimeout(encSessTimeout) {
|
|
|
|
protectedSessionTimeout = encSessTimeout;
|
2017-09-07 10:06:43 +08:00
|
|
|
}
|
2017-11-04 08:01:32 +08:00
|
|
|
|
2017-11-15 11:34:33 +08:00
|
|
|
function ensureProtectedSession(requireProtectedSession, modal) {
|
2017-11-05 06:18:55 +08:00
|
|
|
const dfd = $.Deferred();
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-15 11:34:33 +08:00
|
|
|
if (requireProtectedSession && !isProtectedSessionAvailable()) {
|
|
|
|
protectedSessionDeferred = dfd;
|
2017-09-07 10:06:43 +08:00
|
|
|
|
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-19 06:05:50 +08:00
|
|
|
noteTree.getCurrentNode().setFocus();
|
2017-11-05 06:18:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dfd.resolve();
|
|
|
|
}
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-05 06:18:55 +08:00
|
|
|
return dfd.promise();
|
|
|
|
}
|
2017-09-09 08:55:24 +08:00
|
|
|
|
2017-11-15 11:21:56 +08:00
|
|
|
async function setupProtectedSession() {
|
2017-11-15 11:34:33 +08:00
|
|
|
const password = passwordEl.val();
|
|
|
|
passwordEl.val("");
|
2017-09-07 10:06:43 +08:00
|
|
|
|
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-09-07 11:13:39 +08:00
|
|
|
|
2017-11-11 11:55:19 +08:00
|
|
|
dialogEl.dialog("close");
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-13 10:40:26 +08:00
|
|
|
noteEditor.reload();
|
2017-11-11 11:55:19 +08:00
|
|
|
noteTree.reload();
|
2017-09-18 00:46:14 +08:00
|
|
|
|
2017-11-15 11:34:33 +08:00
|
|
|
if (protectedSessionDeferred !== null) {
|
2017-11-15 13:10:11 +08:00
|
|
|
ensureDialogIsClosed(dialogEl, passwordEl);
|
2017-11-15 12:01:23 +08:00
|
|
|
|
2017-11-15 11:34:33 +08:00
|
|
|
protectedSessionDeferred.resolve();
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-15 11:34:33 +08:00
|
|
|
protectedSessionDeferred = null;
|
2017-11-09 11:33:08 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-15 13:10:11 +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) {
|
2017-11-29 09:52:38 +08:00
|
|
|
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;
|
|
|
|
|
2017-11-10 12:25:23 +08:00
|
|
|
// most secure solution - guarantees nothing remained in memory
|
|
|
|
// since this expires because user doesn't use the app, it shouldn't be disruptive
|
2017-11-30 12:30:35 +08:00
|
|
|
reloadApp();
|
2017-09-07 11:16:54 +08:00
|
|
|
}
|
2017-09-13 11:07:08 +08:00
|
|
|
|
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
|
|
|
}
|
2017-09-09 10:43:02 +08:00
|
|
|
|
2017-11-15 10:54:12 +08:00
|
|
|
async function protectNoteAndSendToServer() {
|
|
|
|
await ensureProtectedSession(true, true);
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-05 06:18:55 +08:00
|
|
|
const note = noteEditor.getCurrentNote();
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-05 06:18:55 +08:00
|
|
|
noteEditor.updateNoteFromInputs(note);
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-15 10:54:12 +08:00
|
|
|
note.detail.is_protected = true;
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-05 06:18:55 +08:00
|
|
|
await noteEditor.saveNoteToServer(note);
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-12-25 22:30:37 +08:00
|
|
|
noteTree.setProtected(note.detail.note_id, note.detail.is_protected);
|
|
|
|
|
2017-11-15 11:21:56 +08:00
|
|
|
noteEditor.setNoteBackgroundIfProtected(note);
|
2017-11-05 06:18:55 +08:00
|
|
|
}
|
2017-11-03 11:36:58 +08:00
|
|
|
|
2017-11-15 10:54:12 +08:00
|
|
|
async function unprotectNoteAndSendToServer() {
|
|
|
|
await ensureProtectedSession(true, true);
|
2017-11-03 11:55:22 +08:00
|
|
|
|
2017-11-05 06:18:55 +08:00
|
|
|
const note = noteEditor.getCurrentNote();
|
2017-11-04 08:01:32 +08:00
|
|
|
|
2017-11-05 06:18:55 +08:00
|
|
|
noteEditor.updateNoteFromInputs(note);
|
2017-11-03 11:36:58 +08:00
|
|
|
|
2017-11-15 10:54:12 +08:00
|
|
|
note.detail.is_protected = false;
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-11-05 06:18:55 +08:00
|
|
|
await noteEditor.saveNoteToServer(note);
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2017-12-25 22:30:37 +08:00
|
|
|
noteTree.setProtected(note.detail.note_id, note.detail.is_protected);
|
|
|
|
|
2017-11-15 11:21:56 +08:00
|
|
|
noteEditor.setNoteBackgroundIfProtected(note);
|
2017-11-05 06:18:55 +08:00
|
|
|
}
|
2017-09-18 00:46:14 +08:00
|
|
|
|
2017-11-15 11:50:56 +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);
|
|
|
|
|
2018-01-14 09:53:00 +08:00
|
|
|
await server.put('notes/' + 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(() => {
|
2017-11-15 11:21:56 +08:00
|
|
|
setupProtectedSession();
|
2017-11-09 11:33:08 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
setInterval(() => {
|
2017-11-15 11:44:45 +08:00
|
|
|
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 {
|
2017-11-15 11:44:45 +08:00
|
|
|
setProtectedSessionTimeout,
|
2017-11-15 10:54:12 +08:00
|
|
|
ensureProtectedSession,
|
2017-11-15 11:34:33 +08:00
|
|
|
resetProtectedSession,
|
|
|
|
isProtectedSessionAvailable,
|
2017-11-15 10:54:12 +08:00
|
|
|
protectNoteAndSendToServer,
|
|
|
|
unprotectNoteAndSendToServer,
|
2017-11-15 11:50:56 +08:00
|
|
|
getProtectedSessionId,
|
2017-11-15 13:04:26 +08:00
|
|
|
touchProtectedSession,
|
2017-11-15 13:10:11 +08:00
|
|
|
protectSubTree,
|
|
|
|
ensureDialogIsClosed
|
2017-11-05 06:18:55 +08:00
|
|
|
};
|
|
|
|
})();
|