2020-03-10 18:31:40 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2019, 2020 , 2021 The Matrix.org Foundation C.I.C.
|
2020-03-10 18:31:40 +03:00
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-03-10 18:31:40 +03:00
|
|
|
*/
|
|
|
|
|
2023-08-08 10:16:04 +03:00
|
|
|
import { User, MatrixClient, RoomMember } from "matrix-js-sdk/src/matrix";
|
2023-06-14 17:35:32 +03:00
|
|
|
import { CrossSigningKey, VerificationRequest } from "matrix-js-sdk/src/crypto-api";
|
2021-05-12 19:08:44 +03:00
|
|
|
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2020-03-10 18:31:40 +03:00
|
|
|
import Modal from "./Modal";
|
2022-01-05 18:14:44 +03:00
|
|
|
import { RightPanelPhases } from "./stores/right-panel/RightPanelStorePhases";
|
2021-05-12 19:08:44 +03:00
|
|
|
import { accessSecretStorage } from "./SecurityManager";
|
2021-05-12 19:04:24 +03:00
|
|
|
import UntrustedDeviceDialog from "./components/views/dialogs/UntrustedDeviceDialog";
|
2022-03-23 02:07:37 +03:00
|
|
|
import { IDevice } from "./components/views/right_panel/UserInfo";
|
2023-04-26 13:23:32 +03:00
|
|
|
import { ManualDeviceKeyVerificationDialog } from "./components/views/dialogs/ManualDeviceKeyVerificationDialog";
|
2022-01-05 18:14:44 +03:00
|
|
|
import RightPanelStore from "./stores/right-panel/RightPanelStore";
|
2022-01-05 19:25:41 +03:00
|
|
|
import { IRightPanelCardState } from "./stores/right-panel/RightPanelStoreIPanelState";
|
2022-07-25 11:17:40 +03:00
|
|
|
import { findDMForUser } from "./utils/dm/findDMForUser";
|
2020-03-10 18:31:40 +03:00
|
|
|
|
2023-06-01 16:43:24 +03:00
|
|
|
async function enable4SIfNeeded(matrixClient: MatrixClient): Promise<boolean> {
|
|
|
|
const crypto = matrixClient.getCrypto();
|
2023-05-15 21:30:43 +03:00
|
|
|
if (!crypto) return false;
|
|
|
|
const usk = await crypto.getCrossSigningKeyId(CrossSigningKey.UserSigning);
|
2020-03-10 20:40:24 +03:00
|
|
|
if (!usk) {
|
2020-03-10 18:31:40 +03:00
|
|
|
await accessSecretStorage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:43:24 +03:00
|
|
|
export async function verifyDevice(matrixClient: MatrixClient, user: User, device: IDevice): Promise<void> {
|
|
|
|
if (matrixClient.isGuest()) {
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: "require_registration" });
|
2021-01-26 14:00:56 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-04-14 18:53:33 +03:00
|
|
|
// if cross-signing is not explicitly disabled, check if it should be enabled first.
|
2024-10-21 14:53:39 +03:00
|
|
|
if (matrixClient.getCrypto()?.getTrustCrossSignedDevices()) {
|
2023-06-01 16:43:24 +03:00
|
|
|
if (!(await enable4SIfNeeded(matrixClient))) {
|
2020-04-14 18:53:33 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-03-10 18:31:40 +03:00
|
|
|
}
|
2020-04-27 22:31:14 +03:00
|
|
|
|
2022-06-14 19:51:51 +03:00
|
|
|
Modal.createDialog(UntrustedDeviceDialog, {
|
2020-04-28 20:35:16 +03:00
|
|
|
user,
|
|
|
|
device,
|
2023-01-12 16:25:14 +03:00
|
|
|
onFinished: async (action): Promise<void> => {
|
2020-04-28 20:35:16 +03:00
|
|
|
if (action === "sas") {
|
2024-10-21 14:53:39 +03:00
|
|
|
const verificationRequestPromise = matrixClient
|
|
|
|
.getCrypto()
|
|
|
|
?.requestDeviceVerification(user.userId, device.deviceId);
|
2022-01-05 19:25:41 +03:00
|
|
|
setRightPanel({ member: user, verificationRequestPromise });
|
2020-04-28 20:35:16 +03:00
|
|
|
} else if (action === "legacy") {
|
2022-06-14 19:51:51 +03:00
|
|
|
Modal.createDialog(ManualDeviceKeyVerificationDialog, {
|
|
|
|
userId: user.userId,
|
|
|
|
device,
|
|
|
|
});
|
2020-04-28 20:35:16 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2020-03-10 18:31:40 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 16:43:24 +03:00
|
|
|
export async function verifyUser(matrixClient: MatrixClient, user: User): Promise<void> {
|
|
|
|
if (matrixClient.isGuest()) {
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: "require_registration" });
|
2021-01-26 14:00:56 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-06-01 16:43:24 +03:00
|
|
|
if (!(await enable4SIfNeeded(matrixClient))) {
|
2020-03-10 18:31:40 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-06-01 16:43:24 +03:00
|
|
|
const existingRequest = pendingVerificationRequestForUser(matrixClient, user);
|
2022-01-05 19:25:41 +03:00
|
|
|
setRightPanel({ member: user, verificationRequest: existingRequest });
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
function setRightPanel(state: IRightPanelCardState): void {
|
2022-01-05 19:25:41 +03:00
|
|
|
if (RightPanelStore.instance.roomPhaseHistory.some((card) => card.phase == RightPanelPhases.RoomSummary)) {
|
|
|
|
RightPanelStore.instance.pushCard({ phase: RightPanelPhases.EncryptionPanel, state });
|
|
|
|
} else {
|
|
|
|
RightPanelStore.instance.setCards([
|
|
|
|
{ phase: RightPanelPhases.RoomSummary },
|
2024-11-25 20:29:32 +03:00
|
|
|
{ phase: RightPanelPhases.MemberInfo, state: { member: state.member } },
|
2022-01-05 19:25:41 +03:00
|
|
|
{ phase: RightPanelPhases.EncryptionPanel, state },
|
|
|
|
]);
|
|
|
|
}
|
2020-03-10 18:31:40 +03:00
|
|
|
}
|
2020-03-26 19:31:31 +03:00
|
|
|
|
2023-06-01 16:43:24 +03:00
|
|
|
export function pendingVerificationRequestForUser(
|
|
|
|
matrixClient: MatrixClient,
|
|
|
|
user: User | RoomMember,
|
|
|
|
): VerificationRequest | undefined {
|
|
|
|
const dmRoom = findDMForUser(matrixClient, user.userId);
|
2020-03-26 19:31:31 +03:00
|
|
|
if (dmRoom) {
|
2023-08-01 11:18:34 +03:00
|
|
|
return matrixClient.getCrypto()!.findVerificationRequestDMInProgress(dmRoom.roomId, user.userId);
|
2020-03-26 19:31:31 +03:00
|
|
|
}
|
|
|
|
}
|