This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
/
DevicesPanel.tsx
378 lines (327 loc) · 13.1 KB
/
DevicesPanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
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<IProps, IState> {
public static contextType = MatrixClientContext;
public context!: React.ContextType<typeof MatrixClientContext>;
private unmounted = false;
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[]) => {
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<void> => {
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 <DevicesPanelEntry
key={device.device_id}
device={device}
selected={this.state.selectedDevices.includes(device.device_id)}
isOwnDevice={isOwnDevice}
verified={this.isDeviceVerified(device)}
canBeVerified={canBeVerified}
onDeviceChange={this.loadDevices}
onDeviceToggled={this.onDeviceSelectionToggled}
/>;
};
public render(): JSX.Element {
const loadError = (
<div className={classNames(this.props.className, "error")}>
{ this.state.deviceLoadError }
</div>
);
if (this.state.deviceLoadError !== undefined) {
return loadError;
}
const devices = this.state.devices;
if (devices === undefined) {
// still loading
return <Spinner />;
}
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 <React.Fragment />;
}
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 = <div className="mx_DevicesPanel_header_button">
<AccessibleButton
className="mx_DevicesPanel_selectButton"
kind="secondary"
onClick={buttonAction}
>
{ buttonText }
</AccessibleButton>
</div>;
}
return <React.Fragment>
<hr />
<div className="mx_DevicesPanel_header">
<div className="mx_DevicesPanel_header_trust">
{ trustIcon }
</div>
<div className="mx_DevicesPanel_header_title">
{ title }
</div>
{ selectButton }
</div>
{ deviceList.map(this.renderDevice) }
</React.Fragment>;
};
const verifiedDevicesSection = section(
<span className="mx_DevicesPanel_header_icon mx_E2EIcon mx_E2EIcon_verified" />,
_t("Verified devices"),
verifiedDevices,
);
const unverifiedDevicesSection = section(
<span className="mx_DevicesPanel_header_icon mx_E2EIcon mx_E2EIcon_warning" />,
_t("Unverified devices"),
unverifiedDevices,
);
const nonCryptoDevicesSection = section(
<React.Fragment />,
_t("Devices without encryption support"),
nonCryptoDevices,
);
const deleteButton = this.state.deleting ?
<Spinner w={22} h={22} /> :
<AccessibleButton
className="mx_DevicesPanel_deleteButton"
onClick={this.onDeleteClick}
kind="danger_outline"
disabled={this.state.selectedDevices.length === 0}
data-testid='sign-out-devices-btn'
>
{ _t("Sign out %(count)s selected devices", { count: this.state.selectedDevices.length }) }
</AccessibleButton>;
const otherDevicesSection = (otherDevices.length > 0) ?
<React.Fragment>
{ verifiedDevicesSection }
{ unverifiedDevicesSection }
{ nonCryptoDevicesSection }
{ deleteButton }
</React.Fragment> :
<React.Fragment>
<hr />
<div className="mx_DevicesPanel_noOtherDevices">
{ _t("You aren't signed into any other devices.") }
</div>
</React.Fragment>;
const classes = classNames(this.props.className, "mx_DevicesPanel");
return (
<div className={classes}>
<div className="mx_DevicesPanel_header">
<div className="mx_DevicesPanel_header_title">
{ _t("This device") }
</div>
</div>
{ this.renderDevice(myDevice) }
{ otherDevicesSection }
</div>
);
}
}