2021-06-18 18:13:55 +03:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
2020-03-27 16:45:21 +03:00
|
|
|
|
2021-06-18 18:13:55 +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
|
2020-03-27 16:45:21 +03:00
|
|
|
|
2021-06-18 18:13:55 +03:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
|
|
|
|
import DMRoomMap from './DMRoomMap';
|
2020-03-27 16:50:03 +03:00
|
|
|
|
2020-09-07 19:32:03 +03:00
|
|
|
export enum E2EStatus {
|
|
|
|
Warning = "warning",
|
|
|
|
Verified = "verified",
|
|
|
|
Normal = "normal"
|
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:55 +03:00
|
|
|
export async function shieldStatusForRoom(client: MatrixClient, room: Room): Promise<E2EStatus> {
|
2020-03-27 16:45:21 +03:00
|
|
|
const members = (await room.getEncryptionTargetMembers()).map(({userId}) => userId);
|
|
|
|
const inDMMap = !!DMRoomMap.shared().getUserIdForRoomId(room.roomId);
|
|
|
|
|
2020-03-27 16:50:03 +03:00
|
|
|
const verified: string[] = [];
|
|
|
|
const unverified: string[] = [];
|
2020-03-27 16:45:21 +03:00
|
|
|
members.filter((userId) => userId !== client.getUserId())
|
|
|
|
.forEach((userId) => {
|
|
|
|
(client.checkUserTrust(userId).isCrossSigningVerified() ?
|
2020-06-23 18:41:36 +03:00
|
|
|
verified : unverified).push(userId);
|
2020-03-27 16:45:21 +03:00
|
|
|
});
|
|
|
|
|
2020-03-28 03:21:17 +03:00
|
|
|
/* Alarm if any unverified users were verified before. */
|
|
|
|
for (const userId of unverified) {
|
|
|
|
if (client.checkUserTrust(userId).wasCrossSigningVerified()) {
|
2020-09-07 19:32:03 +03:00
|
|
|
return E2EStatus.Warning;
|
2020-03-28 03:21:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:45:21 +03:00
|
|
|
/* Check all verified user devices. */
|
|
|
|
/* Don't alarm if no other users are verified */
|
|
|
|
const includeUser = (verified.length > 0) && // Don't alarm for self in rooms where nobody else is verified
|
|
|
|
!inDMMap && // Don't alarm for self in DMs with other users
|
2020-03-30 12:18:47 +03:00
|
|
|
(members.length !== 2) || // Don't alarm for self in 1:1 chats with other users
|
|
|
|
(members.length === 1); // Do alarm for self if we're alone in a room
|
2020-03-27 16:45:21 +03:00
|
|
|
const targets = includeUser ? [...verified, client.getUserId()] : verified;
|
|
|
|
for (const userId of targets) {
|
2020-04-28 20:35:16 +03:00
|
|
|
const devices = client.getStoredDevicesForUser(userId);
|
2020-03-27 16:45:21 +03:00
|
|
|
const anyDeviceNotVerified = devices.some(({deviceId}) => {
|
|
|
|
return !client.checkDeviceTrust(userId, deviceId).isVerified();
|
|
|
|
});
|
|
|
|
if (anyDeviceNotVerified) {
|
2020-09-07 19:32:03 +03:00
|
|
|
return E2EStatus.Warning;
|
2020-03-27 16:45:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 19:32:03 +03:00
|
|
|
return unverified.length === 0 ? E2EStatus.Verified : E2EStatus.Normal;
|
2020-03-27 16:45:21 +03:00
|
|
|
}
|