element-web/src/components/views/right_panel/EncryptionPanel.tsx

159 lines
6.4 KiB
TypeScript
Raw Normal View History

2019-12-10 19:53:51 +03:00
/*
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
2019-12-10 19:53:51 +03: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.
*/
2021-06-18 18:21:46 +03:00
import React, { useCallback, useEffect, useState } from "react";
import { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { User } from "matrix-js-sdk/src/models/user";
import { PHASE_REQUESTED, PHASE_UNSENT } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
2019-12-16 20:16:22 +03:00
import EncryptionInfo from "./EncryptionInfo";
import VerificationPanel from "./VerificationPanel";
2021-06-18 18:21:46 +03:00
import { MatrixClientPeg } from "../../../MatrixClientPeg";
import { ensureDMExists } from "../../../createRoom";
import { useEventEmitter } from "../../../hooks/useEventEmitter";
2020-01-28 14:13:09 +03:00
import Modal from "../../../Modal";
import * as sdk from "../../../index";
2021-06-18 18:21:46 +03:00
import { _t } from "../../../languageHandler";
import dis from "../../../dispatcher/dispatcher";
2021-06-18 18:21:46 +03:00
import { Action } from "../../../dispatcher/actions";
import { RightPanelPhases } from "../../../stores/RightPanelStorePhases";
2020-01-28 14:13:09 +03:00
// cancellation codes which constitute a key mismatch
const MISMATCHES = ["m.key_mismatch", "m.user_error", "m.mismatched_sas"];
2020-07-18 20:39:11 +03:00
interface IProps {
2021-06-18 18:21:46 +03:00
member: RoomMember | User;
2020-07-18 20:39:11 +03:00
onClose: () => void;
verificationRequest: VerificationRequest;
verificationRequestPromise?: Promise<VerificationRequest>;
2020-07-18 20:39:11 +03:00
layout: string;
isRoomEncrypted: boolean;
}
const EncryptionPanel: React.FC<IProps> = (props: IProps) => {
const {
verificationRequest,
verificationRequestPromise,
member,
onClose,
layout,
isRoomEncrypted,
} = props;
const [request, setRequest] = useState(verificationRequest);
2020-02-27 14:35:34 +03:00
// state to show a spinner immediately after clicking "start verification",
// before we have a request
const [isRequesting, setRequesting] = useState(false);
2020-02-24 15:39:20 +03:00
const [phase, setPhase] = useState(request && request.phase);
useEffect(() => {
setRequest(verificationRequest);
2020-02-24 15:39:20 +03:00
if (verificationRequest) {
setRequesting(false);
2020-02-24 15:39:20 +03:00
setPhase(verificationRequest.phase);
}
}, [verificationRequest]);
useEffect(() => {
async function awaitPromise() {
setRequesting(true);
const requestFromPromise = await verificationRequestPromise;
setRequesting(false);
setRequest(requestFromPromise);
setPhase(requestFromPromise.phase);
}
if (verificationRequestPromise) {
awaitPromise();
}
}, [verificationRequestPromise]);
const changeHandler = useCallback(() => {
2020-01-28 14:13:09 +03:00
// handle transitions -> cancelled for mismatches which fire a modal instead of showing a card
if (request && request.cancelled && MISMATCHES.includes(request.cancellationCode)) {
// FIXME: Using an import will result in test failures
2020-01-28 14:13:09 +03:00
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
Modal.createTrackedDialog("Verification failed", "insecure", ErrorDialog, {
headerImage: require("../../../../res/img/e2e/warning.svg"),
title: _t("Your messages are not secure"),
description: <div>
{_t("One of the following may be compromised:")}
<ul>
<li>{_t("Your homeserver")}</li>
<li>{_t("The homeserver the user youre verifying is connected to")}</li>
<li>{_t("Yours, or the other users internet connection")}</li>
<li>{_t("Yours, or the other users session")}</li>
2020-01-28 14:13:09 +03:00
</ul>
</div>,
onFinished: onClose,
});
return; // don't update phase here as we will be transitioning away from this view shortly
}
if (request) {
setPhase(request.phase);
}
}, [onClose, request]);
useEventEmitter(request, "change", changeHandler);
const onStartVerification = useCallback(async () => {
setRequesting(true);
const cli = MatrixClientPeg.get();
const roomId = await ensureDMExists(cli, member.userId);
const verificationRequest_ = await cli.requestVerificationDM(member.userId, roomId);
setRequest(verificationRequest_);
setPhase(verificationRequest_.phase);
// Notify the RightPanelStore about this
dis.dispatch({
action: Action.SetRightPanelPhase,
phase: RightPanelPhases.EncryptionPanel,
refireParams: { member, verificationRequest: verificationRequest_ },
});
}, [member]);
const requested =
(!request && isRequesting) ||
(request && (phase === PHASE_REQUESTED || phase === PHASE_UNSENT || phase === undefined));
const isSelfVerification = request ?
request.isSelfVerification :
member.userId === MatrixClientPeg.get().getUserId();
2020-01-28 14:13:09 +03:00
if (!request || requested) {
const initiatedByMe = (!request && isRequesting) || (request && request.initiatedByMe);
return (
<EncryptionInfo
isRoomEncrypted={isRoomEncrypted}
onStartVerification={onStartVerification}
member={member}
isSelfVerification={isSelfVerification}
waitingForOtherParty={requested && initiatedByMe}
waitingForNetwork={requested && !initiatedByMe}
inDialog={layout === "dialog"} />
);
} else {
return (
2020-01-28 14:13:09 +03:00
<VerificationPanel
isRoomEncrypted={isRoomEncrypted}
2020-01-31 18:04:44 +03:00
layout={layout}
2020-01-28 14:13:09 +03:00
onClose={onClose}
member={member}
request={request}
key={request.channel.transactionId}
inDialog={layout === "dialog"}
phase={phase}
/>
);
}
};
2019-12-16 20:16:22 +03:00
export default EncryptionPanel;