Notes/public/javascripts/encryption.js

157 lines
4.3 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-04 18:18:55 -04:00
const encryption = (function() {
const dialogEl = $("#encryption-password-dialog");
const encryptionPasswordFormEl = $("#encryption-password-form");
const encryptionPasswordEl = $("#encryption-password");
let encryptionDeferred = null;
let lastEncryptionOperationDate = null;
let encryptionSessionTimeout = null;
2017-11-10 22:55:19 -05:00
let protectedSessionId = null;
2017-11-04 18:18:55 -04:00
$.ajax({
url: baseApiUrl + 'settings/all',
type: 'GET',
2017-11-08 22:33:08 -05:00
error: () => showError("Error getting encryption settings.")
}).then(settings => {
encryptionSessionTimeout = settings.encryption_session_timeout;
});
2017-11-04 18:18:55 -04:00
function setEncryptionSessionTimeout(encSessTimeout) {
encryptionSessionTimeout = encSessTimeout;
}
function ensureProtectedSession(requireEncryption, modal) {
2017-11-04 18:18:55 -04:00
const dfd = $.Deferred();
2017-11-12 21:40:26 -05:00
if (requireEncryption && !isEncryptionAvailable()) {
2017-11-08 22:33:08 -05:00
// if this is entry point then we need to show the app even before the note is loaded
showAppIfHidden();
2017-11-04 18:18:55 -04:00
encryptionDeferred = dfd;
2017-11-04 18:18:55 -04: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-04 22:18:36 -04:00
treeUtils.getNodeByKey(noteEditor.getCurrentNoteId()).setFocus();
2017-11-04 18:18:55 -04:00
}
}
});
}
else {
dfd.resolve();
}
2017-11-04 18:18:55 -04:00
return dfd.promise();
}
async function setupProtectedSession() {
2017-11-04 18:18:55 -04:00
const password = encryptionPasswordEl.val();
encryptionPasswordEl.val("");
2017-11-10 22:55:19 -05:00
const response = await enterProtectedSession(password);
if (!response.success) {
showError("Wrong password.");
2017-11-08 22:33:08 -05:00
return;
}
2017-09-17 00:18:03 -04:00
2017-11-10 22:55:19 -05:00
protectedSessionId = response.protectedSessionId;
initAjax();
2017-11-10 22:55:19 -05:00
dialogEl.dialog("close");
2017-11-12 21:40:26 -05:00
noteEditor.reload();
2017-11-10 22:55:19 -05:00
noteTree.reload();
2017-11-08 22:33:08 -05:00
if (encryptionDeferred !== null) {
encryptionDeferred.resolve();
2017-11-08 22:33:08 -05:00
encryptionDeferred = null;
}
}
2017-11-10 22:55:19 -05: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-04 18:18:55 -04:00
function resetEncryptionSession() {
2017-11-10 22:55:19 -05: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-04 18:18:55 -04:00
function isEncryptionAvailable() {
2017-11-12 21:40:26 -05:00
return protectedSessionId !== null;
2017-11-04 18:18:55 -04:00
}
async function protectNoteAndSendToServer() {
await ensureProtectedSession(true, true);
2017-11-04 18:18:55 -04:00
const note = noteEditor.getCurrentNote();
2017-11-04 18:18:55 -04:00
noteEditor.updateNoteFromInputs(note);
note.detail.is_protected = true;
2017-11-04 18:18:55 -04:00
await noteEditor.saveNoteToServer(note);
noteEditor.setNoteBackgroundIfProtected(note);
2017-11-04 18:18:55 -04:00
}
async function unprotectNoteAndSendToServer() {
await ensureProtectedSession(true, true);
2017-11-04 18:18:55 -04:00
const note = noteEditor.getCurrentNote();
2017-11-04 18:18:55 -04:00
noteEditor.updateNoteFromInputs(note);
note.detail.is_protected = false;
2017-11-04 18:18:55 -04:00
await noteEditor.saveNoteToServer(note);
noteEditor.setNoteBackgroundIfProtected(note);
2017-11-04 18:18:55 -04:00
}
2017-11-08 22:33:08 -05:00
encryptionPasswordFormEl.submit(() => {
setupProtectedSession();
2017-11-08 22:33:08 -05:00
return false;
});
setInterval(() => {
if (lastEncryptionOperationDate !== null && new Date().getTime() - lastEncryptionOperationDate.getTime() > encryptionSessionTimeout * 1000) {
resetEncryptionSession();
}
}, 5000);
2017-11-04 18:18:55 -04:00
return {
setEncryptionSessionTimeout,
ensureProtectedSession,
2017-11-04 18:18:55 -04:00
resetEncryptionSession,
isEncryptionAvailable,
protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
2017-11-10 22:55:19 -05:00
getProtectedSessionId
2017-11-04 18:18:55 -04:00
};
})();