/* Copyright 2016 - 2021 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. */ import React from "react"; import classNames from "classnames"; import { IMyDevice } from "matrix-js-sdk/src/client"; import { logger } from "matrix-js-sdk/src/logger"; import { CrossSigningInfo } from "matrix-js-sdk/src/crypto/CrossSigning"; import { CryptoEvent } from "matrix-js-sdk/src/crypto"; import { _t } from "../../../languageHandler"; import DevicesPanelEntry from "./DevicesPanelEntry"; import Spinner from "../elements/Spinner"; import AccessibleButton from "../elements/AccessibleButton"; import { deleteDevicesWithInteractiveAuth } from "./devices/deleteDevices"; import MatrixClientContext from "../../../contexts/MatrixClientContext"; interface IProps { className?: string; } interface IState { devices: IMyDevice[]; crossSigningInfo?: CrossSigningInfo; deviceLoadError?: string; selectedDevices: string[]; deleting?: boolean; } export default class DevicesPanel extends React.Component { public static contextType = MatrixClientContext; public context!: React.ContextType; private unmounted = false; public constructor(props: IProps) { super(props); this.state = { devices: [], selectedDevices: [], }; this.loadDevices = this.loadDevices.bind(this); } public componentDidMount(): void { this.context.on(CryptoEvent.DevicesUpdated, this.onDevicesUpdated); this.loadDevices(); } public componentWillUnmount(): void { this.context.off(CryptoEvent.DevicesUpdated, this.onDevicesUpdated); this.unmounted = true; } private onDevicesUpdated = (users: string[]): void => { if (!users.includes(this.context.getUserId())) return; this.loadDevices(); }; private loadDevices(): void { const cli = this.context; cli.getDevices().then( (resp) => { if (this.unmounted) { return; } const crossSigningInfo = cli.getStoredCrossSigningForUser(cli.getUserId()); this.setState((state, props) => { const deviceIds = resp.devices.map((device) => device.device_id); const selectedDevices = state.selectedDevices.filter((deviceId) => deviceIds.includes(deviceId)); return { devices: resp.devices || [], selectedDevices, crossSigningInfo: crossSigningInfo, }; }); }, (error) => { if (this.unmounted) { return; } let errtxt; if (error.httpStatus == 404) { // 404 probably means the HS doesn't yet support the API. errtxt = _t("Your homeserver does not support device management."); } else { logger.error("Error loading sessions:", error); errtxt = _t("Unable to load device list"); } this.setState({ deviceLoadError: errtxt }); }, ); } /* * compare two devices, sorting from most-recently-seen to least-recently-seen * (and then, for stability, by device id) */ private deviceCompare(a: IMyDevice, b: IMyDevice): number { // return < 0 if a comes before b, > 0 if a comes after b. const lastSeenDelta = (b.last_seen_ts || 0) - (a.last_seen_ts || 0); if (lastSeenDelta !== 0) { return lastSeenDelta; } const idA = a.device_id; const idB = b.device_id; return idA < idB ? -1 : idA > idB ? 1 : 0; } private isDeviceVerified(device: IMyDevice): boolean | null { try { const cli = this.context; const deviceInfo = cli.getStoredDevice(cli.getUserId(), device.device_id); return this.state.crossSigningInfo .checkDeviceTrust(this.state.crossSigningInfo, deviceInfo, false, true) .isCrossSigningVerified(); } catch (e) { console.error("Error getting device cross-signing info", e); return null; } } private onDeviceSelectionToggled = (device: IMyDevice): void => { if (this.unmounted) { return; } const deviceId = device.device_id; this.setState((state, props) => { // Make a copy of the selected devices, then add or remove the device const selectedDevices = state.selectedDevices.slice(); const i = selectedDevices.indexOf(deviceId); if (i === -1) { selectedDevices.push(deviceId); } else { selectedDevices.splice(i, 1); } return { selectedDevices }; }); }; private selectAll = (devices: IMyDevice[]): void => { this.setState((state, props) => { const selectedDevices = state.selectedDevices.slice(); for (const device of devices) { const deviceId = device.device_id; if (!selectedDevices.includes(deviceId)) { selectedDevices.push(deviceId); } } return { selectedDevices }; }); }; private deselectAll = (devices: IMyDevice[]): void => { this.setState((state, props) => { const selectedDevices = state.selectedDevices.slice(); for (const device of devices) { const deviceId = device.device_id; const i = selectedDevices.indexOf(deviceId); if (i !== -1) { selectedDevices.splice(i, 1); } } return { selectedDevices }; }); }; private onDeleteClick = async (): Promise => { if (this.state.selectedDevices.length === 0) { return; } this.setState({ deleting: true, }); try { await deleteDevicesWithInteractiveAuth(this.context, this.state.selectedDevices, (success) => { if (success) { // Reset selection to [], update device list this.setState({ selectedDevices: [], }); this.loadDevices(); } this.setState({ deleting: false, }); }); } catch (error) { logger.error("Error deleting sessions", error); this.setState({ deleting: false, }); } }; private renderDevice = (device: IMyDevice): JSX.Element => { const myDeviceId = this.context.getDeviceId(); const myDevice = this.state.devices.find((device) => device.device_id === myDeviceId); const isOwnDevice = device.device_id === myDeviceId; // If our own device is unverified, it can't verify other // devices, it can only request verification for itself const canBeVerified = (myDevice && this.isDeviceVerified(myDevice)) || isOwnDevice; return ( ); }; public render(): JSX.Element { const loadError =
{this.state.deviceLoadError}
; if (this.state.deviceLoadError !== undefined) { return loadError; } const devices = this.state.devices; if (devices === undefined) { // still loading return ; } const myDeviceId = this.context.getDeviceId(); const myDevice = devices.find((device) => device.device_id === myDeviceId); if (!myDevice) { return loadError; } const otherDevices = devices.filter((device) => device.device_id !== myDeviceId); otherDevices.sort(this.deviceCompare); const verifiedDevices = []; const unverifiedDevices = []; const nonCryptoDevices = []; for (const device of otherDevices) { const verified = this.isDeviceVerified(device); if (verified === true) { verifiedDevices.push(device); } else if (verified === false) { unverifiedDevices.push(device); } else { nonCryptoDevices.push(device); } } const section = (trustIcon: JSX.Element, title: string, deviceList: IMyDevice[]): JSX.Element => { if (deviceList.length === 0) { return ; } let selectButton: JSX.Element; if (deviceList.length > 1) { const anySelected = deviceList.some((device) => this.state.selectedDevices.includes(device.device_id)); const buttonAction = anySelected ? () => { this.deselectAll(deviceList); } : () => { this.selectAll(deviceList); }; const buttonText = anySelected ? _t("Deselect all") : _t("Select all"); selectButton = (
{buttonText}
); } return (
{trustIcon}
{title}
{selectButton}
{deviceList.map(this.renderDevice)}
); }; const verifiedDevicesSection = section( , _t("Verified devices"), verifiedDevices, ); const unverifiedDevicesSection = section( , _t("Unverified devices"), unverifiedDevices, ); const nonCryptoDevicesSection = section( , _t("Devices without encryption support"), nonCryptoDevices, ); const deleteButton = this.state.deleting ? ( ) : ( {_t("Sign out %(count)s selected devices", { count: this.state.selectedDevices.length })} ); const otherDevicesSection = otherDevices.length > 0 ? ( {verifiedDevicesSection} {unverifiedDevicesSection} {nonCryptoDevicesSection} {deleteButton} ) : (
{_t("You aren't signed into any other devices.")}
); const classes = classNames(this.props.className, "mx_DevicesPanel"); return (
{_t("This device")}
{this.renderDevice(myDevice)} {otherDevicesSection}
); } }