1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/app-mobile/components/screens/encryption-config.tsx

322 lines
10 KiB
TypeScript
Raw Normal View History

2019-07-29 15:43:53 +02:00
const React = require('react');
2019-07-29 15:58:33 +02:00
const { TextInput, TouchableOpacity, Linking, View, StyleSheet, Text, Button, ScrollView } = require('react-native');
2017-12-30 21:57:34 +02:00
const { connect } = require('react-redux');
2020-11-05 18:58:23 +02:00
const { ScreenHeader } = require('../screen-header.js');
const { BaseScreenComponent } = require('../base-screen.js');
const { themeStyle } = require('../global-style.js');
const DialogBox = require('react-native-dialogbox').default;
const { dialogs } = require('../../utils/dialogs.js');
import EncryptionService from '@joplin/lib/services/EncryptionService';
import { _ } from '@joplin/lib/locale';
import time from '@joplin/lib/time';
import shared from '@joplin/lib/components/shared/encryption-config-shared';
import { MasterKeyEntity } from '@joplin/lib/services/database/types';
import { State } from '@joplin/lib/reducer';
import { SyncInfo } from '@joplin/lib/services/synchronizer/syncInfoUtils';
import { setupAndDisableEncryption, toggleAndSetupEncryption } from '@joplin/lib/services/e2ee/utils';
2021-08-12 20:26:35 +02:00
import MasterKey from '@joplin/lib/models/MasterKey';
interface Props {}
2017-12-30 21:57:34 +02:00
class EncryptionConfigScreenComponent extends BaseScreenComponent<Props> {
static navigationOptions(): any {
2017-12-30 21:57:34 +02:00
return { header: null };
}
constructor(props: Props) {
super(props);
2017-12-30 21:57:34 +02:00
2017-12-31 15:58:50 +02:00
this.state = {
passwordPromptShow: false,
passwordPromptAnswer: '',
passwordPromptConfirmAnswer: '',
2017-12-31 15:58:50 +02:00
};
shared.initialize(this, props);
2017-12-30 21:57:34 +02:00
this.styles_ = {};
}
componentWillUnmount() {
this.isMounted_ = false;
}
async refreshStats() {
return shared.refreshStats(this);
}
componentDidMount() {
this.isMounted_ = true;
shared.componentDidMount(this);
2017-12-30 21:57:34 +02:00
}
componentDidUpdate(prevProps: Props) {
shared.componentDidUpdate(this, prevProps);
2017-12-30 21:57:34 +02:00
}
async checkPasswords() {
return shared.checkPasswords(this);
}
styles() {
2020-09-15 15:01:07 +02:00
const themeId = this.props.themeId;
2017-12-30 21:57:34 +02:00
const theme = themeStyle(themeId);
if (this.styles_[themeId]) return this.styles_[themeId];
this.styles_ = {};
const styles = {
2017-12-30 21:57:34 +02:00
titleText: {
flex: 1,
fontWeight: 'bold',
flexDirection: 'column',
fontSize: theme.fontSize,
paddingTop: 5,
paddingBottom: 5,
2017-12-31 15:58:50 +02:00
marginTop: theme.marginTop,
marginBottom: 5,
color: theme.color,
2017-12-30 21:57:34 +02:00
},
normalText: {
flex: 1,
fontSize: theme.fontSize,
color: theme.color,
2017-12-30 21:57:34 +02:00
},
normalTextInput: {
margin: 10,
color: theme.color,
borderWidth: 1,
borderColor: theme.dividerColor,
},
2017-12-30 21:57:34 +02:00
container: {
flex: 1,
padding: theme.margin,
},
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
this.styles_[themeId] = StyleSheet.create(styles);
return this.styles_[themeId];
}
renderMasterKey(_num: number, mk: MasterKeyEntity) {
2020-09-15 15:01:07 +02:00
const theme = themeStyle(this.props.themeId);
2017-12-30 21:57:34 +02:00
const onSaveClick = () => {
return shared.onSavePasswordClick(this, mk);
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
const onPasswordChange = (text: string) => {
2017-12-30 21:57:34 +02:00
return shared.onPasswordChange(this, mk, text);
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
const password = this.state.passwords[mk.id] ? this.state.passwords[mk.id] : '';
2017-12-30 21:57:34 +02:00
const passwordOk = this.state.passwordChecks[mk.id] === true ? '✔' : '❌';
const inputStyle: any = { flex: 1, marginRight: 10, color: theme.color };
inputStyle.borderBottomWidth = 1;
inputStyle.borderBottomColor = theme.dividerColor;
2019-07-29 15:43:53 +02:00
2017-12-30 21:57:34 +02:00
return (
<View key={mk.id}>
2019-07-29 15:43:53 +02:00
<Text style={this.styles().titleText}>{_('Master Key %s', mk.id.substr(0, 6))}</Text>
2017-12-30 21:57:34 +02:00
<Text style={this.styles().normalText}>{_('Created: %s', time.formatMsToLocal(mk.created_time))}</Text>
2019-07-29 15:43:53 +02:00
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={{ flex: 0, fontSize: theme.fontSize, marginRight: 10, color: theme.color }}>{_('Password:')}</Text>
<TextInput selectionColor={theme.textSelectionColor} keyboardAppearance={theme.keyboardAppearance} secureTextEntry={true} value={password} onChangeText={(text: string) => onPasswordChange(text)} style={inputStyle}></TextInput>
2019-07-29 15:43:53 +02:00
<Text style={{ fontSize: theme.fontSize, marginRight: 10, color: theme.color }}>{passwordOk}</Text>
2017-12-30 21:57:34 +02:00
<Button title={_('Save')} onPress={() => onSaveClick()}></Button>
</View>
</View>
);
}
2017-12-31 15:58:50 +02:00
passwordPromptComponent() {
2020-09-15 15:01:07 +02:00
const theme = themeStyle(this.props.themeId);
const masterKey = MasterKey.latest();
2017-12-31 15:58:50 +02:00
const onEnableClick = async () => {
try {
const password = this.state.passwordPromptAnswer;
if (!password) throw new Error(_('Password cannot be empty'));
const password2 = this.state.passwordPromptConfirmAnswer;
if (!password2) throw new Error(_('Confirm password cannot be empty'));
if (password !== password2) throw new Error(_('Passwords do not match!'));
await toggleAndSetupEncryption(EncryptionService.instance(), true, masterKey, password);
// await generateMasterKeyAndEnableEncryption(EncryptionService.instance(), password);
2017-12-31 15:58:50 +02:00
this.setState({ passwordPromptShow: false });
} catch (error) {
await dialogs.error(this, error.message);
2017-12-31 15:58:50 +02:00
}
2019-07-29 15:43:53 +02:00
};
2017-12-31 15:58:50 +02:00
const messages = shared.enableEncryptionConfirmationMessages(masterKey);
const messageComps = messages.map(msg => {
return <Text key={msg} style={{ fontSize: theme.fontSize, color: theme.color, marginBottom: 10 }}>{msg}</Text>;
});
2017-12-31 15:58:50 +02:00
return (
2019-07-29 15:43:53 +02:00
<View style={{ flex: 1, borderColor: theme.dividerColor, borderWidth: 1, padding: 10, marginTop: 10, marginBottom: 10 }}>
<View>{messageComps}</View>
<Text style={this.styles().normalText}>{_('Password:')}</Text>
2019-07-29 15:43:53 +02:00
<TextInput
selectionColor={theme.textSelectionColor}
keyboardAppearance={theme.keyboardAppearance}
style={this.styles().normalTextInput}
2019-07-29 15:43:53 +02:00
secureTextEntry={true}
value={this.state.passwordPromptAnswer}
onChangeText={(text: string) => {
2019-07-29 15:43:53 +02:00
this.setState({ passwordPromptAnswer: text });
}}
></TextInput>
<Text style={this.styles().normalText}>{_('Confirm password:')}</Text>
<TextInput
selectionColor={theme.textSelectionColor}
keyboardAppearance={theme.keyboardAppearance}
style={this.styles().normalTextInput}
secureTextEntry={true}
value={this.state.passwordPromptConfirmAnswer}
onChangeText={(text: string) => {
this.setState({ passwordPromptConfirmAnswer: text });
}}
></TextInput>
2019-07-29 15:43:53 +02:00
<View style={{ flexDirection: 'row' }}>
<View style={{ flex: 1, marginRight: 10 }}>
<Button
title={_('Enable')}
onPress={() => {
void onEnableClick();
2019-07-29 15:43:53 +02:00
}}
></Button>
2017-12-31 15:58:50 +02:00
</View>
2019-07-29 15:43:53 +02:00
<View style={{ flex: 1 }}>
<Button
title={_('Cancel')}
onPress={() => {
this.setState({ passwordPromptShow: false });
}}
></Button>
2017-12-31 15:58:50 +02:00
</View>
</View>
</View>
);
}
2017-12-30 21:57:34 +02:00
render() {
2020-09-15 15:01:07 +02:00
const theme = themeStyle(this.props.themeId);
const masterKeys = this.props.masterKeys;
2017-12-30 21:57:34 +02:00
const decryptedItemsInfo = this.props.encryptionEnabled ? <Text style={this.styles().normalText}>{shared.decryptedStatText(this)}</Text> : null;
const mkComps = [];
const nonExistingMasterKeyIds = this.props.notLoadedMasterKeys.slice();
2017-12-30 21:57:34 +02:00
for (let i = 0; i < masterKeys.length; i++) {
const mk = masterKeys[i];
2019-07-29 15:43:53 +02:00
mkComps.push(this.renderMasterKey(i + 1, mk));
const idx = nonExistingMasterKeyIds.indexOf(mk.id);
if (idx >= 0) nonExistingMasterKeyIds.splice(idx, 1);
2017-12-30 21:57:34 +02:00
}
2017-12-31 15:58:50 +02:00
const onToggleButtonClick = async () => {
if (this.props.encryptionEnabled) {
const ok = await dialogs.confirm(this, _('Disabling encryption means *all* your notes and attachments are going to be re-synchronised and sent unencrypted to the sync target. Do you wish to continue?'));
2017-12-31 15:58:50 +02:00
if (!ok) return;
try {
await setupAndDisableEncryption(EncryptionService.instance());
2017-12-31 15:58:50 +02:00
} catch (error) {
await dialogs.error(this, error.message);
2017-12-31 15:58:50 +02:00
}
} else {
this.setState({
passwordPromptShow: true,
passwordPromptAnswer: '',
passwordPromptConfirmAnswer: '',
2017-12-31 15:58:50 +02:00
});
return;
}
};
let nonExistingMasterKeySection = null;
if (nonExistingMasterKeyIds.length) {
const rows = [];
for (let i = 0; i < nonExistingMasterKeyIds.length; i++) {
const id = nonExistingMasterKeyIds[i];
2019-07-29 15:43:53 +02:00
rows.push(
<Text style={this.styles().normalText} key={id}>
{id}
</Text>
);
}
nonExistingMasterKeySection = (
<View>
<Text style={this.styles().titleText}>{_('Missing Master Keys')}</Text>
<Text style={this.styles().normalText}>{_('The master keys with these IDs are used to encrypt some of your items, however the application does not currently have access to them. It is likely they will eventually be downloaded via synchronisation.')}</Text>
2019-07-29 15:43:53 +02:00
<View style={{ marginTop: 10 }}>{rows}</View>
</View>
);
}
2017-12-31 15:58:50 +02:00
const passwordPromptComp = this.state.passwordPromptShow ? this.passwordPromptComponent() : null;
2019-07-29 15:43:53 +02:00
const toggleButton = !this.state.passwordPromptShow ? (
<View style={{ marginTop: 10 }}>
<Button title={this.props.encryptionEnabled ? _('Disable encryption') : _('Enable encryption')} onPress={() => onToggleButtonClick()}></Button>
</View>
) : null;
2017-12-31 15:58:50 +02:00
2017-12-30 21:57:34 +02:00
return (
2020-09-15 15:01:07 +02:00
<View style={this.rootStyle(this.props.themeId).root}>
2019-07-29 15:43:53 +02:00
<ScreenHeader title={_('Encryption Config')} />
2017-12-30 21:57:34 +02:00
<ScrollView style={this.styles().container}>
2019-07-29 15:43:53 +02:00
{
<View style={{ backgroundColor: theme.warningBackgroundColor, paddingTop: 5, paddingBottom: 5, paddingLeft: 10, paddingRight: 10 }}>
<Text>{_('For more information about End-To-End Encryption (E2EE) and advice on how to enable it please check the documentation:')}</Text>
2019-07-29 15:43:53 +02:00
<TouchableOpacity
onPress={() => {
Linking.openURL('https://joplinapp.org/e2ee/');
}}
>
<Text>https://joplinapp.org/e2ee/</Text>
</TouchableOpacity>
</View>
}
2017-12-30 21:57:34 +02:00
<Text style={this.styles().titleText}>{_('Status')}</Text>
<Text style={this.styles().normalText}>{_('Encryption is: %s', this.props.encryptionEnabled ? _('Enabled') : _('Disabled'))}</Text>
{decryptedItemsInfo}
2017-12-31 15:58:50 +02:00
{toggleButton}
{passwordPromptComp}
2017-12-30 21:57:34 +02:00
{mkComps}
{nonExistingMasterKeySection}
2019-07-29 15:43:53 +02:00
<View style={{ flex: 1, height: 20 }}></View>
2017-12-30 21:57:34 +02:00
</ScrollView>
<DialogBox
ref={(dialogbox: any) => {
this.dialogbox = dialogbox;
}}
/>
2017-12-30 21:57:34 +02:00
</View>
);
}
}
const EncryptionConfigScreen = connect((state: State) => {
const syncInfo = new SyncInfo(state.settings['syncInfoCache']);
2019-07-29 15:43:53 +02:00
return {
2020-09-15 15:01:07 +02:00
themeId: state.settings.theme,
masterKeys: syncInfo.masterKeys,
2019-07-29 15:43:53 +02:00
passwords: state.settings['encryption.passwordCache'],
encryptionEnabled: syncInfo.e2ee,
activeMasterKeyId: syncInfo.activeMasterKeyId,
2019-07-29 15:43:53 +02:00
notLoadedMasterKeys: state.notLoadedMasterKeys,
};
})(EncryptionConfigScreenComponent);
export default EncryptionConfigScreen;