/* Copyright 2018 New Vector Ltd Copyright 2019 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React from 'react'; import sdk from '../../../index'; import MatrixClientPeg from '../../../MatrixClientPeg'; import { _t } from '../../../languageHandler'; import Modal from '../../../Modal'; import SettingsStore from '../../../../lib/settings/SettingsStore'; import { accessSecretStorage } from '../../../CrossSigningManager'; export default class KeyBackupPanel extends React.PureComponent { constructor(props) { super(props); this._unmounted = false; this.state = { loading: true, error: null, backupInfo: null, backupSigStatus: null, backupKeyStored: null, sessionsRemaining: 0, }; } componentWillMount() { this._checkKeyBackupStatus(); MatrixClientPeg.get().on('crypto.keyBackupStatus', this._onKeyBackupStatus); MatrixClientPeg.get().on( 'crypto.keyBackupSessionsRemaining', this._onKeyBackupSessionsRemaining, ); } componentWillUnmount() { this._unmounted = true; if (MatrixClientPeg.get()) { MatrixClientPeg.get().removeListener('crypto.keyBackupStatus', this._onKeyBackupStatus); MatrixClientPeg.get().removeListener( 'crypto.keyBackupSessionsRemaining', this._onKeyBackupSessionsRemaining, ); } } _onKeyBackupSessionsRemaining = (sessionsRemaining) => { this.setState({ sessionsRemaining, }); } _onKeyBackupStatus = () => { // This just loads the current backup status rather than forcing // a re-check otherwise we risk causing infinite loops this._loadBackupStatus(); } async _checkKeyBackupStatus() { try { const {backupInfo, trustInfo} = await MatrixClientPeg.get().checkKeyBackup(); const backupKeyStored = await MatrixClientPeg.get().isKeyBackupKeyStored(); this.setState({ backupInfo, backupSigStatus: trustInfo, backupKeyStored, error: null, loading: false, }); } catch (e) { console.log("Unable to fetch check backup status", e); if (this._unmounted) return; this.setState({ error: e, backupInfo: null, backupSigStatus: null, backupKeyStored: null, loading: false, }); } } async _loadBackupStatus() { this.setState({loading: true}); try { const backupInfo = await MatrixClientPeg.get().getKeyBackupVersion(); const backupSigStatus = await MatrixClientPeg.get().isKeyBackupTrusted(backupInfo); const backupKeyStored = await MatrixClientPeg.get().isKeyBackupKeyStored(); if (this._unmounted) return; this.setState({ error: null, backupInfo, backupSigStatus, backupKeyStored, loading: false, }); } catch (e) { console.log("Unable to fetch key backup status", e); if (this._unmounted) return; this.setState({ error: e, backupInfo: null, backupSigStatus: null, backupKeyStored: null, loading: false, }); } } _startNewBackup = () => { Modal.createTrackedDialogAsync('Key Backup', 'Key Backup', import('../../../async-components/views/dialogs/keybackup/CreateKeyBackupDialog'), { onFinished: () => { this._loadBackupStatus(); }, }, ); } _startNewBackupWithSecureSecretStorage = async () => { const cli = MatrixClientPeg.get(); let info; try { await accessSecretStorage(async () => { info = await cli.prepareKeyBackupVersion( null /* random key */, { secureSecretStorage: true }, ); info = await cli.createKeyBackupVersion(info); }); await MatrixClientPeg.get().scheduleAllGroupSessionsForBackup(); this._loadBackupStatus(); } catch (e) { console.error("Error creating key backup", e); // TODO: If creating a version succeeds, but backup fails, should we // delete the version, disable backup, or do nothing? If we just // disable without deleting, we'll enable on next app reload since // it is trusted. if (info && info.version) { MatrixClientPeg.get().deleteKeyBackupVersion(info.version); } } } _deleteBackup = () => { const QuestionDialog = sdk.getComponent('dialogs.QuestionDialog'); Modal.createTrackedDialog('Delete Backup', '', QuestionDialog, { title: _t('Delete Backup'), description: _t( "Are you sure? You will lose your encrypted messages if your " + "keys are not backed up properly.", ), button: _t('Delete Backup'), danger: true, onFinished: (proceed) => { if (!proceed) return; this.setState({loading: true}); MatrixClientPeg.get().deleteKeyBackupVersion(this.state.backupInfo.version).then(() => { this._loadBackupStatus(); }); }, }); } _restoreBackup = async () => { // Use legacy path if backup key not stored in secret storage if (!this.state.backupKeyStored) { const RestoreKeyBackupDialog = sdk.getComponent('dialogs.keybackup.RestoreKeyBackupDialog'); Modal.createTrackedDialog('Restore Backup', '', RestoreKeyBackupDialog); return; } try { await accessSecretStorage(async () => { await MatrixClientPeg.get().restoreKeyBackupWithSecretStorage( this.state.backupInfo, ); }); } catch (e) { console.log("Error restoring backup", e); } } render() { const Spinner = sdk.getComponent("elements.Spinner"); const AccessibleButton = sdk.getComponent("elements.AccessibleButton"); const encryptedMessageAreEncrypted = _t( "Encrypted messages are secured with end-to-end encryption. " + "Only you and the recipient(s) have the keys to read these messages.", ); if (this.state.error) { return (
{encryptedMessageAreEncrypted}
✅ {_t("This device is backing up your keys. ")}
{encryptedMessageAreEncrypted}
{_t( "This device is not backing up your keys, " + "but you do have an existing backup you can restore from " + "and add to going forward.", {}, {b: sub => {sub}}, )}
{_t( "Connect this device to key backup before signing out to avoid " + "losing any keys that may only be on this device.", )}
⚠️ {_t( "Backup key stored in secret storage, but this feature is not " + "enabled on this device. Please enable cross-signing in Labs to " + "modify key backup state.", )}
; } return{_t( "Your keys are not being backed up from this device.", {}, {b: sub => {sub}}, )}
{encryptedMessageAreEncrypted}
{_t("Back up your keys before signing out to avoid losing them.")}