1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

All: Handle master key in backend

This commit is contained in:
Laurent Cozic 2017-12-12 21:58:57 +00:00
parent 9bce52a92a
commit f6fbf3ba0f
6 changed files with 23 additions and 6 deletions

View File

@ -1,10 +1,9 @@
const { BaseCommand } = require('./base-command.js');
const { app } = require('./app.js');
const { _ } = require('lib/locale.js');
const { Folder } = require('lib/models/folder.js');
const { importEnex } = require('lib/import-enex');
const { filename, basename } = require('lib/path-utils.js');
const { cliUtils } = require('./cli-utils.js');
const EncryptionService = require('lib/services/EncryptionService');
const MasterKey = require('lib/models/MasterKey');
const { Setting } = require('lib/models/setting.js');
class Command extends BaseCommand {
@ -22,7 +21,17 @@ class Command extends BaseCommand {
if (args.command === 'init') {
const password = await this.prompt(_('Enter master password:'), { type: 'string', secure: true });
this.logger().info(password);
if (!password) {
this.stdout(_('Operation cancelled'));
return;
}
const service = new EncryptionService();
const masterKey = await service.generateMasterKey(password);
await MasterKey.save(masterKey);
Setting.setValue('encryption.enabled', true);
}
}

View File

@ -401,6 +401,7 @@ BaseModel.TYPE_TAG = 5;
BaseModel.TYPE_NOTE_TAG = 6;
BaseModel.TYPE_SEARCH = 7;
BaseModel.TYPE_ALARM = 8;
BaseModel.TYPE_MASTER_KEY = 9;
BaseModel.db_ = null;
BaseModel.dispatch = function(o) {};

View File

@ -153,6 +153,7 @@ class Database {
if (s == 'string') return 2;
}
if (type == 'fieldType') {
if (s) s = s.toUpperCase();
if (s == 'INTEGER') s = 'INT';
if (!(('TYPE_' + s) in this)) throw new Error('Unkonwn fieldType: ' + s);
return this['TYPE_' + s];

View File

@ -457,6 +457,7 @@ BaseItem.syncItemDefinitions_ = [
{ type: BaseModel.TYPE_RESOURCE, className: 'Resource' },
{ type: BaseModel.TYPE_TAG, className: 'Tag' },
{ type: BaseModel.TYPE_NOTE_TAG, className: 'NoteTag' },
{ type: BaseModel.TYPE_MASTER_KEY, className: 'MasterKey' },
];
module.exports = { BaseItem };

View File

@ -60,6 +60,7 @@ class Setting extends BaseModel {
// })},
'uncompletedTodosOnTop': { value: true, type: Setting.TYPE_BOOL, public: true, label: () => _('Show uncompleted todos on top of the lists') },
'trackLocation': { value: true, type: Setting.TYPE_BOOL, public: true, label: () => _('Save geo-location with notes') },
'encryption.enabled': { value: false, type: Setting.TYPE_BOOL, public: false },
'sync.interval': { value: 300, type: Setting.TYPE_INT, isEnum: true, public: true, label: () => _('Synchronisation interval'), options: () => {
return {
0: _('Disabled'),

View File

@ -1,6 +1,5 @@
const { padLeft } = require('lib/string-utils.js');
const { shim } = require('lib/shim.js');
const sjcl = shim.sjclModule;
class EncryptionService {
@ -10,6 +9,7 @@ class EncryptionService {
}
sha256(string) {
const sjcl = shim.sjclModule;
const bitArray = sjcl.hash.sha256.hash(string);
return sjcl.codec.hex.fromBits(bitArray);
}
@ -39,6 +39,8 @@ class EncryptionService {
}
async encrypt(method, key, plainText) {
const sjcl = shim.sjclModule;
if (method === EncryptionService.METHOD_SJCL) {
// Good demo to understand each parameter: https://bitwiseshiftleft.github.io/sjcl/demo/
return sjcl.json.encrypt(key, plainText, {
@ -68,6 +70,8 @@ class EncryptionService {
}
async decrypt(method, key, cipherText) {
const sjcl = shim.sjclModule;
if (method === EncryptionService.METHOD_SJCL || method === EncryptionService.METHOD_SJCL_2) {
return sjcl.json.decrypt(key, cipherText);
}