Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
cosmetic cleanup, mostly in encryption
azivner4efa00f36bf4
diff --git a/public/javascripts/dialogs/event_log.js b/public/javascripts/dialogs/event_log.jsindex 1dedf18639..bd650eb05c 100644--- a/public/javascripts/dialogs/event_log.js+++ b/public/javascripts/dialogs/event_log.js@@ -16,7 +16,7 @@ const eventLog = (function() {const result = await $.ajax({url: baseApiUrl + 'event-log',type: 'GET',- error: () => error("Error getting event log.")+ error: () => showError("Error getting event log.")});listEl.html('');diff --git a/public/javascripts/dialogs/note_history.js b/public/javascripts/dialogs/note_history.jsindex 0f40cd1e73..07f2e5cc78 100644--- a/public/javascripts/dialogs/note_history.js+++ b/public/javascripts/dialogs/note_history.js@@ -27,7 +27,7 @@ const noteHistory = (function() {historyItems = await $.ajax({url: baseApiUrl + 'notes-history/' + noteId,type: 'GET',- error: () => error("Error getting note history.")+ error: () => showError("Error getting note history.")});for (const item of historyItems) {diff --git a/public/javascripts/dialogs/recent_changes.js b/public/javascripts/dialogs/recent_changes.jsindex 89fc3d3637..86a5af0860 100644--- a/public/javascripts/dialogs/recent_changes.js+++ b/public/javascripts/dialogs/recent_changes.js@@ -15,7 +15,7 @@ const recentChanges = (function() {const result = await $.ajax({url: baseApiUrl + 'recent-changes/',type: 'GET',- error: () => error("Error getting recent changes.")+ error: () => showError("Error getting recent changes.")});dialogEl.html('');diff --git a/public/javascripts/dialogs/recent_notes.js b/public/javascripts/dialogs/recent_notes.jsindex 52c3f4416f..f995dab636 100644--- a/public/javascripts/dialogs/recent_notes.js+++ b/public/javascripts/dialogs/recent_notes.js@@ -11,7 +11,7 @@ const recentNotes = (function() {$.ajax({url: baseApiUrl + 'recent-notes',type: 'GET',- error: () => error("Error getting recent notes.")+ error: () => showError("Error getting recent notes.")}).then(result => {list = result.map(r => r.note_id);});@@ -23,7 +23,7 @@ const recentNotes = (function() {$.ajax({url: baseApiUrl + 'recent-notes/' + noteTreeId,type: 'PUT',- error: () => error("Error setting recent notes.")+ error: () => showError("Error setting recent notes.")}).then(result => {list = result.map(r => r.note_id);});@@ -35,7 +35,7 @@ const recentNotes = (function() {$.ajax({url: baseApiUrl + 'recent-notes/' + noteIdToRemove,type: 'DELETE',- error: () => error("Error removing note from recent notes.")+ error: () => showError("Error removing note from recent notes.")}).then(result => {list = result.map(r => r.note_id);});diff --git a/public/javascripts/dialogs/settings.js b/public/javascripts/dialogs/settings.jsindex 46115304c4..63ae5aa516 100644--- a/public/javascripts/dialogs/settings.js+++ b/public/javascripts/dialogs/settings.js@@ -16,7 +16,7 @@ const settings = (function() {const settings = await $.ajax({url: baseApiUrl + 'settings',type: 'GET',- error: () => error("Error getting settings.")+ error: () => showError("Error getting settings.")});dialogEl.dialog({@@ -41,7 +41,7 @@ const settings = (function() {}),contentType: "application/json",success: () => {- message("Settings change have been saved.");+ showMessage("Settings change have been saved.");},error: () => alert("Error occurred during saving settings change.")});@@ -95,10 +95,10 @@ settings.addModule((function() {encryption.setEncryptedDataKey(result.new_encrypted_data_key);}else {- error(result.message);+ showError(result.message);}},- error: () => error("Error occurred during changing password.")+ error: () => showError("Error occurred during changing password.")});return false;diff --git a/public/javascripts/encryption.js b/public/javascripts/encryption.jsindex 08336c98c1..3df43aabfa 100644--- a/public/javascripts/encryption.js+++ b/public/javascripts/encryption.js@@ -8,16 +8,16 @@ const encryption = (function() {let encryptionDeferred = null;let dataKey = null;let lastEncryptionOperationDate = null;- let encryptionSalt = null;+ let passwordDerivedKeySalt = null;let encryptedDataKey = null;let encryptionSessionTimeout = null;$.ajax({url: baseApiUrl + 'settings/all',type: 'GET',- error: () => error("Error getting encryption settings.")+ error: () => showError("Error getting encryption settings.")}).then(settings => {- encryptionSalt = settings.password_derived_key_salt;+ passwordDerivedKeySalt = settings.password_derived_key_salt;encryptionSessionTimeout = settings.encryption_session_timeout;encryptedDataKey = settings.encrypted_data_key;});@@ -34,6 +34,9 @@ const encryption = (function() {const dfd = $.Deferred();if (requireEncryption && dataKey === null) {+ // if this is entry point then we need to show the app even before the note is loaded+ showAppIfHidden();+encryptionDeferred = dfd;dialogEl.dialog({@@ -54,21 +57,15 @@ const encryption = (function() {return dfd.promise();}- function getDataKey(password) {- return computeScrypt(password, encryptionSalt, (key, resolve, reject) => {- const dataKeyAes = getDataKeyAes(key);-- const decryptedDataKey = decrypt(dataKeyAes, encryptedDataKey);+ async function getDataKey(password) {+ const passwordDerivedKey = await computeScrypt(password, passwordDerivedKeySalt);- if (decryptedDataKey === false) {- reject("Wrong password.");- }+ const dataKeyAes = getDataKeyAes(passwordDerivedKey);- resolve(decryptedDataKey);- });+ return decrypt(dataKeyAes, encryptedDataKey);}- function computeScrypt(password, salt, callback) {+ function computeScrypt(password, salt) {const normalizedPassword = password.normalize('NFKC');const passwordBuffer = new buffer.SlowBuffer(normalizedPassword);const saltBuffer = new buffer.SlowBuffer(salt);@@ -78,22 +75,15 @@ const encryption = (function() {// 32 byte key - AES 256const dkLen = 32;- const startedDate = new Date();-return new Promise((resolve, reject) => {scrypt(passwordBuffer, saltBuffer, N, r, p, dkLen, (error, progress, key) => {if (error) {- console.log("Error: " + error);+ showError(error);- reject();+ reject(error);}else if (key) {- console.log("Computation took " + (new Date().getTime() - startedDate.getTime()) + "ms");-- callback(key, resolve, reject);- }- else {- // update UI with progress complete+ resolve(key);}});});@@ -115,31 +105,28 @@ const encryption = (function() {}}- encryptionPasswordFormEl.submit(() => {+ async function setupEncryptionSession() {const password = encryptionPasswordEl.val();encryptionPasswordEl.val("");- getDataKey(password).then(key => {- dialogEl.dialog("close");-- dataKey = key;+ const key = await getDataKey(password);+ if (key === false) {+ showError("Wrong password!");+ return;+ }- decryptTreeItems();+ dialogEl.dialog("close");- if (encryptionDeferred !== null) {- encryptionDeferred.resolve();+ dataKey = key;- encryptionDeferred = null;- }- })- .catch(reason => {- console.log(reason);+ decryptTreeItems();- error(reason);- });+ if (encryptionDeferred !== null) {+ encryptionDeferred.resolve();- return false;- });+ encryptionDeferred = null;+ }+ }function resetEncryptionSession() {dataKey = null;@@ -151,12 +138,6 @@ const encryption = (function() {}}- setInterval(() => {- if (lastEncryptionOperationDate !== null && new Date().getTime() - lastEncryptionOperationDate.getTime() > encryptionSessionTimeout * 1000) {- resetEncryptionSession();- }- }, 5000);-function isEncryptionAvailable() {return dataKey !== null;}@@ -167,8 +148,8 @@ const encryption = (function() {return new aesjs.ModeOfOperation.ctr(dataKey, new aesjs.Counter(5));}- function getDataKeyAes(key) {- return new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));+ function getDataKeyAes(passwordDerivedKey) {+ return new aesjs.ModeOfOperation.ctr(passwordDerivedKey, new aesjs.Counter(5));}function encryptNoteIfNecessary(note) {@@ -264,7 +245,7 @@ const encryption = (function() {const result = await $.ajax({url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),type: 'GET',- error: () => error("Error getting note history.")+ error: () => showError("Error getting note history.")});for (const row of result) {@@ -284,7 +265,7 @@ const encryption = (function() {type: 'PUT',contentType: 'application/json',data: JSON.stringify(row),- error: () => error("Error de/encrypting note history.")+ error: () => showError("Error de/encrypting note history.")});console.log('Note history ' + row.note_history_id + ' de/encrypted');@@ -342,7 +323,7 @@ const encryption = (function() {}});- message("Encryption finished.");+ showMessage("Encryption finished.");}async function decryptSubTree(noteId) {@@ -369,7 +350,7 @@ const encryption = (function() {}});- message("Decryption finished.");+ showMessage("Decryption finished.");}function updateSubTreeRecursively(noteId, updateCallback, successCallback) {@@ -412,17 +393,26 @@ const encryption = (function() {successCallback(note);}},- error: () => {- console.log("Updating " + noteId + " failed.");- }+ error: () => showError("Updating " + noteId + " failed.")+});},- error: () => {- console.log("Reading " + noteId + " failed.");- }+ error: () => showError("Reading " + noteId + " failed.")});}+ encryptionPasswordFormEl.submit(() => {+ setupEncryptionSession();++ return false;+ });++ setInterval(() => {+ if (lastEncryptionOperationDate !== null && new Date().getTime() - lastEncryptionOperationDate.getTime() > encryptionSessionTimeout * 1000) {+ resetEncryptionSession();+ }+ }, 5000);+return {setEncryptedDataKey,setEncryptionSessionTimeout,diff --git a/public/javascripts/migration.js b/public/javascripts/migration.jsindex 27da713152..ca10779e38 100644--- a/public/javascripts/migration.js+++ b/public/javascripts/migration.js@@ -40,6 +40,6 @@ $("#run-migration").click(() => {$("#migration-table").append(row);}},- error: () => error("Migration failed with unknown error")+ error: () => showError("Migration failed with unknown error")});});\ No newline at end of filediff --git a/public/javascripts/note_editor.js b/public/javascripts/note_editor.jsindex 213a11e068..8f374a728d 100644--- a/public/javascripts/note_editor.js+++ b/public/javascripts/note_editor.js@@ -104,13 +104,13 @@ const noteEditor = (function() {data: JSON.stringify(note),contentType: "application/json",error: () => {- error("Error saving the note!");+ showError("Error saving the note!");}});isNoteChanged = false;- message("Saved!");+ showMessage("Saved!");}function createNewTopLevelNote() {@@ -165,7 +165,7 @@ const noteEditor = (function() {node.renderTitle();}- message("Created!");+ showMessage("Created!");}function setTreeBasedOnEncryption(note) {diff --git a/public/javascripts/status.js b/public/javascripts/status.jsindex e001487e38..a35914b39d 100644--- a/public/javascripts/status.js+++ b/public/javascripts/status.js@@ -33,7 +33,7 @@ const status = (function() {}if (resp.changedCurrentNote) {- message('Reloading note because background change');+ showMessage('Reloading note because background change');noteEditor.reload();}diff --git a/public/javascripts/sync.js b/public/javascripts/sync.jsindex b3c84aa2fd..0bc32696b5 100644--- a/public/javascripts/sync.js+++ b/public/javascripts/sync.js@@ -8,16 +8,16 @@ function syncNow() {if (result.success) {status.checkStatus();- message("Sync finished successfully.");+ showMessage("Sync finished successfully.");}else {if (result.message.length > 50) {result.message = result.message.substr(0, 50);}- error("Sync failed: " + result.message);+ showError("Sync failed: " + result.message);}},- error: () => error("Sync failed for unknown reason.")+ error: () => showError("Sync failed for unknown reason.")});}\ No newline at end of filediff --git a/public/javascripts/utils.js b/public/javascripts/utils.jsindex 5ca522631e..b231344b84 100644--- a/public/javascripts/utils.js+++ b/public/javascripts/utils.js@@ -1,6 +1,8 @@"use strict";-function message(str) {+function showMessage(str) {+ console.log("message: ", str);+const top = $("#top-message");top.fadeIn(1500).css("display","inline-block");@@ -8,7 +10,9 @@ function message(str) {top.fadeOut(1500);}-function error(str) {+function showError(str) {+ console.log("error: ", str);+const error = $("#error-message");error.show().css("display","inline-block");diff --git a/services/log.js b/services/log.jsindex 2470cd41d6..f18892f626 100644--- a/services/log.js+++ b/services/log.js@@ -21,7 +21,7 @@ function info(message) {}function error(message) {- // we're using .info() instead of .error() because simple-node-logger emits weird error for error()+ // we're using .info() instead of .error() because simple-node-logger emits weird error for showError()info(message);}