1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-23 18:53:36 +02:00

CLI: improve e2ee decrypt command

This commit is contained in:
Thomas Kriechbaumer 2018-09-10 18:39:19 +02:00
parent 0749e0b675
commit 6e235605ed

View File

@ -47,34 +47,63 @@ class Command extends BaseCommand {
} }
if (args.command === 'decrypt') { if (args.command === 'decrypt') {
if (args.path) { while (true) {
const plainText = await EncryptionService.instance().decryptString(args.path); try {
this.stdout(plainText); if (args.path) {
} else { const plainText = await EncryptionService.instance().decryptString(args.path);
this.stdout(_('Starting decryption... Please wait as it may take several minutes depending on how much there is to decrypt.')); this.stdout(plainText);
return;
} else {
if (process.stdin.isTTY) {
this.stdout(_('Starting decryption... Please wait as it may take several minutes depending on how much there is to decrypt.'));
await DecryptionWorker.instance().start();
this.stdout(_('Completed decryption.'));
return;
} else {
// var repl = require("repl");
// var r = repl.start("node> ");
while (true) { const text = await new Promise((accept, reject) => {
try { var buffer = '';
await DecryptionWorker.instance().start(); process.stdin.setEncoding('utf8');
break; process.stdin.on('data', function(chunk) {
} catch (error) { buffer += chunk;
if (error.code === 'masterKeyNotLoaded') { // process.stdout.write(chunk);
const masterKeyId = error.masterKeyId; });
const password = await this.prompt(_('Enter master password:'), { type: 'string', secure: true }); process.stdin.on('end', function() {
if (!password) { accept(buffer.trim());
this.stdout(_('Operation cancelled')); });
return; });
if (text.length > 0) {
var cipherText = text;
try {
var item = await BaseItem.unserialize(text);
cipherText = item.encryption_cipher_text;
} catch (error) {
// we already got the pure cipher text
}
const plainText = await EncryptionService.instance().decryptString(cipherText);
this.stdout(plainText);
} }
Setting.setObjectKey('encryption.passwordCache', masterKeyId, password); return;
await EncryptionService.instance().loadMasterKeysFromSettings();
continue;
} }
throw error;
} }
} } catch (error) {
if (error.code === 'masterKeyNotLoaded') {
const masterKeyId = error.masterKeyId;
const password = await this.prompt(_('Enter master password:'), { type: 'string', secure: true });
if (!password) {
this.stdout(_('Operation cancelled'));
return;
}
Setting.setObjectKey('encryption.passwordCache', masterKeyId, password);
await EncryptionService.instance().loadMasterKeysFromSettings();
continue;
}
this.stdout(_('Completed decryption.')); throw error;
}
} }
return; return;
@ -186,4 +215,4 @@ class Command extends BaseCommand {
} }
module.exports = Command; module.exports = Command;