2020-01-17 12:43:35 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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-10-23 00:23:32 +02:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-02-22 13:18:08 +01:00
|
|
|
import { CryptoEvent } from "matrix-js-sdk/src/crypto";
|
2023-06-01 15:43:24 +02:00
|
|
|
import { ClientEvent, EventType, MatrixClient, RoomStateEvent } from "matrix-js-sdk/src/matrix";
|
2022-05-26 12:12:49 +02:00
|
|
|
import { SyncState } from "matrix-js-sdk/src/sync";
|
2023-01-12 14:25:14 +01:00
|
|
|
import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup";
|
2021-10-23 00:23:32 +02:00
|
|
|
|
2020-08-19 17:13:29 +02:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2020-05-22 14:29:53 +02:00
|
|
|
import {
|
|
|
|
hideToast as hideBulkUnverifiedSessionsToast,
|
2020-06-23 17:41:36 +02:00
|
|
|
showToast as showBulkUnverifiedSessionsToast,
|
2020-05-22 14:29:53 +02:00
|
|
|
} from "./toasts/BulkUnverifiedSessionsToast";
|
|
|
|
import {
|
|
|
|
hideToast as hideSetupEncryptionToast,
|
|
|
|
Kind as SetupKind,
|
2020-06-23 17:41:36 +02:00
|
|
|
showToast as showSetupEncryptionToast,
|
2020-05-22 14:29:53 +02:00
|
|
|
} from "./toasts/SetupEncryptionToast";
|
|
|
|
import {
|
|
|
|
hideToast as hideUnverifiedSessionsToast,
|
2020-06-23 17:41:36 +02:00
|
|
|
showToast as showUnverifiedSessionsToast,
|
2020-05-22 14:29:53 +02:00
|
|
|
} from "./toasts/UnverifiedSessionToast";
|
2022-02-22 13:18:08 +01:00
|
|
|
import { accessSecretStorage, isSecretStorageBeingAccessed } from "./SecurityManager";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { isSecureBackupRequired } from "./utils/WellKnownUtils";
|
2021-07-19 23:43:11 +02:00
|
|
|
import { ActionPayload } from "./dispatcher/payloads";
|
2022-02-11 14:00:37 +01:00
|
|
|
import { Action } from "./dispatcher/actions";
|
2022-03-24 22:33:09 +01:00
|
|
|
import { isLoggedIn } from "./utils/login";
|
2022-10-04 09:53:23 +02:00
|
|
|
import SdkConfig from "./SdkConfig";
|
|
|
|
import PlatformPeg from "./PlatformPeg";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { recordClientInformation, removeClientInformation } from "./utils/device/clientInformation";
|
2022-10-04 09:53:23 +02:00
|
|
|
import SettingsStore, { CallbackFn } from "./settings/SettingsStore";
|
2022-12-02 04:24:45 +01:00
|
|
|
import { UIFeature } from "./settings/UIFeature";
|
2022-12-06 07:18:03 +01:00
|
|
|
import { isBulkUnverifiedDeviceReminderSnoozed } from "./utils/device/snoozeBulkUnverifiedDeviceReminder";
|
2020-01-17 12:43:35 +01:00
|
|
|
|
2020-01-25 17:52:12 +01:00
|
|
|
const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;
|
2020-04-28 19:35:16 +02:00
|
|
|
|
2020-01-17 12:43:35 +01:00
|
|
|
export default class DeviceListener {
|
2023-05-05 10:11:14 +02:00
|
|
|
private dispatcherRef?: string;
|
2020-05-22 13:54:03 +02:00
|
|
|
// device IDs for which the user has dismissed the verify toast ('Later')
|
|
|
|
private dismissed = new Set<string>();
|
|
|
|
// has the user dismissed any of the various nag toasts to setup encryption on this device?
|
|
|
|
private dismissedThisDeviceToast = false;
|
|
|
|
// cache of the key backup info
|
2023-01-12 14:25:14 +01:00
|
|
|
private keyBackupInfo: IKeyBackupInfo | null = null;
|
2023-02-03 16:27:47 +01:00
|
|
|
private keyBackupFetchedAt: number | null = null;
|
2022-02-11 14:00:37 +01:00
|
|
|
private keyBackupStatusChecked = false;
|
2020-05-22 13:54:03 +02:00
|
|
|
// We keep a list of our own device IDs so we can batch ones that were already
|
|
|
|
// there the last time the app launched into a single toast, but display new
|
|
|
|
// ones in their own toasts.
|
2023-02-03 16:27:47 +01:00
|
|
|
private ourDeviceIdsAtStart: Set<string> | null = null;
|
2020-05-22 13:54:03 +02:00
|
|
|
// The set of device IDs we're currently displaying toasts for
|
|
|
|
private displayingToastsForDeviceIds = new Set<string>();
|
2022-05-26 12:12:49 +02:00
|
|
|
private running = false;
|
2023-06-01 15:43:24 +02:00
|
|
|
// The client with which the instance is running. Only set if `running` is true, otherwise undefined.
|
|
|
|
private client?: MatrixClient;
|
2022-10-04 09:53:23 +02:00
|
|
|
private shouldRecordClientInformation = false;
|
2022-12-02 04:24:45 +01:00
|
|
|
private enableBulkUnverifiedSessionsReminder = true;
|
2022-10-04 09:53:23 +02:00
|
|
|
private deviceClientInformationSettingWatcherRef: string | undefined;
|
2020-01-25 17:52:12 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public static sharedInstance(): DeviceListener {
|
2020-07-20 21:43:49 +02:00
|
|
|
if (!window.mxDeviceListener) window.mxDeviceListener = new DeviceListener();
|
|
|
|
return window.mxDeviceListener;
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
|
|
|
|
2023-06-01 15:43:24 +02:00
|
|
|
public start(matrixClient: MatrixClient): void {
|
2022-05-26 12:12:49 +02:00
|
|
|
this.running = true;
|
2023-06-01 15:43:24 +02:00
|
|
|
this.client = matrixClient;
|
|
|
|
this.client.on(CryptoEvent.WillUpdateDevices, this.onWillUpdateDevices);
|
|
|
|
this.client.on(CryptoEvent.DevicesUpdated, this.onDevicesUpdated);
|
|
|
|
this.client.on(CryptoEvent.DeviceVerificationChanged, this.onDeviceVerificationChanged);
|
|
|
|
this.client.on(CryptoEvent.UserTrustStatusChanged, this.onUserTrustStatusChanged);
|
|
|
|
this.client.on(CryptoEvent.KeysChanged, this.onCrossSingingKeysChanged);
|
|
|
|
this.client.on(ClientEvent.AccountData, this.onAccountData);
|
|
|
|
this.client.on(ClientEvent.Sync, this.onSync);
|
|
|
|
this.client.on(RoomStateEvent.Events, this.onRoomStateEvents);
|
2022-12-12 12:24:14 +01:00
|
|
|
this.shouldRecordClientInformation = SettingsStore.getValue("deviceClientInformationOptIn");
|
2022-12-02 04:24:45 +01:00
|
|
|
// only configurable in config, so we don't need to watch the value
|
|
|
|
this.enableBulkUnverifiedSessionsReminder = SettingsStore.getValue(UIFeature.BulkUnverifiedSessionsReminder);
|
2022-10-04 09:53:23 +02:00
|
|
|
this.deviceClientInformationSettingWatcherRef = SettingsStore.watchSetting(
|
2022-12-12 12:24:14 +01:00
|
|
|
"deviceClientInformationOptIn",
|
2022-10-04 09:53:23 +02:00
|
|
|
null,
|
|
|
|
this.onRecordClientInformationSettingChange,
|
|
|
|
);
|
2021-07-19 23:43:11 +02:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
|
|
|
this.recheck();
|
2022-10-10 21:00:46 +02:00
|
|
|
this.updateClientInformation();
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public stop(): void {
|
2022-05-26 12:12:49 +02:00
|
|
|
this.running = false;
|
2023-06-01 15:43:24 +02:00
|
|
|
if (this.client) {
|
|
|
|
this.client.removeListener(CryptoEvent.WillUpdateDevices, this.onWillUpdateDevices);
|
|
|
|
this.client.removeListener(CryptoEvent.DevicesUpdated, this.onDevicesUpdated);
|
|
|
|
this.client.removeListener(CryptoEvent.DeviceVerificationChanged, this.onDeviceVerificationChanged);
|
|
|
|
this.client.removeListener(CryptoEvent.UserTrustStatusChanged, this.onUserTrustStatusChanged);
|
|
|
|
this.client.removeListener(CryptoEvent.KeysChanged, this.onCrossSingingKeysChanged);
|
|
|
|
this.client.removeListener(ClientEvent.AccountData, this.onAccountData);
|
|
|
|
this.client.removeListener(ClientEvent.Sync, this.onSync);
|
|
|
|
this.client.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
2022-10-04 09:53:23 +02:00
|
|
|
if (this.deviceClientInformationSettingWatcherRef) {
|
|
|
|
SettingsStore.unwatchSetting(this.deviceClientInformationSettingWatcherRef);
|
|
|
|
}
|
2020-08-19 17:13:29 +02:00
|
|
|
if (this.dispatcherRef) {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
2023-05-05 10:11:14 +02:00
|
|
|
this.dispatcherRef = undefined;
|
2020-08-19 17:13:29 +02:00
|
|
|
}
|
2020-05-22 13:54:03 +02:00
|
|
|
this.dismissed.clear();
|
|
|
|
this.dismissedThisDeviceToast = false;
|
|
|
|
this.keyBackupInfo = null;
|
|
|
|
this.keyBackupFetchedAt = null;
|
2022-02-11 14:00:37 +01:00
|
|
|
this.keyBackupStatusChecked = false;
|
2020-05-22 13:54:03 +02:00
|
|
|
this.ourDeviceIdsAtStart = null;
|
|
|
|
this.displayingToastsForDeviceIds = new Set();
|
2023-06-01 15:43:24 +02:00
|
|
|
this.client = undefined;
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 19:35:16 +02:00
|
|
|
/**
|
|
|
|
* Dismiss notifications about our own unverified devices
|
|
|
|
*
|
|
|
|
* @param {String[]} deviceIds List of device IDs to dismiss notifications for
|
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public async dismissUnverifiedSessions(deviceIds: Iterable<string>): Promise<void> {
|
2022-12-12 12:24:14 +01:00
|
|
|
logger.log("Dismissing unverified sessions: " + Array.from(deviceIds).join(","));
|
2020-04-28 19:35:16 +02:00
|
|
|
for (const d of deviceIds) {
|
2020-05-22 13:54:03 +02:00
|
|
|
this.dismissed.add(d);
|
2020-04-28 19:35:16 +02:00
|
|
|
}
|
2020-04-27 19:33:54 +02:00
|
|
|
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-01-25 17:52:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public dismissEncryptionSetup(): void {
|
2020-05-22 13:54:03 +02:00
|
|
|
this.dismissedThisDeviceToast = true;
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:39:51 +02:00
|
|
|
private async ensureDeviceIdsAtStartPopulated(): Promise<void> {
|
2020-05-22 13:54:03 +02:00
|
|
|
if (this.ourDeviceIdsAtStart === null) {
|
2023-05-09 15:39:51 +02:00
|
|
|
this.ourDeviceIdsAtStart = await this.getDeviceIds();
|
2020-04-28 19:35:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 15:39:51 +02:00
|
|
|
/** Get the device list for the current user
|
|
|
|
*
|
|
|
|
* @returns the set of device IDs
|
|
|
|
*/
|
|
|
|
private async getDeviceIds(): Promise<Set<string>> {
|
2023-06-01 15:43:24 +02:00
|
|
|
const cli = this.client;
|
|
|
|
const crypto = cli?.getCrypto();
|
2023-05-09 15:39:51 +02:00
|
|
|
if (crypto === undefined) return new Set();
|
|
|
|
|
2023-06-01 15:43:24 +02:00
|
|
|
const userId = cli!.getSafeUserId();
|
2023-05-09 15:39:51 +02:00
|
|
|
const devices = await crypto.getUserDeviceInfo([userId]);
|
|
|
|
return new Set(devices.get(userId)?.keys() ?? []);
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onWillUpdateDevices = async (users: string[], initialFetch?: boolean): Promise<void> => {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!this.client) return;
|
2020-04-29 18:33:18 +02:00
|
|
|
// If we didn't know about *any* devices before (ie. it's fresh login),
|
2020-04-29 18:16:04 +02:00
|
|
|
// then they are all pre-existing devices, so ignore this and set the
|
|
|
|
// devicesAtStart list to the devices that we see after the fetch.
|
|
|
|
if (initialFetch) return;
|
|
|
|
|
2023-06-01 15:43:24 +02:00
|
|
|
const myUserId = this.client.getSafeUserId();
|
2023-05-09 15:39:51 +02:00
|
|
|
if (users.includes(myUserId)) await this.ensureDeviceIdsAtStartPopulated();
|
2020-04-29 11:55:44 +02:00
|
|
|
|
|
|
|
// No need to do a recheck here: we just need to get a snapshot of our devices
|
2020-04-29 12:25:18 +02:00
|
|
|
// before we download any new ones.
|
2020-06-18 15:32:43 +02:00
|
|
|
};
|
2020-04-28 19:35:16 +02:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onDevicesUpdated = (users: string[]): void => {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!this.client) return;
|
|
|
|
if (!users.includes(this.client.getSafeUserId())) return;
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-06-18 15:32:43 +02:00
|
|
|
};
|
2020-01-17 12:43:35 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onDeviceVerificationChanged = (userId: string): void => {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!this.client) return;
|
|
|
|
if (userId !== this.client.getUserId()) return;
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-06-18 15:32:43 +02:00
|
|
|
};
|
2020-01-25 17:52:12 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onUserTrustStatusChanged = (userId: string): void => {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!this.client) return;
|
|
|
|
if (userId !== this.client.getUserId()) return;
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-06-18 15:32:43 +02:00
|
|
|
};
|
2020-01-25 17:52:12 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onCrossSingingKeysChanged = (): void => {
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-06-18 15:32:43 +02:00
|
|
|
};
|
2020-04-09 13:43:51 +02:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onAccountData = (ev: MatrixEvent): void => {
|
2020-04-07 11:57:10 +02:00
|
|
|
// User may have:
|
|
|
|
// * migrated SSSS to symmetric
|
|
|
|
// * uploaded keys to secret storage
|
|
|
|
// * completed secret storage creation
|
|
|
|
// which result in account data changes affecting checks below.
|
|
|
|
if (
|
2022-12-12 12:24:14 +01:00
|
|
|
ev.getType().startsWith("m.secret_storage.") ||
|
|
|
|
ev.getType().startsWith("m.cross_signing.") ||
|
|
|
|
ev.getType() === "m.megolm_backup.v1"
|
2020-04-07 11:57:10 +02:00
|
|
|
) {
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-03-20 19:53:31 +01:00
|
|
|
}
|
2020-06-18 15:32:43 +02:00
|
|
|
};
|
2020-03-20 19:53:31 +01:00
|
|
|
|
2023-02-03 16:27:47 +01:00
|
|
|
private onSync = (state: SyncState, prevState: SyncState | null): void => {
|
2022-12-12 12:24:14 +01:00
|
|
|
if (state === "PREPARED" && prevState === null) {
|
2022-05-26 12:12:49 +02:00
|
|
|
this.recheck();
|
|
|
|
}
|
2020-06-18 15:32:43 +02:00
|
|
|
};
|
2020-04-20 15:36:15 +02:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onRoomStateEvents = (ev: MatrixEvent): void => {
|
2022-02-24 15:39:25 +01:00
|
|
|
if (ev.getType() !== EventType.RoomEncryption) return;
|
2020-09-15 18:37:05 +02:00
|
|
|
|
|
|
|
// If a room changes to encrypted, re-check as it may be our first
|
|
|
|
// encrypted room. This also catches encrypted room creation as well.
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2020-09-15 18:37:05 +02:00
|
|
|
};
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onAction = ({ action }: ActionPayload): void => {
|
2022-05-26 10:56:53 +02:00
|
|
|
if (action !== Action.OnLoggedIn) return;
|
2021-07-19 23:43:11 +02:00
|
|
|
this.recheck();
|
2022-10-10 21:00:46 +02:00
|
|
|
this.updateClientInformation();
|
2020-08-19 17:13:29 +02:00
|
|
|
};
|
|
|
|
|
2020-01-25 17:52:12 +01:00
|
|
|
// The server doesn't tell us when key backup is set up, so we poll
|
|
|
|
// & cache the result
|
2023-02-16 18:21:44 +01:00
|
|
|
private async getKeyBackupInfo(): Promise<IKeyBackupInfo | null> {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!this.client) return null;
|
2022-12-12 12:24:14 +01:00
|
|
|
const now = new Date().getTime();
|
2023-03-07 14:19:18 +01:00
|
|
|
if (
|
|
|
|
!this.keyBackupInfo ||
|
|
|
|
!this.keyBackupFetchedAt ||
|
|
|
|
this.keyBackupFetchedAt < now - KEY_BACKUP_POLL_INTERVAL
|
|
|
|
) {
|
2023-06-01 15:43:24 +02:00
|
|
|
this.keyBackupInfo = await this.client.getKeyBackupVersion();
|
2020-05-22 13:54:03 +02:00
|
|
|
this.keyBackupFetchedAt = now;
|
2020-01-25 17:52:12 +01:00
|
|
|
}
|
2020-05-22 13:54:03 +02:00
|
|
|
return this.keyBackupInfo;
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private shouldShowSetupEncryptionToast(): boolean {
|
2020-08-19 14:28:03 +02:00
|
|
|
// If we're in the middle of a secret storage operation, we're likely
|
|
|
|
// modifying the state involved here, so don't add new toasts to setup.
|
|
|
|
if (isSecretStorageBeingAccessed()) return false;
|
2020-09-16 13:25:02 +02:00
|
|
|
// Show setup toasts once the user is in at least one encrypted room.
|
2023-06-01 15:43:24 +02:00
|
|
|
const cli = this.client;
|
|
|
|
return cli?.getRooms().some((r) => cli.isRoomEncrypted(r.roomId)) ?? false;
|
2020-06-02 22:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private async recheck(): Promise<void> {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!this.running || !this.client) return; // we have been stopped
|
|
|
|
const cli = this.client;
|
2020-01-17 12:43:35 +01:00
|
|
|
|
2023-04-14 12:14:05 +02:00
|
|
|
// cross-signing support was added to Matrix in MSC1756, which landed in spec v1.1
|
|
|
|
if (!(await cli.isVersionSupported("v1.1"))) return;
|
2020-03-12 19:03:18 +01:00
|
|
|
|
2023-05-12 17:55:01 +02:00
|
|
|
const crypto = cli.getCrypto();
|
|
|
|
if (!crypto) return;
|
|
|
|
|
2020-04-20 15:36:15 +02:00
|
|
|
// don't recheck until the initial sync is complete: lots of account data events will fire
|
|
|
|
// while the initial sync is processing and we don't need to recheck on each one of them
|
|
|
|
// (we add a listener on sync to do once check after the initial sync is done)
|
|
|
|
if (!cli.isInitialSyncComplete()) return;
|
2020-03-23 19:36:37 +01:00
|
|
|
|
2023-05-12 17:55:01 +02:00
|
|
|
const crossSigningReady = await crypto.isCrossSigningReady();
|
|
|
|
const secretStorageReady = await crypto.isSecretStorageReady();
|
2020-08-21 19:22:05 +02:00
|
|
|
const allSystemsReady = crossSigningReady && secretStorageReady;
|
2020-03-23 19:36:37 +01:00
|
|
|
|
2020-08-21 19:22:05 +02:00
|
|
|
if (this.dismissedThisDeviceToast || allSystemsReady) {
|
2020-05-22 14:29:53 +02:00
|
|
|
hideSetupEncryptionToast();
|
2022-02-11 14:00:37 +01:00
|
|
|
|
|
|
|
this.checkKeyBackupStatus();
|
2020-06-02 22:40:32 +02:00
|
|
|
} else if (this.shouldShowSetupEncryptionToast()) {
|
2020-05-22 14:29:53 +02:00
|
|
|
// make sure our keys are finished downloading
|
2023-06-01 15:43:24 +02:00
|
|
|
await crypto.getUserDeviceInfo([cli.getSafeUserId()]);
|
2023-05-12 17:55:01 +02:00
|
|
|
|
2020-05-22 14:29:53 +02:00
|
|
|
// cross signing isn't enabled - nag to enable it
|
|
|
|
// There are 3 different toasts for:
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!(await crypto.getCrossSigningKeyId()) && cli.getStoredCrossSigningForUser(cli.getSafeUserId())) {
|
2020-05-22 14:29:53 +02:00
|
|
|
// Cross-signing on account but this device doesn't trust the master key (verify this session)
|
|
|
|
showSetupEncryptionToast(SetupKind.VERIFY_THIS_SESSION);
|
2022-02-11 14:00:37 +01:00
|
|
|
this.checkKeyBackupStatus();
|
2020-05-22 14:29:53 +02:00
|
|
|
} else {
|
2021-07-19 23:43:11 +02:00
|
|
|
const backupInfo = await this.getKeyBackupInfo();
|
2020-05-22 14:29:53 +02:00
|
|
|
if (backupInfo) {
|
|
|
|
// No cross-signing on account but key backup available (upgrade encryption)
|
2020-06-02 22:40:32 +02:00
|
|
|
showSetupEncryptionToast(SetupKind.UPGRADE_ENCRYPTION);
|
2020-01-25 17:52:12 +01:00
|
|
|
} else {
|
2020-05-22 14:29:53 +02:00
|
|
|
// No cross-signing or key backup on account (set up encryption)
|
2020-08-24 17:11:35 +02:00
|
|
|
await cli.waitForClientWellKnown();
|
2023-05-23 17:24:12 +02:00
|
|
|
if (isSecureBackupRequired(cli) && isLoggedIn()) {
|
2020-08-19 17:13:29 +02:00
|
|
|
// If we're meant to set up, and Secure Backup is required,
|
|
|
|
// trigger the flow directly without a toast once logged in.
|
|
|
|
hideSetupEncryptionToast();
|
|
|
|
accessSecretStorage();
|
|
|
|
} else {
|
|
|
|
showSetupEncryptionToast(SetupKind.SET_UP_ENCRYPTION);
|
|
|
|
}
|
2020-01-25 17:52:12 +01:00
|
|
|
}
|
2020-03-20 20:01:26 +01:00
|
|
|
}
|
2020-01-25 17:52:12 +01:00
|
|
|
}
|
2020-01-17 15:08:37 +01:00
|
|
|
|
2023-05-12 17:55:01 +02:00
|
|
|
// This needs to be done after awaiting on getUserDeviceInfo() above, so
|
2020-04-29 18:16:04 +02:00
|
|
|
// we make sure we get the devices after the fetch is done.
|
2023-05-09 15:39:51 +02:00
|
|
|
await this.ensureDeviceIdsAtStartPopulated();
|
2020-04-29 18:16:04 +02:00
|
|
|
|
2020-04-28 19:35:16 +02:00
|
|
|
// Unverified devices that were there last time the app ran
|
|
|
|
// (technically could just be a boolean: we don't actually
|
|
|
|
// need to remember the device IDs, but for the sake of
|
|
|
|
// symmetry...).
|
2020-05-22 13:54:03 +02:00
|
|
|
const oldUnverifiedDeviceIds = new Set<string>();
|
2020-04-28 19:35:16 +02:00
|
|
|
// Unverified devices that have appeared since then
|
2020-05-22 13:54:03 +02:00
|
|
|
const newUnverifiedDeviceIds = new Set<string>();
|
2020-04-28 19:35:16 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const isCurrentDeviceTrusted =
|
2023-04-24 15:19:46 +02:00
|
|
|
crossSigningReady &&
|
2023-06-01 15:43:24 +02:00
|
|
|
Boolean(
|
|
|
|
(await crypto.getDeviceVerificationStatus(cli.getSafeUserId(), cli.deviceId!))?.crossSigningVerified,
|
|
|
|
);
|
2022-12-02 03:59:46 +01:00
|
|
|
|
2020-03-27 16:45:46 +01:00
|
|
|
// as long as cross-signing isn't ready,
|
|
|
|
// you can't see or dismiss any device toasts
|
|
|
|
if (crossSigningReady) {
|
2023-05-09 15:39:51 +02:00
|
|
|
const devices = await this.getDeviceIds();
|
|
|
|
for (const deviceId of devices) {
|
|
|
|
if (deviceId === cli.deviceId) continue;
|
|
|
|
|
2023-06-01 15:43:24 +02:00
|
|
|
const deviceTrust = await crypto.getDeviceVerificationStatus(cli.getSafeUserId(), deviceId);
|
2023-05-09 15:39:51 +02:00
|
|
|
if (!deviceTrust?.crossSigningVerified && !this.dismissed.has(deviceId)) {
|
|
|
|
if (this.ourDeviceIdsAtStart?.has(deviceId)) {
|
|
|
|
oldUnverifiedDeviceIds.add(deviceId);
|
2020-04-28 19:35:16 +02:00
|
|
|
} else {
|
2023-05-09 15:39:51 +02:00
|
|
|
newUnverifiedDeviceIds.add(deviceId);
|
2020-04-28 19:35:16 +02:00
|
|
|
}
|
2020-03-27 16:45:46 +01:00
|
|
|
}
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
2020-04-28 19:35:16 +02:00
|
|
|
}
|
2020-01-25 18:08:31 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
logger.debug("Old unverified sessions: " + Array.from(oldUnverifiedDeviceIds).join(","));
|
|
|
|
logger.debug("New unverified sessions: " + Array.from(newUnverifiedDeviceIds).join(","));
|
|
|
|
logger.debug("Currently showing toasts for: " + Array.from(this.displayingToastsForDeviceIds).join(","));
|
2021-09-03 16:14:44 +02:00
|
|
|
|
2022-12-06 07:18:03 +01:00
|
|
|
const isBulkUnverifiedSessionsReminderSnoozed = isBulkUnverifiedDeviceReminderSnoozed();
|
|
|
|
|
2020-04-28 19:35:16 +02:00
|
|
|
// Display or hide the batch toast for old unverified sessions
|
2022-12-02 03:59:46 +01:00
|
|
|
// don't show the toast if the current device is unverified
|
2022-12-02 04:24:45 +01:00
|
|
|
if (
|
2022-12-12 12:24:14 +01:00
|
|
|
oldUnverifiedDeviceIds.size > 0 &&
|
|
|
|
isCurrentDeviceTrusted &&
|
|
|
|
this.enableBulkUnverifiedSessionsReminder &&
|
|
|
|
!isBulkUnverifiedSessionsReminderSnoozed
|
2022-12-02 04:24:45 +01:00
|
|
|
) {
|
2020-05-22 14:29:53 +02:00
|
|
|
showBulkUnverifiedSessionsToast(oldUnverifiedDeviceIds);
|
2020-04-28 19:35:16 +02:00
|
|
|
} else {
|
2020-05-22 14:29:53 +02:00
|
|
|
hideBulkUnverifiedSessionsToast();
|
2020-04-28 19:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show toasts for new unverified devices if they aren't already there
|
|
|
|
for (const deviceId of newUnverifiedDeviceIds) {
|
2020-05-22 14:29:53 +02:00
|
|
|
showUnverifiedSessionsToast(deviceId);
|
2020-04-28 19:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ...and hide any we don't need any more
|
2020-05-22 13:54:03 +02:00
|
|
|
for (const deviceId of this.displayingToastsForDeviceIds) {
|
2020-04-28 19:35:16 +02:00
|
|
|
if (!newUnverifiedDeviceIds.has(deviceId)) {
|
2022-03-02 15:30:04 +01:00
|
|
|
logger.debug("Hiding unverified session toast for " + deviceId);
|
2020-05-22 14:29:53 +02:00
|
|
|
hideUnverifiedSessionsToast(deviceId);
|
2020-03-27 16:45:46 +01:00
|
|
|
}
|
2020-01-25 18:08:31 +01:00
|
|
|
}
|
2020-04-28 19:35:16 +02:00
|
|
|
|
2020-05-22 13:54:03 +02:00
|
|
|
this.displayingToastsForDeviceIds = newUnverifiedDeviceIds;
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|
2022-02-11 14:00:37 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private checkKeyBackupStatus = async (): Promise<void> => {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (this.keyBackupStatusChecked || !this.client) {
|
2022-02-11 14:00:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// returns null when key backup status hasn't finished being checked
|
2023-06-01 15:43:24 +02:00
|
|
|
const isKeyBackupEnabled = this.client.getKeyBackupEnabled();
|
2022-02-11 14:00:37 +01:00
|
|
|
this.keyBackupStatusChecked = isKeyBackupEnabled !== null;
|
|
|
|
|
|
|
|
if (isKeyBackupEnabled === false) {
|
|
|
|
dis.dispatch({ action: Action.ReportKeyBackupNotEnabled });
|
|
|
|
}
|
|
|
|
};
|
2022-10-04 09:53:23 +02:00
|
|
|
|
|
|
|
private onRecordClientInformationSettingChange: CallbackFn = (
|
2022-12-12 12:24:14 +01:00
|
|
|
_originalSettingName,
|
|
|
|
_roomId,
|
|
|
|
_level,
|
|
|
|
_newLevel,
|
|
|
|
newValue,
|
2022-10-04 09:53:23 +02:00
|
|
|
) => {
|
|
|
|
const prevValue = this.shouldRecordClientInformation;
|
|
|
|
|
|
|
|
this.shouldRecordClientInformation = !!newValue;
|
|
|
|
|
2022-10-10 21:00:46 +02:00
|
|
|
if (this.shouldRecordClientInformation !== prevValue) {
|
|
|
|
this.updateClientInformation();
|
2022-10-04 09:53:23 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private updateClientInformation = async (): Promise<void> => {
|
2023-06-01 15:43:24 +02:00
|
|
|
if (!this.client) return;
|
2022-10-04 09:53:23 +02:00
|
|
|
try {
|
2022-10-10 21:00:46 +02:00
|
|
|
if (this.shouldRecordClientInformation) {
|
2023-06-01 15:43:24 +02:00
|
|
|
await recordClientInformation(this.client, SdkConfig.get(), PlatformPeg.get() ?? undefined);
|
2022-10-10 21:00:46 +02:00
|
|
|
} else {
|
2023-06-01 15:43:24 +02:00
|
|
|
await removeClientInformation(this.client);
|
2022-10-10 21:00:46 +02:00
|
|
|
}
|
2022-10-04 09:53:23 +02:00
|
|
|
} catch (error) {
|
|
|
|
// this is a best effort operation
|
|
|
|
// log the error without rethrowing
|
2022-12-12 12:24:14 +01:00
|
|
|
logger.error("Failed to update client information", error);
|
2022-10-04 09:53:23 +02:00
|
|
|
}
|
|
|
|
};
|
2020-01-17 12:43:35 +01:00
|
|
|
}
|