mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 18:05:55 +01:00 
			
		
		
		
	cosmetic cleanup, mostly in encryption
This commit is contained in:
		| @@ -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(''); | ||||
|   | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -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(''); | ||||
|   | ||||
| @@ -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); | ||||
|         }); | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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); | ||||
|     async function getDataKey(password) { | ||||
|         const passwordDerivedKey = await computeScrypt(password, passwordDerivedKeySalt); | ||||
|  | ||||
|             const decryptedDataKey = decrypt(dataKeyAes, encryptedDataKey); | ||||
|         const dataKeyAes = getDataKeyAes(passwordDerivedKey); | ||||
|  | ||||
|             if (decryptedDataKey === false) { | ||||
|                 reject("Wrong password."); | ||||
|             } | ||||
|  | ||||
|             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 256 | ||||
|         const 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"); | ||||
|         const key = await getDataKey(password); | ||||
|         if (key === false) { | ||||
|             showError("Wrong password!"); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|             dataKey = key; | ||||
|         dialogEl.dialog("close"); | ||||
|  | ||||
|             decryptTreeItems(); | ||||
|         dataKey = key; | ||||
|  | ||||
|             if (encryptionDeferred !== null) { | ||||
|                 encryptionDeferred.resolve(); | ||||
|         decryptTreeItems(); | ||||
|  | ||||
|                 encryptionDeferred = null; | ||||
|             } | ||||
|         }) | ||||
|             .catch(reason => { | ||||
|                 console.log(reason); | ||||
|         if (encryptionDeferred !== null) { | ||||
|             encryptionDeferred.resolve(); | ||||
|  | ||||
|                 error(reason); | ||||
|             }); | ||||
|  | ||||
|         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, | ||||
|   | ||||
| @@ -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") | ||||
|     }); | ||||
| }); | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -33,7 +33,7 @@ const status = (function() { | ||||
|         } | ||||
|  | ||||
|         if (resp.changedCurrentNote) { | ||||
|             message('Reloading note because background change'); | ||||
|             showMessage('Reloading note because background change'); | ||||
|  | ||||
|             noteEditor.reload(); | ||||
|         } | ||||
|   | ||||
| @@ -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.") | ||||
|     }); | ||||
| } | ||||
| @@ -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"); | ||||
|   | ||||
| @@ -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); | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user