1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Merge pull request #784 from Kriechi/cli-decrypt

CLI: improve e2ee decrypt command
This commit is contained in:
Laurent Cozic 2018-09-16 20:01:09 +01:00 committed by GitHub
commit 46a3b020a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,34 +47,63 @@ class Command extends BaseCommand {
}
if (args.command === 'decrypt') {
if (args.path) {
const plainText = await EncryptionService.instance().decryptString(args.path);
this.stdout(plainText);
} else {
this.stdout(_('Starting decryption... Please wait as it may take several minutes depending on how much there is to decrypt.'));
while (true) {
try {
if (args.path) {
const plainText = await EncryptionService.instance().decryptString(args.path);
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) {
try {
await DecryptionWorker.instance().start();
break;
} 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;
const text = await new Promise((accept, reject) => {
var buffer = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
buffer += chunk;
// process.stdout.write(chunk);
});
process.stdin.on('end', function() {
accept(buffer.trim());
});
});
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);
await EncryptionService.instance().loadMasterKeysFromSettings();
continue;
return;
}
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;
@ -186,4 +215,4 @@ class Command extends BaseCommand {
}
module.exports = Command;
module.exports = Command;