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