2020-03-10 15:31:40 +00:00
|
|
|
/*
|
2021-05-12 16:08:44 +00:00
|
|
|
Copyright 2019, 2020, 2021 The Matrix.org Foundation C.I.C.
|
2020-03-10 15:31:40 +00: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-05-12 16:08:44 +00:00
|
|
|
import { User } from "matrix-js-sdk/src/models/user";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { verificationMethods as VerificationMethods } from "matrix-js-sdk/src/crypto";
|
2022-01-05 15:14:44 +00:00
|
|
|
import { RoomMember } from "matrix-js-sdk/src/matrix";
|
2023-01-12 13:25:14 +00:00
|
|
|
import { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
|
2021-05-12 16:08:44 +00:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
2020-05-14 02:41:41 +00:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2020-03-10 15:31:40 +00:00
|
|
|
import Modal from "./Modal";
|
2022-01-05 15:14:44 +00:00
|
|
|
import { RightPanelPhases } from "./stores/right-panel/RightPanelStorePhases";
|
2021-05-12 16:08:44 +00:00
|
|
|
import { accessSecretStorage } from "./SecurityManager";
|
2021-05-12 16:04:24 +00:00
|
|
|
import UntrustedDeviceDialog from "./components/views/dialogs/UntrustedDeviceDialog";
|
2022-03-22 23:07:37 +00:00
|
|
|
import { IDevice } from "./components/views/right_panel/UserInfo";
|
2023-04-26 10:23:32 +00:00
|
|
|
import { ManualDeviceKeyVerificationDialog } from "./components/views/dialogs/ManualDeviceKeyVerificationDialog";
|
2022-01-05 15:14:44 +00:00
|
|
|
import RightPanelStore from "./stores/right-panel/RightPanelStore";
|
2022-01-05 16:25:41 +00:00
|
|
|
import { IRightPanelCardState } from "./stores/right-panel/RightPanelStoreIPanelState";
|
2022-07-25 08:17:40 +00:00
|
|
|
import { findDMForUser } from "./utils/dm/findDMForUser";
|
2020-03-10 15:31:40 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
async function enable4SIfNeeded(): Promise<boolean> {
|
2020-03-10 15:31:40 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
2020-05-27 09:28:25 +00:00
|
|
|
if (!cli.isCryptoEnabled()) {
|
2020-03-10 15:31:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-10 17:40:24 +00:00
|
|
|
const usk = cli.getCrossSigningId("user_signing");
|
|
|
|
if (!usk) {
|
2020-03-10 15:31:40 +00:00
|
|
|
await accessSecretStorage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export async function verifyDevice(user: User, device: IDevice): Promise<void> {
|
2020-04-14 15:53:33 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
2021-01-26 11:00:56 +00:00
|
|
|
if (cli.isGuest()) {
|
2021-06-29 12:11:58 +00:00
|
|
|
dis.dispatch({ action: "require_registration" });
|
2021-01-26 11:00:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-14 15:53:33 +00:00
|
|
|
// if cross-signing is not explicitly disabled, check if it should be enabled first.
|
|
|
|
if (cli.getCryptoTrustCrossSignedDevices()) {
|
2021-09-21 15:48:09 +00:00
|
|
|
if (!(await enable4SIfNeeded())) {
|
2020-04-14 15:53:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-10 15:31:40 +00:00
|
|
|
}
|
2020-04-27 19:31:14 +00:00
|
|
|
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(UntrustedDeviceDialog, {
|
2020-04-28 17:35:16 +00:00
|
|
|
user,
|
|
|
|
device,
|
2023-01-12 13:25:14 +00:00
|
|
|
onFinished: async (action): Promise<void> => {
|
2020-04-28 17:35:16 +00:00
|
|
|
if (action === "sas") {
|
|
|
|
const verificationRequestPromise = cli.legacyDeviceVerification(
|
|
|
|
user.userId,
|
|
|
|
device.deviceId,
|
2021-07-10 14:43:46 +00:00
|
|
|
VerificationMethods.SAS,
|
2020-04-28 17:35:16 +00:00
|
|
|
);
|
2022-01-05 16:25:41 +00:00
|
|
|
setRightPanel({ member: user, verificationRequestPromise });
|
2020-04-28 17:35:16 +00:00
|
|
|
} else if (action === "legacy") {
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(ManualDeviceKeyVerificationDialog, {
|
|
|
|
userId: user.userId,
|
|
|
|
device,
|
|
|
|
});
|
2020-04-28 17:35:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2020-03-10 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export async function legacyVerifyUser(user: User): Promise<void> {
|
2020-03-10 15:31:40 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
2021-01-26 11:00:56 +00:00
|
|
|
if (cli.isGuest()) {
|
2021-06-29 12:11:58 +00:00
|
|
|
dis.dispatch({ action: "require_registration" });
|
2021-01-26 11:00:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-14 15:53:33 +00:00
|
|
|
// if cross-signing is not explicitly disabled, check if it should be enabled first.
|
|
|
|
if (cli.getCryptoTrustCrossSignedDevices()) {
|
2021-09-21 15:48:09 +00:00
|
|
|
if (!(await enable4SIfNeeded())) {
|
2020-04-14 15:53:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 08:32:27 +00:00
|
|
|
const verificationRequestPromise = cli.requestVerification(user.userId);
|
2022-01-05 16:25:41 +00:00
|
|
|
setRightPanel({ member: user, verificationRequestPromise });
|
2020-03-10 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export async function verifyUser(user: User): Promise<void> {
|
2021-01-26 11:00:56 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
if (cli.isGuest()) {
|
2021-06-29 12:11:58 +00:00
|
|
|
dis.dispatch({ action: "require_registration" });
|
2021-01-26 11:00:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-21 15:48:09 +00:00
|
|
|
if (!(await enable4SIfNeeded())) {
|
2020-03-10 15:31:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-26 16:31:31 +00:00
|
|
|
const existingRequest = pendingVerificationRequestForUser(user);
|
2022-01-05 16:25:41 +00:00
|
|
|
setRightPanel({ member: user, verificationRequest: existingRequest });
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
function setRightPanel(state: IRightPanelCardState): void {
|
2022-01-05 16:25:41 +00: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 },
|
|
|
|
{ phase: RightPanelPhases.RoomMemberInfo, state: { member: state.member } },
|
|
|
|
{ phase: RightPanelPhases.EncryptionPanel, state },
|
|
|
|
]);
|
|
|
|
}
|
2020-03-10 15:31:40 +00:00
|
|
|
}
|
2020-03-26 16:31:31 +00:00
|
|
|
|
2023-02-10 18:11:57 +00:00
|
|
|
export function pendingVerificationRequestForUser(user: User | RoomMember): VerificationRequest | undefined {
|
2020-03-26 16:31:31 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const dmRoom = findDMForUser(cli, user.userId);
|
|
|
|
if (dmRoom) {
|
|
|
|
return cli.findVerificationRequestDMInProgress(dmRoom.roomId);
|
|
|
|
}
|
|
|
|
}
|