From a024978d7b26f80f0009a4bfefd0ba4c84af7734 Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 5 Sep 2017 23:48:41 -0400 Subject: [PATCH] jquery ui dialog for encryption password --- src/templates/app.html | 11 ++++ static/js/init.js | 2 + static/js/note.js | 118 ++++++++++++++++++++++++++--------------- static/js/tree.js | 2 + 4 files changed, 91 insertions(+), 42 deletions(-) diff --git a/src/templates/app.html b/src/templates/app.html index e959bf17d..8d94b4c92 100644 --- a/src/templates/app.html +++ b/src/templates/app.html @@ -98,6 +98,17 @@ + + diff --git a/static/js/init.js b/static/js/init.js index d9821b90b..12d4f051f 100644 --- a/static/js/init.js +++ b/static/js/init.js @@ -4,6 +4,8 @@ $(function() { if (fancyTree.length) { fancyTree.height($(window).height() - fancyTree.offset().top - 10); + + console.log("height: ", $(window).height() - fancyTree.offset().top - 10); } const noteEditable = $('div.note-editable'); diff --git a/static/js/note.js b/static/js/note.js index 8b356c32c..5a18ec281 100644 --- a/static/js/note.js +++ b/static/js/note.js @@ -67,9 +67,9 @@ function saveNoteIfChanged(callback) { updateNoteFromInputs(note); - encryptNoteIfNecessary(note).then(note => { - saveNoteToServer(note, callback); - }); + encryptNoteIfNecessary(note); + + saveNoteToServer(note, callback); } setInterval(saveNoteIfChanged, 5000); @@ -154,6 +154,23 @@ function setNoteBackgroundIfEncrypted(note) { } } +let globalEncryptionCallback = null; + +function handleEncryption(requireEncryption, callback) { + if (requireEncryption && globalEncryptionKey === null) { + globalEncryptionCallback = callback; + + $("#noteDetailWrapper").hide(); + $("#encryptionPasswordDialog").dialog({ + modal: false, + width: 400 + }); + } + else { + callback(); + } +} + function loadNote(noteId) { $.get(baseUrl + 'notes/' + noteId).then(function(note) { globalNote = note; @@ -166,8 +183,16 @@ function loadNote(noteId) { $("#noteTitle").focus().select(); } - decryptNoteIfNecessary(note).then(decrypted => { - note.detail.note_text = decrypted; + handleEncryption(note.detail.encryption > 0, () => { + $("#noteDetailWrapper").show(); + try { + $("#encryptionPasswordDialog").dialog('close'); + } + catch(e) {} + + $("#encryptionPassword").val(''); + + note.detail.note_text = decryptNoteIfNecessary(note); let noteText = notecase2html(note); @@ -256,27 +281,35 @@ function deriveEncryptionKey(password) { }); } -let globalEncryptionKeyPromise = null; +let globalEncryptionKey = null; -function getEncryptionKey() { - if (globalEncryptionKeyPromise === null) { - const password = prompt("Enter password for encryption"); +$("#encryptionPasswordForm").submit(function() { + const password = $("#encryptionPassword").val(); + $("#encryptionPassword").val(""); - globalEncryptionKeyPromise = deriveEncryptionKey(password); - } + deriveEncryptionKey(password).then(key => { + $("#noteDetailWrapper").show(); + $("#encryptionPasswordDialog").dialog("close"); - return globalEncryptionKeyPromise; -} + globalEncryptionKey = key; + + if (globalEncryptionCallback !== null) { + globalEncryptionCallback(); + + globalEncryptionCallback = null; + } + }); + + return false; +}); function getAes() { - return getEncryptionKey().then(encryptionKey => { - return new aesjs.ModeOfOperation.ctr(encryptionKey, new aesjs.Counter(5)); - }); + return new aesjs.ModeOfOperation.ctr(globalEncryptionKey, new aesjs.Counter(5)); } function encryptNoteIfNecessary(note) { if (note.detail.encryption === 0) { - return Promise.resolve(note); + return note; } else { return encryptNote(note); @@ -284,24 +317,27 @@ function encryptNoteIfNecessary(note) { } function encryptNote(note) { - return getAes().then(aes => { - const noteJson = note.detail.note_text; + const aes = getAes(); + const noteJson = note.detail.note_text; - const noteBytes = aesjs.utils.utf8.toBytes(noteJson); + const noteBytes = aesjs.utils.utf8.toBytes(noteJson); - const encryptedBytes = aes.encrypt(noteBytes); + const encryptedBytes = aes.encrypt(noteBytes); - // To print or store the binary data, you may convert it to hex - note.detail.note_text = uint8ToBase64(encryptedBytes); + // To print or store the binary data, you may convert it to hex + note.detail.note_text = uint8ToBase64(encryptedBytes); - return note; - }); + return note; } function encryptNoteAndSendToServer() { - updateNoteFromInputs(globalNote); + handleEncryption(true, () => { + const note = globalNote; + + updateNoteFromInputs(note); + + encryptNote(note); - encryptNote(globalNote).then(note => { note.detail.encryption = 1; saveNoteToServer(note); @@ -311,37 +347,35 @@ function encryptNoteAndSendToServer() { } function decryptNoteAndSendToServer() { - const note = globalNote; + handleEncryption(true, () => { + const note = globalNote; - updateNoteFromInputs(note); + updateNoteFromInputs(note); - note.detail.encryption = 0; + note.detail.encryption = 0; - saveNoteToServer(note); + saveNoteToServer(note); - setNoteBackgroundIfEncrypted(note); + setNoteBackgroundIfEncrypted(note); + }); } function decryptNoteIfNecessary(note) { - let decryptPromise; - if (note.detail.encryption === 1) { - decryptPromise = decryptNote(note.detail.note_text); + return decryptNote(note.detail.note_text); } else { - decryptPromise = Promise.resolve(note.detail.note_text); + return note.detail.note_text; } - return decryptPromise; } function decryptNote(encryptedBase64) { - return getAes().then(aes => { - const encryptedBytes = base64ToUint8Array(encryptedBase64); + const aes = getAes(); + const encryptedBytes = base64ToUint8Array(encryptedBase64); - const decryptedBytes = aes.decrypt(encryptedBytes); + const decryptedBytes = aes.decrypt(encryptedBytes); - return aesjs.utils.utf8.fromBytes(decryptedBytes); - }); + return aesjs.utils.utf8.fromBytes(decryptedBytes); } function uint8ToBase64(u8Arr) { diff --git a/static/js/tree.js b/static/js/tree.js index 91483942f..7679c69b7 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -212,6 +212,8 @@ $(function(){ if (startNoteId) { data.tree.activateKey(startNoteId); } + + $(window).resize(); }, hotkeys: { keydown: keybindings