1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/ReactNativeClient/lib/components/shared/encryption-config-shared.js

137 lines
4.0 KiB
JavaScript
Raw Normal View History

const EncryptionService = require('lib/services/EncryptionService');
const { _ } = require('lib/locale');
const BaseItem = require('lib/models/BaseItem.js');
const Setting = require('lib/models/Setting').default;
const MasterKey = require('lib/models/MasterKey.js');
const { reg } = require('lib/registry.js');
const shim = require('lib/shim').default;
2017-12-30 21:57:34 +02:00
const shared = {};
shared.constructor = function(comp, props) {
2017-12-30 21:57:34 +02:00
comp.state = {
passwordChecks: {},
stats: {
encrypted: null,
total: null,
},
passwords: Object.assign({}, props.passwords),
2017-12-30 21:57:34 +02:00
};
comp.isMounted_ = false;
shared.refreshStatsIID_ = null;
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.refreshStats = async function(comp) {
const stats = await BaseItem.encryptedItemsStats();
comp.setState({
stats: stats,
});
};
shared.reencryptData = async function() {
2020-03-14 02:52:28 +02:00
const ok = confirm(_('Please confirm that you would like to re-encrypt your complete database.'));
if (!ok) return;
await BaseItem.forceSyncAll();
reg.waitForSyncFinishedThenSync();
Setting.setValue('encryption.shouldReencrypt', Setting.SHOULD_REENCRYPT_NO);
2020-03-14 02:52:28 +02:00
alert(_('Your data is going to be re-encrypted and synced again.'));
};
shared.dontReencryptData = function() {
Setting.setValue('encryption.shouldReencrypt', Setting.SHOULD_REENCRYPT_NO);
};
shared.upgradeMasterKey = async function(comp, masterKey) {
const passwordCheck = comp.state.passwordChecks[masterKey.id];
if (!passwordCheck) {
alert(_('Please enter your password in the master key list below before upgrading the key.'));
return;
}
try {
const password = comp.state.passwords[masterKey.id];
const newMasterKey = await EncryptionService.instance().upgradeMasterKey(masterKey, password);
await MasterKey.save(newMasterKey);
reg.waitForSyncFinishedThenSync();
alert(_('The master key has been upgraded successfully!'));
} catch (error) {
alert(_('Could not upgrade master key: %s', error.message));
}
};
shared.componentDidMount = async function(comp) {
shared.componentDidUpdate(comp);
2017-12-30 21:57:34 +02:00
shared.refreshStats(comp);
2017-12-30 21:57:34 +02:00
if (shared.refreshStatsIID_) {
shim.clearInterval(shared.refreshStatsIID_);
shared.refreshStatsIID_ = null;
2017-12-30 21:57:34 +02:00
}
shared.refreshStatsIID_ = shim.setInterval(() => {
2017-12-30 21:57:34 +02:00
if (!comp.isMounted_) {
shim.clearInterval(shared.refreshStatsIID_);
shared.refreshStatsIID_ = null;
2017-12-30 21:57:34 +02:00
return;
}
shared.refreshStats(comp);
2017-12-30 21:57:34 +02:00
}, 3000);
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.componentDidUpdate = async function(comp, prevProps = null) {
if (prevProps && comp.props.passwords !== prevProps.passwords) {
comp.setState({ passwords: Object.assign({}, comp.props.passwords) });
}
if (!prevProps || comp.props.masterKeys !== prevProps.masterKeys || comp.props.passwords !== prevProps.passwords) {
comp.checkPasswords();
}
};
shared.componentWillUnmount = function() {
if (shared.refreshStatsIID_) {
shim.clearInterval(shared.refreshStatsIID_);
shared.refreshStatsIID_ = null;
}
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.checkPasswords = async function(comp) {
const passwordChecks = Object.assign({}, comp.state.passwordChecks);
for (let i = 0; i < comp.props.masterKeys.length; i++) {
const mk = comp.props.masterKeys[i];
const password = comp.state.passwords[mk.id];
2017-12-30 21:57:34 +02:00
const ok = password ? await EncryptionService.instance().checkMasterKeyPassword(mk, password) : false;
passwordChecks[mk.id] = ok;
}
comp.setState({ passwordChecks: passwordChecks });
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.decryptedStatText = function(comp) {
const stats = comp.state.stats;
2019-07-29 15:43:53 +02:00
const doneCount = stats.encrypted !== null ? stats.total - stats.encrypted : '-';
const totalCount = stats.total !== null ? stats.total : '-';
return _('Decrypted items: %s / %s', doneCount, totalCount);
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.onSavePasswordClick = function(comp, mk) {
const password = comp.state.passwords[mk.id];
2017-12-30 21:57:34 +02:00
if (!password) {
Setting.deleteObjectValue('encryption.passwordCache', mk.id);
2017-12-30 21:57:34 +02:00
} else {
Setting.setObjectValue('encryption.passwordCache', mk.id, password);
2017-12-30 21:57:34 +02:00
}
comp.checkPasswords();
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.onPasswordChange = function(comp, mk, password) {
const passwords = Object.assign({}, comp.state.passwords);
2017-12-30 21:57:34 +02:00
passwords[mk.id] = password;
comp.setState({ passwords: passwords });
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
2019-07-29 15:43:53 +02:00
module.exports = shared;