Files
ThreadNet-Web/src/components/views/settings/SecureBackupPanel.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

440 lines
16 KiB
TypeScript
Raw Normal View History

2018-09-13 17:11:46 +01:00
/*
Copyright 2018 New Vector Ltd
2020-01-03 13:33:32 +00:00
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
2018-09-13 17:11:46 +01:00
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.
*/
2023-04-21 10:48:48 +01:00
import React, { ReactNode } from "react";
import { CryptoEvent } from "matrix-js-sdk/src/crypto";
2021-10-27 21:53:09 -05:00
import { logger } from "matrix-js-sdk/src/logger";
import { BackupTrustInfo, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api";
2018-09-13 17:11:46 +01:00
import type CreateKeyBackupDialog from "../../../async-components/views/dialogs/security/CreateKeyBackupDialog";
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../../MatrixClientPeg";
2018-09-13 17:11:46 +01:00
import { _t } from "../../../languageHandler";
import Modal from "../../../Modal";
import { isSecureBackupRequired } from "../../../utils/WellKnownUtils";
2020-09-04 13:55:25 +01:00
import Spinner from "../elements/Spinner";
import AccessibleButton from "../elements/AccessibleButton";
import QuestionDialog from "../dialogs/QuestionDialog";
import RestoreKeyBackupDialog from "../dialogs/security/RestoreKeyBackupDialog";
import { accessSecretStorage } from "../../../SecurityManager";
import { SettingsSubsectionText } from "./shared/SettingsSubsection";
2021-09-21 14:48:20 +02:00
interface IState {
loading: boolean;
error: boolean;
backupKeyStored: boolean | null;
backupKeyCached: boolean | null;
backupKeyWellFormed: boolean | null;
secretStorageKeyInAccount: boolean | null;
secretStorageReady: boolean | null;
/** Information on the current key backup version, as returned by the server.
*
* `null` could mean any of:
* * we haven't yet requested the data from the server.
* * we were unable to reach the server.
* * the server returned key backup version data we didn't understand or was malformed.
* * there is actually no backup on the server.
*/
backupInfo: KeyBackupInfo | null;
/**
* Information on whether the backup in `backupInfo` is correctly signed, and whether we have the right key to
* decrypt it.
*
* `undefined` if `backupInfo` is null, or if crypto is not enabled in the client.
*/
backupTrustInfo: BackupTrustInfo | undefined;
/**
* If key backup is currently enabled, the backup version we are backing up to.
*/
activeBackupVersion: string | null;
/**
* Number of sessions remaining to be backed up. `null` if we have no information on this.
*/
sessionsRemaining: number | null;
2021-09-21 14:48:20 +02:00
}
2018-09-13 17:11:46 +01:00
2021-09-21 14:48:20 +02:00
export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
private unmounted = false;
public constructor(props: {}) {
2018-09-13 17:11:46 +01:00
super(props);
this.state = {
loading: true,
error: false,
2020-09-08 14:10:34 +01:00
backupKeyStored: null,
backupKeyCached: null,
backupKeyWellFormed: null,
secretStorageKeyInAccount: null,
secretStorageReady: null,
2018-09-13 17:11:46 +01:00
backupInfo: null,
backupTrustInfo: undefined,
activeBackupVersion: null,
sessionsRemaining: null,
2018-09-13 17:11:46 +01:00
};
}
2021-09-21 14:48:20 +02:00
public componentDidMount(): void {
this.loadBackupStatus();
MatrixClientPeg.safeGet().on(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
MatrixClientPeg.safeGet().on(CryptoEvent.KeyBackupSessionsRemaining, this.onKeyBackupSessionsRemaining);
2018-09-13 17:11:46 +01:00
}
2021-09-21 14:48:20 +02:00
public componentWillUnmount(): void {
this.unmounted = true;
2018-09-17 16:00:23 +01:00
if (MatrixClientPeg.get()) {
MatrixClientPeg.get()!.removeListener(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
MatrixClientPeg.get()!.removeListener(
CryptoEvent.KeyBackupSessionsRemaining,
2021-09-21 14:48:20 +02:00
this.onKeyBackupSessionsRemaining,
);
2018-09-17 16:00:23 +01:00
}
}
2021-09-21 14:48:20 +02:00
private onKeyBackupSessionsRemaining = (sessionsRemaining: number): void => {
this.setState({
sessionsRemaining,
});
2021-09-21 14:48:20 +02:00
};
2021-09-21 14:48:20 +02:00
private onKeyBackupStatus = (): void => {
// This just loads the current backup status rather than forcing
// a re-check otherwise we risk causing infinite loops
2021-09-21 14:48:20 +02:00
this.loadBackupStatus();
};
2018-09-13 17:11:46 +01:00
2021-09-21 14:48:20 +02:00
private async loadBackupStatus(): Promise<void> {
2020-09-08 14:10:34 +01:00
this.setState({ loading: true });
2021-09-21 14:48:20 +02:00
this.getUpdatedDiagnostics();
2018-09-13 17:11:46 +01:00
try {
const cli = MatrixClientPeg.safeGet();
const backupInfo = await cli.getKeyBackupVersion();
const backupTrustInfo = backupInfo ? await cli.getCrypto()?.isKeyBackupTrusted(backupInfo) : undefined;
const activeBackupVersion = (await cli.getCrypto()?.getActiveSessionBackupVersion()) ?? null;
2021-09-21 14:48:20 +02:00
if (this.unmounted) return;
2018-09-13 17:11:46 +01:00
this.setState({
2020-09-08 14:10:34 +01:00
loading: false,
error: false,
2018-09-13 17:11:46 +01:00
backupInfo,
backupTrustInfo,
activeBackupVersion,
2018-09-13 17:11:46 +01:00
});
} catch (e) {
logger.log("Unable to fetch key backup status", e);
2021-09-21 14:48:20 +02:00
if (this.unmounted) return;
2018-09-13 17:11:46 +01:00
this.setState({
2020-09-08 14:10:34 +01:00
loading: false,
error: true,
backupInfo: null,
backupTrustInfo: undefined,
activeBackupVersion: null,
2018-09-13 17:11:46 +01:00
});
}
}
2021-09-21 14:48:20 +02:00
private async getUpdatedDiagnostics(): Promise<void> {
const cli = MatrixClientPeg.safeGet();
const crypto = cli.getCrypto();
if (!crypto) return;
const secretStorage = cli.secretStorage;
2020-09-08 14:10:34 +01:00
2020-09-16 12:00:49 +01:00
const backupKeyStored = !!(await cli.isKeyBackupKeyStored());
const backupKeyFromCache = await crypto.getSessionBackupPrivateKey();
2020-09-08 14:10:34 +01:00
const backupKeyCached = !!backupKeyFromCache;
const backupKeyWellFormed = backupKeyFromCache instanceof Uint8Array;
const secretStorageKeyInAccount = await secretStorage.hasKey();
const secretStorageReady = await crypto.isSecretStorageReady();
2020-09-08 14:10:34 +01:00
2021-09-21 14:48:20 +02:00
if (this.unmounted) return;
2020-09-08 14:10:34 +01:00
this.setState({
backupKeyStored,
backupKeyCached,
backupKeyWellFormed,
secretStorageKeyInAccount,
secretStorageReady,
});
}
2021-09-21 14:48:20 +02:00
private startNewBackup = (): void => {
2022-06-14 17:51:51 +01:00
Modal.createDialogAsync(
import("../../../async-components/views/dialogs/security/CreateKeyBackupDialog") as unknown as Promise<
typeof CreateKeyBackupDialog
>,
2018-11-23 10:55:18 +00:00
{
2020-01-03 15:34:03 +00:00
onFinished: () => {
2021-09-21 14:48:20 +02:00
this.loadBackupStatus();
2020-01-03 15:34:03 +00:00
},
},
undefined,
2020-01-03 15:34:03 +00:00
/* priority = */ false,
/* static = */ true,
);
2021-09-21 14:48:20 +02:00
};
2021-09-21 14:48:20 +02:00
private deleteBackup = (): void => {
2022-06-14 17:51:51 +01:00
Modal.createDialog(QuestionDialog, {
2018-10-31 19:56:34 +00:00
title: _t("Delete Backup"),
2018-09-13 17:11:46 +01:00
description: _t(
2023-02-27 09:15:27 +00:00
"Are you sure? You will lose your encrypted messages if your keys are not backed up properly.",
2018-09-13 17:11:46 +01:00
),
2019-02-12 16:01:38 +00:00
button: _t("Delete Backup"),
2018-09-13 17:11:46 +01:00
danger: true,
onFinished: (proceed) => {
if (!proceed) return;
2021-06-29 13:11:58 +01:00
this.setState({ loading: true });
const versionToDelete = this.state.backupInfo!.version!;
MatrixClientPeg.safeGet()
.getCrypto()
?.deleteKeyBackupVersion(versionToDelete)
2018-09-13 17:11:46 +01:00
.then(() => {
2021-09-21 14:48:20 +02:00
this.loadBackupStatus();
2018-09-13 17:11:46 +01:00
});
},
});
2021-09-21 14:48:20 +02:00
};
2018-09-13 17:11:46 +01:00
2021-09-21 14:48:20 +02:00
private restoreBackup = async (): Promise<void> => {
Modal.createDialog(RestoreKeyBackupDialog, undefined, undefined, /* priority = */ false, /* static = */ true);
2021-09-21 14:48:20 +02:00
};
2018-09-17 16:00:23 +01:00
2021-09-21 14:48:20 +02:00
private resetSecretStorage = async (): Promise<void> => {
this.setState({ error: false });
try {
await accessSecretStorage(async (): Promise<void> => {}, /* forceReset = */ true);
} catch (e) {
2021-10-15 16:30:53 +02:00
logger.error("Error resetting secret storage", e);
2021-09-21 14:48:20 +02:00
if (this.unmounted) return;
this.setState({ error: true });
}
2021-09-21 14:48:20 +02:00
if (this.unmounted) return;
this.loadBackupStatus();
};
public render(): React.ReactNode {
const {
loading,
error,
2020-09-08 14:10:34 +01:00
backupKeyStored,
backupKeyCached,
backupKeyWellFormed,
secretStorageKeyInAccount,
secretStorageReady,
backupInfo,
backupTrustInfo,
sessionsRemaining,
} = this.state;
2018-09-13 17:11:46 +01:00
2023-04-21 10:48:48 +01:00
let statusDescription: JSX.Element;
let extraDetailsTableRows: JSX.Element | undefined;
let extraDetails: JSX.Element | undefined;
const actions: JSX.Element[] = [];
if (error) {
statusDescription = (
<SettingsSubsectionText className="error">
{_t("Unable to load key backup status")}
</SettingsSubsectionText>
);
} else if (loading) {
statusDescription = <Spinner />;
} else if (backupInfo) {
let restoreButtonCaption = _t("Restore from Backup");
2019-02-12 16:01:38 +00:00
if (this.state.activeBackupVersion !== null) {
statusDescription = (
<SettingsSubsectionText> {_t("This session is backing up your keys.")}</SettingsSubsectionText>
);
2018-09-13 17:11:46 +01:00
} else {
statusDescription = (
<>
<SettingsSubsectionText>
{_t(
"This session is <b>not backing up your keys</b>, but you do have an existing backup you can restore from and add to going forward.",
{},
{ b: (sub) => <b>{sub}</b> },
)}
</SettingsSubsectionText>
<SettingsSubsectionText>
{_t(
"Connect this session to key backup before signing out to avoid losing any keys that may only be on this session.",
)}
</SettingsSubsectionText>
</>
);
2020-01-29 15:48:25 +00:00
restoreButtonCaption = _t("Connect this session to Key Backup");
2018-09-13 17:11:46 +01:00
}
2023-04-21 10:48:48 +01:00
let uploadStatus: ReactNode;
if (sessionsRemaining === null) {
2019-01-09 05:24:15 -06:00
// No upload status to show when backup disabled.
uploadStatus = "";
} else if (sessionsRemaining > 0) {
uploadStatus = (
<div>
{_t("Backing up %(sessionsRemaining)s keys…", { sessionsRemaining })} <br />
2019-01-09 05:24:15 -06:00
</div>
);
} else {
2019-01-09 05:24:15 -06:00
uploadStatus = (
<div>
{_t("All keys backed up")} <br />
2019-01-09 05:24:15 -06:00
</div>
);
}
let trustedLocally: string | undefined;
if (backupTrustInfo?.matchesDecryptionKey) {
trustedLocally = _t("This backup can be restored on this session");
2019-02-07 14:39:47 +00:00
}
2020-09-04 14:09:34 +01:00
extraDetailsTableRows = (
<>
<tr>
<th scope="row">{_t("Latest backup version on server:")}</th>
<td>
{backupInfo.version} ({_t("Algorithm:")} <code>{backupInfo.algorithm}</code>)
</td>
2020-09-04 14:09:34 +01:00
</tr>
<tr>
<th scope="row">{_t("Active backup version:")}</th>
<td>{this.state.activeBackupVersion === null ? _t("None") : this.state.activeBackupVersion}</td>
2020-09-04 14:09:34 +01:00
</tr>
</>
);
extraDetails = (
<>
{uploadStatus}
<div>{trustedLocally}</div>
</>
);
actions.push(
2021-09-21 14:48:20 +02:00
<AccessibleButton key="restore" kind="primary" onClick={this.restoreBackup}>
{restoreButtonCaption}
</AccessibleButton>,
);
if (!isSecureBackupRequired(MatrixClientPeg.safeGet())) {
actions.push(
2021-09-21 14:48:20 +02:00
<AccessibleButton key="delete" kind="danger" onClick={this.deleteBackup}>
{_t("Delete Backup")}
</AccessibleButton>,
);
}
} else {
statusDescription = (
<>
<SettingsSubsectionText>
{_t(
"Your keys are <b>not being backed up from this session</b>.",
{},
{ b: (sub) => <b>{sub}</b> },
)}
</SettingsSubsectionText>
<SettingsSubsectionText>
{_t("Back up your keys before signing out to avoid losing them.")}
</SettingsSubsectionText>
</>
);
actions.push(
2021-09-21 14:48:20 +02:00
<AccessibleButton key="setup" kind="primary" onClick={this.startNewBackup}>
{_t("Set up")}
</AccessibleButton>,
);
}
if (secretStorageKeyInAccount) {
actions.push(
2021-09-21 14:48:20 +02:00
<AccessibleButton key="reset" kind="danger" onClick={this.resetSecretStorage}>
{_t("action|reset")}
</AccessibleButton>,
);
2018-09-13 17:11:46 +01:00
}
2020-09-08 14:10:34 +01:00
let backupKeyWellFormedText = "";
if (backupKeyCached) {
backupKeyWellFormedText = ", ";
if (backupKeyWellFormed) {
backupKeyWellFormedText += _t("well formed");
} else {
backupKeyWellFormedText += _t("unexpected type");
}
}
2023-04-21 10:48:48 +01:00
let actionRow: JSX.Element | undefined;
if (actions.length) {
actionRow = <div className="mx_SecureBackupPanel_buttonRow">{actions}</div>;
}
return (
<>
<SettingsSubsectionText>
{_t(
"Back up your encryption keys with your account data in case you lose access to your sessions. Your keys will be secured with a unique Security Key.",
)}
</SettingsSubsectionText>
{statusDescription}
<details>
<summary>{_t("Advanced")}</summary>
2020-09-04 14:09:34 +01:00
<table className="mx_SecureBackupPanel_statusList">
2023-04-21 10:48:48 +01:00
<tr>
<th scope="row">{_t("Backup key stored:")}</th>
<td>
{backupKeyStored === true
? _t("settings|security|cross_signing_in_4s")
: _t("not stored")}
</td>
2023-04-21 10:48:48 +01:00
</tr>
<tr>
<th scope="row">{_t("Backup key cached:")}</th>
<td>
{backupKeyCached
? _t("settings|security|cross_signing_cached")
: _t("settings|security|cross_signing_not_cached")}
2023-04-21 10:48:48 +01:00
{backupKeyWellFormedText}
</td>
</tr>
<tr>
<th scope="row">{_t("Secret storage public key:")}</th>
<td>
{secretStorageKeyInAccount
? _t("in account data")
: _t("settings|security|cross_signing_not_found")}
</td>
2023-04-21 10:48:48 +01:00
</tr>
<tr>
<th scope="row">{_t("Secret storage:")}</th>
<td>{secretStorageReady ? _t("ready") : _t("not ready")}</td>
</tr>
{extraDetailsTableRows}
2020-09-04 14:09:34 +01:00
</table>
{extraDetails}
</details>
{actionRow}
</>
);
2018-09-13 17:11:46 +01:00
}
}