mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
cosmetic cleanup, mostly in encryption
This commit is contained in:
parent
cd5f742a7b
commit
4efa00f36b
@ -16,7 +16,7 @@ const eventLog = (function() {
|
|||||||
const result = await $.ajax({
|
const result = await $.ajax({
|
||||||
url: baseApiUrl + 'event-log',
|
url: baseApiUrl + 'event-log',
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
error: () => error("Error getting event log.")
|
error: () => showError("Error getting event log.")
|
||||||
});
|
});
|
||||||
|
|
||||||
listEl.html('');
|
listEl.html('');
|
||||||
|
@ -27,7 +27,7 @@ const noteHistory = (function() {
|
|||||||
historyItems = await $.ajax({
|
historyItems = await $.ajax({
|
||||||
url: baseApiUrl + 'notes-history/' + noteId,
|
url: baseApiUrl + 'notes-history/' + noteId,
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
error: () => error("Error getting note history.")
|
error: () => showError("Error getting note history.")
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const item of historyItems) {
|
for (const item of historyItems) {
|
||||||
|
@ -15,7 +15,7 @@ const recentChanges = (function() {
|
|||||||
const result = await $.ajax({
|
const result = await $.ajax({
|
||||||
url: baseApiUrl + 'recent-changes/',
|
url: baseApiUrl + 'recent-changes/',
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
error: () => error("Error getting recent changes.")
|
error: () => showError("Error getting recent changes.")
|
||||||
});
|
});
|
||||||
|
|
||||||
dialogEl.html('');
|
dialogEl.html('');
|
||||||
|
@ -11,7 +11,7 @@ const recentNotes = (function() {
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'recent-notes',
|
url: baseApiUrl + 'recent-notes',
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
error: () => error("Error getting recent notes.")
|
error: () => showError("Error getting recent notes.")
|
||||||
}).then(result => {
|
}).then(result => {
|
||||||
list = result.map(r => r.note_id);
|
list = result.map(r => r.note_id);
|
||||||
});
|
});
|
||||||
@ -23,7 +23,7 @@ const recentNotes = (function() {
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'recent-notes/' + noteTreeId,
|
url: baseApiUrl + 'recent-notes/' + noteTreeId,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
error: () => error("Error setting recent notes.")
|
error: () => showError("Error setting recent notes.")
|
||||||
}).then(result => {
|
}).then(result => {
|
||||||
list = result.map(r => r.note_id);
|
list = result.map(r => r.note_id);
|
||||||
});
|
});
|
||||||
@ -35,7 +35,7 @@ const recentNotes = (function() {
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'recent-notes/' + noteIdToRemove,
|
url: baseApiUrl + 'recent-notes/' + noteIdToRemove,
|
||||||
type: 'DELETE',
|
type: 'DELETE',
|
||||||
error: () => error("Error removing note from recent notes.")
|
error: () => showError("Error removing note from recent notes.")
|
||||||
}).then(result => {
|
}).then(result => {
|
||||||
list = result.map(r => r.note_id);
|
list = result.map(r => r.note_id);
|
||||||
});
|
});
|
||||||
|
@ -16,7 +16,7 @@ const settings = (function() {
|
|||||||
const settings = await $.ajax({
|
const settings = await $.ajax({
|
||||||
url: baseApiUrl + 'settings',
|
url: baseApiUrl + 'settings',
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
error: () => error("Error getting settings.")
|
error: () => showError("Error getting settings.")
|
||||||
});
|
});
|
||||||
|
|
||||||
dialogEl.dialog({
|
dialogEl.dialog({
|
||||||
@ -41,7 +41,7 @@ const settings = (function() {
|
|||||||
}),
|
}),
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
success: () => {
|
success: () => {
|
||||||
message("Settings change have been saved.");
|
showMessage("Settings change have been saved.");
|
||||||
},
|
},
|
||||||
error: () => alert("Error occurred during saving settings change.")
|
error: () => alert("Error occurred during saving settings change.")
|
||||||
});
|
});
|
||||||
@ -95,10 +95,10 @@ settings.addModule((function() {
|
|||||||
encryption.setEncryptedDataKey(result.new_encrypted_data_key);
|
encryption.setEncryptedDataKey(result.new_encrypted_data_key);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
error(result.message);
|
showError(result.message);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: () => error("Error occurred during changing password.")
|
error: () => showError("Error occurred during changing password.")
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -8,16 +8,16 @@ const encryption = (function() {
|
|||||||
let encryptionDeferred = null;
|
let encryptionDeferred = null;
|
||||||
let dataKey = null;
|
let dataKey = null;
|
||||||
let lastEncryptionOperationDate = null;
|
let lastEncryptionOperationDate = null;
|
||||||
let encryptionSalt = null;
|
let passwordDerivedKeySalt = null;
|
||||||
let encryptedDataKey = null;
|
let encryptedDataKey = null;
|
||||||
let encryptionSessionTimeout = null;
|
let encryptionSessionTimeout = null;
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'settings/all',
|
url: baseApiUrl + 'settings/all',
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
error: () => error("Error getting encryption settings.")
|
error: () => showError("Error getting encryption settings.")
|
||||||
}).then(settings => {
|
}).then(settings => {
|
||||||
encryptionSalt = settings.password_derived_key_salt;
|
passwordDerivedKeySalt = settings.password_derived_key_salt;
|
||||||
encryptionSessionTimeout = settings.encryption_session_timeout;
|
encryptionSessionTimeout = settings.encryption_session_timeout;
|
||||||
encryptedDataKey = settings.encrypted_data_key;
|
encryptedDataKey = settings.encrypted_data_key;
|
||||||
});
|
});
|
||||||
@ -34,6 +34,9 @@ const encryption = (function() {
|
|||||||
const dfd = $.Deferred();
|
const dfd = $.Deferred();
|
||||||
|
|
||||||
if (requireEncryption && dataKey === null) {
|
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;
|
encryptionDeferred = dfd;
|
||||||
|
|
||||||
dialogEl.dialog({
|
dialogEl.dialog({
|
||||||
@ -54,21 +57,15 @@ const encryption = (function() {
|
|||||||
return dfd.promise();
|
return dfd.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDataKey(password) {
|
async function getDataKey(password) {
|
||||||
return computeScrypt(password, encryptionSalt, (key, resolve, reject) => {
|
const passwordDerivedKey = await computeScrypt(password, passwordDerivedKeySalt);
|
||||||
const dataKeyAes = getDataKeyAes(key);
|
|
||||||
|
|
||||||
const decryptedDataKey = decrypt(dataKeyAes, encryptedDataKey);
|
const dataKeyAes = getDataKeyAes(passwordDerivedKey);
|
||||||
|
|
||||||
if (decryptedDataKey === false) {
|
return decrypt(dataKeyAes, encryptedDataKey);
|
||||||
reject("Wrong password.");
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(decryptedDataKey);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeScrypt(password, salt, callback) {
|
function computeScrypt(password, salt) {
|
||||||
const normalizedPassword = password.normalize('NFKC');
|
const normalizedPassword = password.normalize('NFKC');
|
||||||
const passwordBuffer = new buffer.SlowBuffer(normalizedPassword);
|
const passwordBuffer = new buffer.SlowBuffer(normalizedPassword);
|
||||||
const saltBuffer = new buffer.SlowBuffer(salt);
|
const saltBuffer = new buffer.SlowBuffer(salt);
|
||||||
@ -78,22 +75,15 @@ const encryption = (function() {
|
|||||||
// 32 byte key - AES 256
|
// 32 byte key - AES 256
|
||||||
const dkLen = 32;
|
const dkLen = 32;
|
||||||
|
|
||||||
const startedDate = new Date();
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
scrypt(passwordBuffer, saltBuffer, N, r, p, dkLen, (error, progress, key) => {
|
scrypt(passwordBuffer, saltBuffer, N, r, p, dkLen, (error, progress, key) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log("Error: " + error);
|
showError(error);
|
||||||
|
|
||||||
reject();
|
reject(error);
|
||||||
}
|
}
|
||||||
else if (key) {
|
else if (key) {
|
||||||
console.log("Computation took " + (new Date().getTime() - startedDate.getTime()) + "ms");
|
resolve(key);
|
||||||
|
|
||||||
callback(key, resolve, reject);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// update UI with progress complete
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -115,31 +105,28 @@ const encryption = (function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
encryptionPasswordFormEl.submit(() => {
|
async function setupEncryptionSession() {
|
||||||
const password = encryptionPasswordEl.val();
|
const password = encryptionPasswordEl.val();
|
||||||
encryptionPasswordEl.val("");
|
encryptionPasswordEl.val("");
|
||||||
|
|
||||||
getDataKey(password).then(key => {
|
const key = await getDataKey(password);
|
||||||
dialogEl.dialog("close");
|
if (key === false) {
|
||||||
|
showError("Wrong password!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
dataKey = key;
|
dialogEl.dialog("close");
|
||||||
|
|
||||||
decryptTreeItems();
|
dataKey = key;
|
||||||
|
|
||||||
if (encryptionDeferred !== null) {
|
decryptTreeItems();
|
||||||
encryptionDeferred.resolve();
|
|
||||||
|
|
||||||
encryptionDeferred = null;
|
if (encryptionDeferred !== null) {
|
||||||
}
|
encryptionDeferred.resolve();
|
||||||
})
|
|
||||||
.catch(reason => {
|
|
||||||
console.log(reason);
|
|
||||||
|
|
||||||
error(reason);
|
encryptionDeferred = null;
|
||||||
});
|
}
|
||||||
|
}
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
function resetEncryptionSession() {
|
function resetEncryptionSession() {
|
||||||
dataKey = null;
|
dataKey = null;
|
||||||
@ -151,12 +138,6 @@ const encryption = (function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(() => {
|
|
||||||
if (lastEncryptionOperationDate !== null && new Date().getTime() - lastEncryptionOperationDate.getTime() > encryptionSessionTimeout * 1000) {
|
|
||||||
resetEncryptionSession();
|
|
||||||
}
|
|
||||||
}, 5000);
|
|
||||||
|
|
||||||
function isEncryptionAvailable() {
|
function isEncryptionAvailable() {
|
||||||
return dataKey !== null;
|
return dataKey !== null;
|
||||||
}
|
}
|
||||||
@ -167,8 +148,8 @@ const encryption = (function() {
|
|||||||
return new aesjs.ModeOfOperation.ctr(dataKey, new aesjs.Counter(5));
|
return new aesjs.ModeOfOperation.ctr(dataKey, new aesjs.Counter(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDataKeyAes(key) {
|
function getDataKeyAes(passwordDerivedKey) {
|
||||||
return new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
|
return new aesjs.ModeOfOperation.ctr(passwordDerivedKey, new aesjs.Counter(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
function encryptNoteIfNecessary(note) {
|
function encryptNoteIfNecessary(note) {
|
||||||
@ -264,7 +245,7 @@ const encryption = (function() {
|
|||||||
const result = await $.ajax({
|
const result = await $.ajax({
|
||||||
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),
|
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
error: () => error("Error getting note history.")
|
error: () => showError("Error getting note history.")
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const row of result) {
|
for (const row of result) {
|
||||||
@ -284,7 +265,7 @@ const encryption = (function() {
|
|||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
data: JSON.stringify(row),
|
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');
|
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) {
|
async function decryptSubTree(noteId) {
|
||||||
@ -369,7 +350,7 @@ const encryption = (function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
message("Decryption finished.");
|
showMessage("Decryption finished.");
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
|
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
|
||||||
@ -412,17 +393,26 @@ const encryption = (function() {
|
|||||||
successCallback(note);
|
successCallback(note);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => showError("Updating " + noteId + " failed.")
|
||||||
console.log("Updating " + noteId + " failed.");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => showError("Reading " + noteId + " failed.")
|
||||||
console.log("Reading " + noteId + " failed.");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
encryptionPasswordFormEl.submit(() => {
|
||||||
|
setupEncryptionSession();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
if (lastEncryptionOperationDate !== null && new Date().getTime() - lastEncryptionOperationDate.getTime() > encryptionSessionTimeout * 1000) {
|
||||||
|
resetEncryptionSession();
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setEncryptedDataKey,
|
setEncryptedDataKey,
|
||||||
setEncryptionSessionTimeout,
|
setEncryptionSessionTimeout,
|
||||||
|
@ -40,6 +40,6 @@ $("#run-migration").click(() => {
|
|||||||
$("#migration-table").append(row);
|
$("#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),
|
data: JSON.stringify(note),
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
error: () => {
|
error: () => {
|
||||||
error("Error saving the note!");
|
showError("Error saving the note!");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
isNoteChanged = false;
|
isNoteChanged = false;
|
||||||
|
|
||||||
message("Saved!");
|
showMessage("Saved!");
|
||||||
}
|
}
|
||||||
|
|
||||||
function createNewTopLevelNote() {
|
function createNewTopLevelNote() {
|
||||||
@ -165,7 +165,7 @@ const noteEditor = (function() {
|
|||||||
node.renderTitle();
|
node.renderTitle();
|
||||||
}
|
}
|
||||||
|
|
||||||
message("Created!");
|
showMessage("Created!");
|
||||||
}
|
}
|
||||||
|
|
||||||
function setTreeBasedOnEncryption(note) {
|
function setTreeBasedOnEncryption(note) {
|
||||||
|
@ -33,7 +33,7 @@ const status = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (resp.changedCurrentNote) {
|
if (resp.changedCurrentNote) {
|
||||||
message('Reloading note because background change');
|
showMessage('Reloading note because background change');
|
||||||
|
|
||||||
noteEditor.reload();
|
noteEditor.reload();
|
||||||
}
|
}
|
||||||
|
@ -8,16 +8,16 @@ function syncNow() {
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
status.checkStatus();
|
status.checkStatus();
|
||||||
|
|
||||||
message("Sync finished successfully.");
|
showMessage("Sync finished successfully.");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (result.message.length > 50) {
|
if (result.message.length > 50) {
|
||||||
result.message = result.message.substr(0, 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";
|
"use strict";
|
||||||
|
|
||||||
function message(str) {
|
function showMessage(str) {
|
||||||
|
console.log("message: ", str);
|
||||||
|
|
||||||
const top = $("#top-message");
|
const top = $("#top-message");
|
||||||
|
|
||||||
top.fadeIn(1500).css("display","inline-block");
|
top.fadeIn(1500).css("display","inline-block");
|
||||||
@ -8,7 +10,9 @@ function message(str) {
|
|||||||
top.fadeOut(1500);
|
top.fadeOut(1500);
|
||||||
}
|
}
|
||||||
|
|
||||||
function error(str) {
|
function showError(str) {
|
||||||
|
console.log("error: ", str);
|
||||||
|
|
||||||
const error = $("#error-message");
|
const error = $("#error-message");
|
||||||
|
|
||||||
error.show().css("display","inline-block");
|
error.show().css("display","inline-block");
|
||||||
|
@ -21,7 +21,7 @@ function info(message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function error(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);
|
info(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user