2020-10-30 18:31:04 +00:00
|
|
|
import React from 'react';
|
2022-05-20 02:42:55 +00:00
|
|
|
import { StyleSheet, Text, View, TextInput as RNTextInput } from 'react-native';
|
2021-11-17 20:02:57 +00:00
|
|
|
import { StackNavigationOptions } from '@react-navigation/stack';
|
2020-10-30 18:31:04 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import * as List from '../containers/List';
|
|
|
|
import I18n from '../i18n';
|
2021-09-13 20:41:05 +00:00
|
|
|
import log, { events, logEvent } from '../utils/log';
|
2022-05-20 02:42:55 +00:00
|
|
|
import { withTheme } from '../theme';
|
2020-10-30 18:31:04 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2022-05-18 19:17:42 +00:00
|
|
|
import FormTextInput from '../containers/TextInput/FormTextInput';
|
2020-10-30 18:31:04 +00:00
|
|
|
import Button from '../containers/Button';
|
|
|
|
import { getUserSelector } from '../selectors/login';
|
|
|
|
import { PADDING_HORIZONTAL } from '../containers/List/constants';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2020-10-30 18:31:04 +00:00
|
|
|
import { Encryption } from '../lib/encryption';
|
2022-05-20 02:42:55 +00:00
|
|
|
import { logout } from '../actions/login';
|
2020-10-30 18:31:04 +00:00
|
|
|
import { showConfirmationAlert, showErrorAlert } from '../utils/info';
|
|
|
|
import EventEmitter from '../utils/events';
|
|
|
|
import { LISTENER } from '../containers/Toast';
|
|
|
|
import debounce from '../utils/debounce';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
2022-05-20 02:42:55 +00:00
|
|
|
import { IApplicationState, IBaseScreen, IUser } from '../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2022-05-20 02:42:55 +00:00
|
|
|
import { SettingsStackParamList } from '../stacks/types';
|
2020-10-30 18:31:04 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
paddingHorizontal: PADDING_HORIZONTAL
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
fontSize: 14,
|
|
|
|
paddingVertical: 10,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
},
|
|
|
|
changePasswordButton: {
|
|
|
|
marginBottom: 4
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-17 20:02:57 +00:00
|
|
|
interface IE2EEncryptionSecurityViewState {
|
|
|
|
newPassword: string;
|
|
|
|
}
|
|
|
|
|
2022-05-20 02:42:55 +00:00
|
|
|
interface IE2EEncryptionSecurityViewProps extends IBaseScreen<SettingsStackParamList, 'E2EEncryptionSecurityView'> {
|
2022-04-07 16:27:45 +00:00
|
|
|
user: IUser;
|
2021-11-17 20:02:57 +00:00
|
|
|
server: string;
|
|
|
|
encryptionEnabled: boolean;
|
|
|
|
}
|
2020-10-30 18:31:04 +00:00
|
|
|
|
2021-11-17 20:02:57 +00:00
|
|
|
class E2EEncryptionSecurityView extends React.Component<IE2EEncryptionSecurityViewProps, IE2EEncryptionSecurityViewState> {
|
|
|
|
private newPasswordInputRef: any = React.createRef();
|
2020-10-30 18:31:04 +00:00
|
|
|
|
2021-11-17 20:02:57 +00:00
|
|
|
static navigationOptions = (): StackNavigationOptions => ({
|
|
|
|
title: I18n.t('E2E_Encryption')
|
|
|
|
});
|
2020-10-30 18:31:04 +00:00
|
|
|
|
2021-11-17 20:02:57 +00:00
|
|
|
state = { newPassword: '' };
|
|
|
|
|
|
|
|
onChangePasswordText = debounce((text: string) => this.setState({ newPassword: text }), 300);
|
|
|
|
|
2022-05-20 02:42:55 +00:00
|
|
|
setNewPasswordRef = (ref: RNTextInput) => (this.newPasswordInputRef = ref);
|
2020-10-30 18:31:04 +00:00
|
|
|
|
|
|
|
changePassword = () => {
|
|
|
|
const { newPassword } = this.state;
|
|
|
|
if (!newPassword.trim()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
showConfirmationAlert({
|
|
|
|
title: I18n.t('Are_you_sure_question_mark'),
|
|
|
|
message: I18n.t('E2E_encryption_change_password_message'),
|
|
|
|
confirmationText: I18n.t('E2E_encryption_change_password_confirmation'),
|
2021-09-13 20:41:05 +00:00
|
|
|
onPress: async () => {
|
2020-10-30 18:31:04 +00:00
|
|
|
logEvent(events.E2E_SEC_CHANGE_PASSWORD);
|
|
|
|
try {
|
|
|
|
const { server } = this.props;
|
|
|
|
await Encryption.changePassword(server, newPassword);
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('E2E_encryption_change_password_success') });
|
|
|
|
this.newPasswordInputRef?.clear();
|
|
|
|
this.newPasswordInputRef?.blur();
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
showErrorAlert(I18n.t('E2E_encryption_change_password_error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 18:31:04 +00:00
|
|
|
|
|
|
|
resetOwnKey = () => {
|
|
|
|
showConfirmationAlert({
|
|
|
|
title: I18n.t('Are_you_sure_question_mark'),
|
|
|
|
message: I18n.t('E2E_encryption_reset_message'),
|
|
|
|
confirmationText: I18n.t('E2E_encryption_reset_confirmation'),
|
2021-09-13 20:41:05 +00:00
|
|
|
onPress: async () => {
|
2020-10-30 18:31:04 +00:00
|
|
|
logEvent(events.E2E_SEC_RESET_OWN_KEY);
|
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
const res = await Services.e2eResetOwnKey();
|
2021-01-20 17:34:01 +00:00
|
|
|
/**
|
|
|
|
* It might return an empty object when TOTP is enabled,
|
|
|
|
* that's why we're using strict equality to boolean
|
|
|
|
*/
|
|
|
|
if (res === true) {
|
2022-05-20 02:42:55 +00:00
|
|
|
const { dispatch } = this.props;
|
|
|
|
dispatch(logout());
|
2021-01-20 17:34:01 +00:00
|
|
|
}
|
2020-10-30 18:31:04 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
showErrorAlert(I18n.t('E2E_encryption_reset_error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 18:31:04 +00:00
|
|
|
|
|
|
|
renderChangePassword = () => {
|
|
|
|
const { newPassword } = this.state;
|
2021-01-20 17:34:01 +00:00
|
|
|
const { theme, encryptionEnabled } = this.props;
|
|
|
|
if (!encryptionEnabled) {
|
2020-10-30 18:31:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<List.Section>
|
2022-05-20 02:42:55 +00:00
|
|
|
<Text style={[styles.title, { color: themes[theme].headerTitleColor }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{I18n.t('E2E_encryption_change_password_title')}
|
|
|
|
</Text>
|
2022-05-20 02:42:55 +00:00
|
|
|
<Text style={[styles.description, { color: themes[theme].bodyText }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{I18n.t('E2E_encryption_change_password_description')}
|
|
|
|
</Text>
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-10-30 18:31:04 +00:00
|
|
|
inputRef={this.setNewPasswordRef}
|
|
|
|
placeholder={I18n.t('New_Password')}
|
|
|
|
returnKeyType='send'
|
|
|
|
secureTextEntry
|
|
|
|
onSubmitEditing={this.changePassword}
|
|
|
|
testID='e2e-encryption-security-view-password'
|
|
|
|
theme={theme}
|
|
|
|
onChangeText={this.onChangePasswordText}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
onPress={this.changePassword}
|
|
|
|
title={I18n.t('Save_Changes')}
|
|
|
|
theme={theme}
|
|
|
|
disabled={!newPassword.trim()}
|
|
|
|
style={styles.changePasswordButton}
|
|
|
|
testID='e2e-encryption-security-view-change-password'
|
|
|
|
/>
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 18:31:04 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
2022-05-20 02:42:55 +00:00
|
|
|
<SafeAreaView testID='e2e-encryption-security-view' style={{ backgroundColor: themes[theme].backgroundColor }}>
|
2022-03-21 19:10:11 +00:00
|
|
|
<StatusBar />
|
2020-10-30 18:31:04 +00:00
|
|
|
<List.Container>
|
|
|
|
<View style={styles.container}>
|
|
|
|
{this.renderChangePassword()}
|
|
|
|
|
|
|
|
<List.Section>
|
2022-05-20 02:42:55 +00:00
|
|
|
<Text style={[styles.title, { color: themes[theme].headerTitleColor }]}>
|
2022-04-12 16:27:05 +00:00
|
|
|
{I18n.t('E2E_encryption_reset_title')}
|
|
|
|
</Text>
|
2022-05-20 02:42:55 +00:00
|
|
|
<Text style={[styles.description, { color: themes[theme].bodyText }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{I18n.t('E2E_encryption_reset_description')}
|
|
|
|
</Text>
|
2020-10-30 18:31:04 +00:00
|
|
|
<Button
|
|
|
|
onPress={this.resetOwnKey}
|
|
|
|
title={I18n.t('E2E_encryption_reset_button')}
|
|
|
|
theme={theme}
|
|
|
|
type='secondary'
|
2022-05-20 02:42:55 +00:00
|
|
|
backgroundColor={themes[theme].chatComponentBackground}
|
2020-10-30 18:31:04 +00:00
|
|
|
testID='e2e-encryption-security-view-reset-key'
|
|
|
|
/>
|
|
|
|
</List.Section>
|
|
|
|
</View>
|
|
|
|
</List.Container>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 02:42:55 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-10-30 18:31:04 +00:00
|
|
|
server: state.server.server,
|
2021-01-20 17:34:01 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
encryptionEnabled: state.encryption.enabled
|
2020-10-30 18:31:04 +00:00
|
|
|
});
|
|
|
|
|
2022-05-20 02:42:55 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(E2EEncryptionSecurityView));
|