Files
ThreadNet-Web/src/components/structures/auth/SetupEncryptionBody.tsx
T

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

261 lines
10 KiB
TypeScript
Raw Normal View History

/*
Copyright 2024, 2025 New Vector Ltd.
2024-09-09 14:57:16 +01:00
Copyright 2020, 2021 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
*/
import React, { type JSX } from "react";
2025-02-05 13:25:06 +00:00
import { type KeyBackupInfo, type VerificationRequest } from "matrix-js-sdk/src/crypto-api";
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger";
2025-09-12 14:37:14 -04:00
import DevicesIcon from "@vector-im/compound-design-tokens/assets/web/icons/devices";
import LockIcon from "@vector-im/compound-design-tokens/assets/web/icons/lock-solid";
import { Button } from "@vector-im/compound-web";
2021-10-22 17:23:32 -05:00
2020-06-19 17:17:04 +01:00
import { _t } from "../../../languageHandler";
import { MatrixClientPeg } from "../../../MatrixClientPeg";
import Modal from "../../../Modal";
import VerificationRequestDialog from "../../views/dialogs/VerificationRequestDialog";
2021-06-18 14:05:12 +01:00
import { SetupEncryptionStore, Phase } from "../../../stores/SetupEncryptionStore";
2021-07-01 15:11:18 +01:00
import EncryptionPanel from "../../views/right_panel/EncryptionPanel";
2025-09-12 14:37:14 -04:00
import AccessibleButton from "../../views/elements/AccessibleButton";
2021-07-01 13:49:58 +01:00
import Spinner from "../../views/elements/Spinner";
import { ResetIdentityDialog } from "../../views/dialogs/ResetIdentityDialog";
2025-09-12 14:37:14 -04:00
import { EncryptionCard } from "../../views/settings/encryption/EncryptionCard";
import { EncryptionCardButtons } from "../../views/settings/encryption/EncryptionCardButtons";
import { EncryptionCardEmphasisedContent } from "../../views/settings/encryption/EncryptionCardEmphasisedContent";
import ExternalLink from "../../views/elements/ExternalLink";
import dispatcher from "../../../dispatcher/dispatcher";
2020-06-23 15:24:02 +01:00
2021-07-01 13:45:33 +01:00
interface IProps {
onFinished: () => void;
2025-09-12 14:37:14 -04:00
/**
* Offer the user an option to log out, instead of setting up encryption.
*
* This is used when this component is shown when the user is initially
* prompted to set up encryption, before the user is shown the main chat
* interface.
*
* Defaults to `false` if omitted.
*/
allowLogout?: boolean;
2021-07-01 13:45:33 +01:00
}
2021-07-01 13:45:33 +01:00
interface IState {
phase?: Phase;
verificationRequest: VerificationRequest | null;
backupInfo: KeyBackupInfo | null;
2021-07-01 13:45:33 +01:00
}
2025-09-12 14:37:14 -04:00
/**
* Component to set up encryption by verifying the current device.
*/
2021-07-01 13:45:33 +01:00
export default class SetupEncryptionBody extends React.Component<IProps, IState> {
2023-02-13 11:39:16 +00:00
public constructor(props: IProps) {
2021-07-01 13:45:33 +01:00
super(props);
const store = SetupEncryptionStore.sharedInstance();
store.start();
this.state = {
phase: store.phase,
// this serves dual purpose as the object for the request logic and
2020-03-25 13:40:09 +01:00
// the presence of it indicating that we're in 'verify mode'.
// Because of the latter, it lives in the state.
verificationRequest: store.verificationRequest,
backupInfo: store.backupInfo,
};
}
public componentDidMount(): void {
const store = SetupEncryptionStore.sharedInstance();
store.on("update", this.onStoreUpdate);
}
private onStoreUpdate = (): void => {
const store = SetupEncryptionStore.sharedInstance();
2021-06-18 14:05:12 +01:00
if (store.phase === Phase.Finished) {
this.props.onFinished();
return;
}
this.setState({
phase: store.phase,
verificationRequest: store.verificationRequest,
backupInfo: store.backupInfo,
});
};
public componentWillUnmount(): void {
const store = SetupEncryptionStore.sharedInstance();
2021-07-01 13:49:58 +01:00
store.off("update", this.onStoreUpdate);
store.stop();
}
private onUsePassphraseClick = async (): Promise<void> => {
const store = SetupEncryptionStore.sharedInstance();
2020-06-18 09:35:11 +01:00
store.usePassPhrase();
2021-07-01 15:11:18 +01:00
};
private onVerifyClick = (): void => {
const cli = MatrixClientPeg.safeGet();
const userId = cli.getSafeUserId();
const requestPromise = cli.getCrypto()!.requestOwnUserVerification();
// We need to call onFinished now to close this dialog, and
// again later to signal that the verification is complete.
this.props.onFinished();
const { finished: verificationFinished } = Modal.createDialog(VerificationRequestDialog, {
verificationRequestPromise: requestPromise,
member: cli.getUser(userId) ?? undefined,
});
verificationFinished.then(async () => {
const request = await requestPromise;
request.cancel();
this.props.onFinished();
});
2021-07-01 15:11:18 +01:00
};
private onSkipConfirmClick = (): void => {
const store = SetupEncryptionStore.sharedInstance();
store.skipConfirm();
2021-07-01 15:11:18 +01:00
};
private onSkipBackClick = (): void => {
const store = SetupEncryptionStore.sharedInstance();
store.returnAfterSkip();
2021-07-01 15:11:18 +01:00
};
2025-09-12 14:37:14 -04:00
private onCantConfirmClick = (): void => {
const store = SetupEncryptionStore.sharedInstance();
Modal.createDialog(ResetIdentityDialog, {
onReset: () => {
// The user completed the reset process - close this dialog
this.props.onFinished();
const store = SetupEncryptionStore.sharedInstance();
store.done();
},
2025-09-12 14:37:14 -04:00
variant: store.lostKeys() ? "no_verification_method" : "confirm",
});
};
2025-09-12 14:37:14 -04:00
private onSignOutClick = (): void => {
dispatcher.dispatch({ action: "logout" });
};
private onDoneClick = (): void => {
const store = SetupEncryptionStore.sharedInstance();
store.done();
2021-07-01 15:11:18 +01:00
};
private onEncryptionPanelClose = (): void => {
this.props.onFinished();
2021-07-01 15:11:18 +01:00
};
2021-07-01 13:45:33 +01:00
public render(): React.ReactNode {
const cli = MatrixClientPeg.safeGet();
2025-09-12 14:37:14 -04:00
const { phase } = this.state;
if (this.state.verificationRequest && cli.getUser(this.state.verificationRequest.otherUserId)) {
return (
<EncryptionPanel
layout="dialog"
verificationRequest={this.state.verificationRequest}
2021-07-01 13:45:33 +01:00
onClose={this.onEncryptionPanelClose}
member={cli.getUser(this.state.verificationRequest.otherUserId)!}
2021-07-01 13:45:33 +01:00
isRoomEncrypted={false}
/>
);
2021-06-18 14:05:12 +01:00
} else if (phase === Phase.Intro) {
2025-09-12 14:37:14 -04:00
const store = SetupEncryptionStore.sharedInstance();
2025-09-12 14:37:14 -04:00
let verifyButton;
if (store.hasDevicesToVerifyAgainst) {
verifyButton = (
<Button kind="primary" onClick={this.onVerifyClick}>
<DevicesIcon /> {_t("encryption|verification|use_another_device")}
</Button>
);
}
2025-09-12 14:37:14 -04:00
let useRecoveryKeyButton;
if (store.keyInfo) {
useRecoveryKeyButton = (
<Button kind="primary" onClick={this.onUsePassphraseClick}>
{_t("encryption|verification|use_recovery_key")}
</Button>
);
}
let signOutButton;
if (this.props.allowLogout) {
signOutButton = (
<Button kind="tertiary" onClick={this.onSignOutClick}>
{_t("action|sign_out")}
</Button>
);
}
return (
<EncryptionCard
title={_t("encryption|verification|confirm_identity_title")}
Icon={LockIcon}
className="mx_EncryptionCard_noBorder mx_SetupEncryptionBody"
>
<EncryptionCardEmphasisedContent>
<span>{_t("encryption|verification|confirm_identity_description")}</span>
<span>
<ExternalLink href="https://element.io/help#encryption-device-verification">
{_t("action|learn_more")}
</ExternalLink>
</span>
</EncryptionCardEmphasisedContent>
<EncryptionCardButtons>
{verifyButton}
{useRecoveryKeyButton}
<Button kind="secondary" onClick={this.onCantConfirmClick}>
{_t("encryption|verification|cant_confirm")}
</Button>
{signOutButton}
</EncryptionCardButtons>
</EncryptionCard>
);
2021-06-18 14:05:12 +01:00
} else if (phase === Phase.Done) {
let message: JSX.Element;
if (this.state.backupInfo) {
2023-09-28 12:51:30 +05:30
message = <p>{_t("encryption|verification|verification_success_with_backup")}</p>;
} else {
2023-09-28 12:51:30 +05:30
message = <p>{_t("encryption|verification|verification_success_without_backup")}</p>;
}
return (
<div>
<div className="mx_CompleteSecurity_heroIcon mx_E2EIcon_verified" />
{message}
<div className="mx_CompleteSecurity_actionRow">
<AccessibleButton kind="primary" onClick={this.onDoneClick}>
{_t("action|done")}
</AccessibleButton>
</div>
</div>
);
2021-06-18 14:05:12 +01:00
} else if (phase === Phase.ConfirmSkip) {
return (
<div>
2023-09-28 12:51:30 +05:30
<p>{_t("encryption|verification|verification_skip_warning")}</p>
<div className="mx_CompleteSecurity_actionRow">
<AccessibleButton kind="danger_outline" onClick={this.onSkipConfirmClick}>
2023-09-28 12:51:30 +05:30
{_t("encryption|verification|verify_later")}
</AccessibleButton>
<AccessibleButton kind="primary" onClick={this.onSkipBackClick}>
{_t("action|go_back")}
</AccessibleButton>
</div>
</div>
);
2021-06-18 14:05:12 +01:00
} else if (phase === Phase.Busy || phase === Phase.Loading) {
return <Spinner />;
} else {
logger.log(`SetupEncryptionBody: Unknown phase ${phase}`);
}
}
}