2020-01-17 14:43:35 +03: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.
|
|
|
|
*/
|
|
|
|
|
2020-05-22 15:29:53 +03:00
|
|
|
import {MatrixClientPeg} from './MatrixClientPeg';
|
|
|
|
import {
|
|
|
|
hideToast as hideBulkUnverifiedSessionsToast,
|
|
|
|
showToast as showBulkUnverifiedSessionsToast
|
|
|
|
} from "./toasts/BulkUnverifiedSessionsToast";
|
|
|
|
import {
|
|
|
|
hideToast as hideSetupEncryptionToast,
|
|
|
|
Kind as SetupKind,
|
|
|
|
showToast as showSetupEncryptionToast
|
|
|
|
} from "./toasts/SetupEncryptionToast";
|
|
|
|
import {
|
|
|
|
hideToast as hideUnverifiedSessionsToast,
|
|
|
|
showToast as showUnverifiedSessionsToast
|
|
|
|
} from "./toasts/UnverifiedSessionToast";
|
2020-06-02 23:40:32 +03:00
|
|
|
import {privateShouldBeEncrypted} from "./createRoom";
|
2020-01-17 14:43:35 +03:00
|
|
|
|
2020-01-25 19:52:12 +03:00
|
|
|
const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;
|
2020-04-28 20:35:16 +03:00
|
|
|
|
2020-01-17 14:43:35 +03:00
|
|
|
export default class DeviceListener {
|
2020-05-22 14:54:03 +03: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
|
|
|
|
private keyBackupInfo: object = null;
|
|
|
|
private keyBackupFetchedAt: number = null;
|
|
|
|
// 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.
|
|
|
|
private ourDeviceIdsAtStart: Set<string> = null;
|
|
|
|
// The set of device IDs we're currently displaying toasts for
|
|
|
|
private displayingToastsForDeviceIds = new Set<string>();
|
2020-01-25 19:52:12 +03:00
|
|
|
|
2020-05-22 14:54:03 +03:00
|
|
|
static sharedInstance() {
|
|
|
|
if (!window.mx_DeviceListener) window.mx_DeviceListener = new DeviceListener();
|
|
|
|
return window.mx_DeviceListener;
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2020-04-28 20:35:16 +03:00
|
|
|
MatrixClientPeg.get().on('crypto.willUpdateDevices', this._onWillUpdateDevices);
|
2020-01-17 14:43:35 +03:00
|
|
|
MatrixClientPeg.get().on('crypto.devicesUpdated', this._onDevicesUpdated);
|
|
|
|
MatrixClientPeg.get().on('deviceVerificationChanged', this._onDeviceVerificationChanged);
|
2020-01-25 19:52:12 +03:00
|
|
|
MatrixClientPeg.get().on('userTrustStatusChanged', this._onUserTrustStatusChanged);
|
2020-04-09 14:43:51 +03:00
|
|
|
MatrixClientPeg.get().on('crossSigning.keysChanged', this._onCrossSingingKeysChanged);
|
2020-03-20 21:53:31 +03:00
|
|
|
MatrixClientPeg.get().on('accountData', this._onAccountData);
|
2020-04-20 16:36:15 +03:00
|
|
|
MatrixClientPeg.get().on('sync', this._onSync);
|
2020-01-25 19:52:12 +03:00
|
|
|
this._recheck();
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
if (MatrixClientPeg.get()) {
|
2020-04-28 20:35:16 +03:00
|
|
|
MatrixClientPeg.get().removeListener('crypto.willUpdateDevices', this._onWillUpdateDevices);
|
2020-01-17 14:43:35 +03:00
|
|
|
MatrixClientPeg.get().removeListener('crypto.devicesUpdated', this._onDevicesUpdated);
|
|
|
|
MatrixClientPeg.get().removeListener('deviceVerificationChanged', this._onDeviceVerificationChanged);
|
2020-01-25 19:52:12 +03:00
|
|
|
MatrixClientPeg.get().removeListener('userTrustStatusChanged', this._onUserTrustStatusChanged);
|
2020-04-09 14:43:51 +03:00
|
|
|
MatrixClientPeg.get().removeListener('crossSigning.keysChanged', this._onCrossSingingKeysChanged);
|
2020-03-20 21:53:31 +03:00
|
|
|
MatrixClientPeg.get().removeListener('accountData', this._onAccountData);
|
2020-04-20 16:36:15 +03:00
|
|
|
MatrixClientPeg.get().removeListener('sync', this._onSync);
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
2020-05-22 14:54:03 +03:00
|
|
|
this.dismissed.clear();
|
|
|
|
this.dismissedThisDeviceToast = false;
|
|
|
|
this.keyBackupInfo = null;
|
|
|
|
this.keyBackupFetchedAt = null;
|
|
|
|
this.ourDeviceIdsAtStart = null;
|
|
|
|
this.displayingToastsForDeviceIds = new Set();
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
|
|
|
|
2020-04-28 20:35:16 +03:00
|
|
|
/**
|
|
|
|
* Dismiss notifications about our own unverified devices
|
|
|
|
*
|
|
|
|
* @param {String[]} deviceIds List of device IDs to dismiss notifications for
|
|
|
|
*/
|
2020-05-22 15:29:53 +03:00
|
|
|
async dismissUnverifiedSessions(deviceIds: Iterable<string>) {
|
2020-04-28 20:35:16 +03:00
|
|
|
for (const d of deviceIds) {
|
2020-05-22 14:54:03 +03:00
|
|
|
this.dismissed.add(d);
|
2020-04-28 20:35:16 +03:00
|
|
|
}
|
2020-04-27 20:33:54 +03:00
|
|
|
|
2020-01-25 19:52:12 +03:00
|
|
|
this._recheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
dismissEncryptionSetup() {
|
2020-05-22 14:54:03 +03:00
|
|
|
this.dismissedThisDeviceToast = true;
|
2020-01-25 19:52:12 +03:00
|
|
|
this._recheck();
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
|
|
|
|
2020-04-28 20:35:16 +03:00
|
|
|
_ensureDeviceIdsAtStartPopulated() {
|
2020-05-22 14:54:03 +03:00
|
|
|
if (this.ourDeviceIdsAtStart === null) {
|
2020-04-28 20:35:16 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
2020-05-22 14:54:03 +03:00
|
|
|
this.ourDeviceIdsAtStart = new Set(
|
2020-04-28 20:35:16 +03:00
|
|
|
cli.getStoredDevicesForUser(cli.getUserId()).map(d => d.deviceId),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 14:54:03 +03:00
|
|
|
_onWillUpdateDevices = async (users: string[], initialFetch?: boolean) => {
|
2020-04-29 19:33:18 +03:00
|
|
|
// If we didn't know about *any* devices before (ie. it's fresh login),
|
2020-04-29 19:16:04 +03: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;
|
|
|
|
|
2020-04-28 20:35:16 +03:00
|
|
|
const myUserId = MatrixClientPeg.get().getUserId();
|
|
|
|
if (users.includes(myUserId)) this._ensureDeviceIdsAtStartPopulated();
|
2020-04-29 12:55:44 +03:00
|
|
|
|
|
|
|
// No need to do a recheck here: we just need to get a snapshot of our devices
|
2020-04-29 13:25:18 +03:00
|
|
|
// before we download any new ones.
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-04-28 20:35:16 +03:00
|
|
|
|
2020-05-22 14:54:03 +03:00
|
|
|
_onDevicesUpdated = (users: string[]) => {
|
2020-01-17 14:43:35 +03:00
|
|
|
if (!users.includes(MatrixClientPeg.get().getUserId())) return;
|
2020-01-25 19:52:12 +03:00
|
|
|
this._recheck();
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-01-17 14:43:35 +03:00
|
|
|
|
2020-05-22 14:54:03 +03:00
|
|
|
_onDeviceVerificationChanged = (userId: string) => {
|
2020-01-30 00:55:27 +03:00
|
|
|
if (userId !== MatrixClientPeg.get().getUserId()) return;
|
2020-01-25 19:52:12 +03:00
|
|
|
this._recheck();
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-01-25 19:52:12 +03:00
|
|
|
|
2020-05-22 14:54:03 +03:00
|
|
|
_onUserTrustStatusChanged = (userId: string) => {
|
2020-01-25 19:52:12 +03:00
|
|
|
if (userId !== MatrixClientPeg.get().getUserId()) return;
|
|
|
|
this._recheck();
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-01-25 19:52:12 +03:00
|
|
|
|
2020-04-09 14:43:51 +03:00
|
|
|
_onCrossSingingKeysChanged = () => {
|
|
|
|
this._recheck();
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-04-09 14:43:51 +03:00
|
|
|
|
2020-03-20 21:53:31 +03:00
|
|
|
_onAccountData = (ev) => {
|
2020-04-07 12:57:10 +03: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 (
|
|
|
|
ev.getType().startsWith('m.secret_storage.') ||
|
|
|
|
ev.getType().startsWith('m.cross_signing.')
|
|
|
|
) {
|
2020-03-20 21:53:31 +03:00
|
|
|
this._recheck();
|
|
|
|
}
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-03-20 21:53:31 +03:00
|
|
|
|
2020-04-20 16:36:15 +03:00
|
|
|
_onSync = (state, prevState) => {
|
|
|
|
if (state === 'PREPARED' && prevState === null) this._recheck();
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-04-20 16:36:15 +03:00
|
|
|
|
2020-01-25 19:52:12 +03:00
|
|
|
// The server doesn't tell us when key backup is set up, so we poll
|
|
|
|
// & cache the result
|
|
|
|
async _getKeyBackupInfo() {
|
|
|
|
const now = (new Date()).getTime();
|
2020-05-22 14:54:03 +03:00
|
|
|
if (!this.keyBackupInfo || this.keyBackupFetchedAt < now - KEY_BACKUP_POLL_INTERVAL) {
|
|
|
|
this.keyBackupInfo = await MatrixClientPeg.get().getKeyBackupVersion();
|
|
|
|
this.keyBackupFetchedAt = now;
|
2020-01-25 19:52:12 +03:00
|
|
|
}
|
2020-05-22 14:54:03 +03:00
|
|
|
return this.keyBackupInfo;
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 23:40:32 +03:00
|
|
|
private shouldShowSetupEncryptionToast() {
|
|
|
|
// In a default configuration, show the toasts. If the well-known config causes e2ee default to be false
|
|
|
|
// then do not show the toasts until user is in at least one encrypted room.
|
|
|
|
if (privateShouldBeEncrypted()) return true;
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
return cli && cli.getRooms().some(r => cli.isRoomEncrypted(r.roomId));
|
|
|
|
}
|
|
|
|
|
2020-01-25 19:52:12 +03:00
|
|
|
async _recheck() {
|
2020-01-17 14:43:35 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
|
2020-05-27 12:28:25 +03:00
|
|
|
if (!await cli.doesServerSupportUnstableFeature("org.matrix.e2e_cross_signing")) return;
|
2020-03-12 21:03:18 +03:00
|
|
|
|
2020-01-25 19:52:12 +03:00
|
|
|
if (!cli.isCryptoEnabled()) return;
|
2020-04-20 16:36:15 +03: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 21:36:37 +03:00
|
|
|
|
2020-03-24 16:03:07 +03:00
|
|
|
const crossSigningReady = await cli.isCrossSigningReady();
|
2020-03-23 21:36:37 +03:00
|
|
|
|
2020-05-22 15:29:53 +03:00
|
|
|
if (this.dismissedThisDeviceToast || crossSigningReady) {
|
|
|
|
hideSetupEncryptionToast();
|
2020-06-02 23:40:32 +03:00
|
|
|
} else if (this.shouldShowSetupEncryptionToast()) {
|
2020-05-22 15:29:53 +03:00
|
|
|
// make sure our keys are finished downloading
|
|
|
|
await cli.downloadKeys([cli.getUserId()]);
|
|
|
|
// cross signing isn't enabled - nag to enable it
|
|
|
|
// There are 3 different toasts for:
|
|
|
|
if (cli.getStoredCrossSigningForUser(cli.getUserId())) {
|
|
|
|
// Cross-signing on account but this device doesn't trust the master key (verify this session)
|
|
|
|
showSetupEncryptionToast(SetupKind.VERIFY_THIS_SESSION);
|
|
|
|
} else {
|
|
|
|
const backupInfo = await this._getKeyBackupInfo();
|
|
|
|
if (backupInfo) {
|
|
|
|
// No cross-signing on account but key backup available (upgrade encryption)
|
2020-06-02 23:40:32 +03:00
|
|
|
showSetupEncryptionToast(SetupKind.UPGRADE_ENCRYPTION);
|
2020-01-25 19:52:12 +03:00
|
|
|
} else {
|
2020-05-22 15:29:53 +03:00
|
|
|
// No cross-signing or key backup on account (set up encryption)
|
2020-06-02 23:40:32 +03:00
|
|
|
showSetupEncryptionToast(SetupKind.SET_UP_ENCRYPTION);
|
2020-01-25 19:52:12 +03:00
|
|
|
}
|
2020-03-20 22:01:26 +03:00
|
|
|
}
|
2020-01-25 19:52:12 +03:00
|
|
|
}
|
2020-01-17 17:08:37 +03:00
|
|
|
|
2020-04-29 19:16:04 +03:00
|
|
|
// This needs to be done after awaiting on downloadKeys() above, so
|
|
|
|
// we make sure we get the devices after the fetch is done.
|
|
|
|
this._ensureDeviceIdsAtStartPopulated();
|
|
|
|
|
2020-04-28 20:35:16 +03: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 14:54:03 +03:00
|
|
|
const oldUnverifiedDeviceIds = new Set<string>();
|
2020-04-28 20:35:16 +03:00
|
|
|
// Unverified devices that have appeared since then
|
2020-05-22 14:54:03 +03:00
|
|
|
const newUnverifiedDeviceIds = new Set<string>();
|
2020-04-28 20:35:16 +03:00
|
|
|
|
2020-03-27 18:45:46 +03:00
|
|
|
// as long as cross-signing isn't ready,
|
|
|
|
// you can't see or dismiss any device toasts
|
|
|
|
if (crossSigningReady) {
|
2020-04-28 20:35:16 +03:00
|
|
|
const devices = cli.getStoredDevicesForUser(cli.getUserId());
|
2020-03-27 18:45:46 +03:00
|
|
|
for (const device of devices) {
|
2020-05-22 14:54:03 +03:00
|
|
|
if (device.deviceId === cli.deviceId) continue;
|
2020-01-17 14:43:35 +03:00
|
|
|
|
2020-03-27 18:45:46 +03:00
|
|
|
const deviceTrust = await cli.checkDeviceTrust(cli.getUserId(), device.deviceId);
|
2020-05-22 14:54:03 +03:00
|
|
|
if (!deviceTrust.isCrossSigningVerified() && !this.dismissed.has(device.deviceId)) {
|
|
|
|
if (this.ourDeviceIdsAtStart.has(device.deviceId)) {
|
2020-04-28 20:35:16 +03:00
|
|
|
oldUnverifiedDeviceIds.add(device.deviceId);
|
|
|
|
} else {
|
|
|
|
newUnverifiedDeviceIds.add(device.deviceId);
|
|
|
|
}
|
2020-03-27 18:45:46 +03:00
|
|
|
}
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
2020-04-28 20:35:16 +03:00
|
|
|
}
|
2020-01-25 20:08:31 +03:00
|
|
|
|
2020-04-28 20:35:16 +03:00
|
|
|
// Display or hide the batch toast for old unverified sessions
|
|
|
|
if (oldUnverifiedDeviceIds.size > 0) {
|
2020-05-22 15:29:53 +03:00
|
|
|
showBulkUnverifiedSessionsToast(oldUnverifiedDeviceIds);
|
2020-04-28 20:35:16 +03:00
|
|
|
} else {
|
2020-05-22 15:29:53 +03:00
|
|
|
hideBulkUnverifiedSessionsToast();
|
2020-04-28 20:35:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show toasts for new unverified devices if they aren't already there
|
|
|
|
for (const deviceId of newUnverifiedDeviceIds) {
|
2020-05-22 15:29:53 +03:00
|
|
|
showUnverifiedSessionsToast(deviceId);
|
2020-04-28 20:35:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ...and hide any we don't need any more
|
2020-05-22 14:54:03 +03:00
|
|
|
for (const deviceId of this.displayingToastsForDeviceIds) {
|
2020-04-28 20:35:16 +03:00
|
|
|
if (!newUnverifiedDeviceIds.has(deviceId)) {
|
2020-05-22 15:29:53 +03:00
|
|
|
hideUnverifiedSessionsToast(deviceId);
|
2020-03-27 18:45:46 +03:00
|
|
|
}
|
2020-01-25 20:08:31 +03:00
|
|
|
}
|
2020-04-28 20:35:16 +03:00
|
|
|
|
2020-05-22 14:54:03 +03:00
|
|
|
this.displayingToastsForDeviceIds = newUnverifiedDeviceIds;
|
2020-01-17 14:43:35 +03:00
|
|
|
}
|
|
|
|
}
|