Simplify isDeviceVerified
definitions (#10594)
* Simplify `isDeviceVerified` definitions Currently, we have two similar but different definitions of `isDeviceVerified`, and they both do a lot of wrangling that relies on js-sdk internals. We can simplify it a lot by just calling `MatrixClientPeg.checkDeviceTrust`. * fix tests * more test fixes
This commit is contained in:
parent
e4ebcf5731
commit
70b87f8bde
8 changed files with 37 additions and 67 deletions
|
@ -120,7 +120,7 @@ export default class DevicesPanel extends React.Component<IProps, IState> {
|
|||
}
|
||||
|
||||
private isDeviceVerified(device: IMyDevice): boolean | null {
|
||||
return isDeviceVerified(device, this.context);
|
||||
return isDeviceVerified(this.context, device.device_id);
|
||||
}
|
||||
|
||||
private onDeviceSelectionToggled = (device: IMyDevice): void => {
|
||||
|
|
|
@ -26,7 +26,6 @@ import {
|
|||
PUSHER_ENABLED,
|
||||
UNSTABLE_MSC3852_LAST_SEEN_UA,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { CrossSigningInfo } from "matrix-js-sdk/src/crypto/CrossSigning";
|
||||
import { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
|
||||
import { MatrixError } from "matrix-js-sdk/src/http-api";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
@ -39,27 +38,7 @@ import { getDeviceClientInformation, pruneClientInformation } from "../../../../
|
|||
import { DevicesDictionary, ExtendedDevice, ExtendedDeviceAppInfo } from "./types";
|
||||
import { useEventEmitter } from "../../../../hooks/useEventEmitter";
|
||||
import { parseUserAgent } from "../../../../utils/device/parseUserAgent";
|
||||
|
||||
const isDeviceVerified = (
|
||||
matrixClient: MatrixClient,
|
||||
crossSigningInfo: CrossSigningInfo,
|
||||
device: IMyDevice,
|
||||
): boolean | null => {
|
||||
try {
|
||||
const userId = matrixClient.getUserId();
|
||||
if (!userId) {
|
||||
throw new Error("No user id");
|
||||
}
|
||||
const deviceInfo = matrixClient.getStoredDevice(userId, device.device_id);
|
||||
if (!deviceInfo) {
|
||||
throw new Error("No device info available");
|
||||
}
|
||||
return crossSigningInfo.checkDeviceTrust(crossSigningInfo, deviceInfo, false, true).isCrossSigningVerified();
|
||||
} catch (error) {
|
||||
logger.error("Error getting device cross-signing info", error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
import { isDeviceVerified } from "../../../../utils/device/isDeviceVerified";
|
||||
|
||||
const parseDeviceExtendedInformation = (matrixClient: MatrixClient, device: IMyDevice): ExtendedDeviceAppInfo => {
|
||||
const { name, version, url } = getDeviceClientInformation(matrixClient, device.device_id);
|
||||
|
@ -71,20 +50,15 @@ const parseDeviceExtendedInformation = (matrixClient: MatrixClient, device: IMyD
|
|||
};
|
||||
};
|
||||
|
||||
const fetchDevicesWithVerification = async (
|
||||
matrixClient: MatrixClient,
|
||||
userId: string,
|
||||
): Promise<DevicesState["devices"]> => {
|
||||
const fetchDevicesWithVerification = async (matrixClient: MatrixClient): Promise<DevicesState["devices"]> => {
|
||||
const { devices } = await matrixClient.getDevices();
|
||||
|
||||
const crossSigningInfo = matrixClient.getStoredCrossSigningForUser(userId);
|
||||
|
||||
const devicesDict = devices.reduce(
|
||||
(acc, device: IMyDevice) => ({
|
||||
...acc,
|
||||
[device.device_id]: {
|
||||
...device,
|
||||
isVerified: isDeviceVerified(matrixClient, crossSigningInfo, device),
|
||||
isVerified: isDeviceVerified(matrixClient, device.device_id),
|
||||
...parseDeviceExtendedInformation(matrixClient, device),
|
||||
...parseUserAgent(device[UNSTABLE_MSC3852_LAST_SEEN_UA.name]),
|
||||
},
|
||||
|
@ -138,7 +112,7 @@ export const useOwnDevices = (): DevicesState => {
|
|||
const refreshDevices = useCallback(async (): Promise<void> => {
|
||||
setIsLoadingDeviceList(true);
|
||||
try {
|
||||
const devices = await fetchDevicesWithVerification(matrixClient, userId);
|
||||
const devices = await fetchDevicesWithVerification(matrixClient);
|
||||
setDevices(devices);
|
||||
|
||||
const { pushers } = await matrixClient.getPushers();
|
||||
|
@ -165,7 +139,7 @@ export const useOwnDevices = (): DevicesState => {
|
|||
}
|
||||
setIsLoadingDeviceList(false);
|
||||
}
|
||||
}, [matrixClient, userId]);
|
||||
}, [matrixClient]);
|
||||
|
||||
useEffect(() => {
|
||||
refreshDevices();
|
||||
|
|
|
@ -48,7 +48,7 @@ export const showToast = async (deviceId: string): Promise<void> => {
|
|||
const device = await cli.getDevice(deviceId);
|
||||
const extendedDevice = {
|
||||
...device,
|
||||
isVerified: isDeviceVerified(device, cli),
|
||||
isVerified: isDeviceVerified(cli, deviceId),
|
||||
deviceType: DeviceType.Unknown,
|
||||
};
|
||||
|
||||
|
|
|
@ -14,17 +14,21 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { IMyDevice, MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
export const isDeviceVerified = (device: IMyDevice, client: MatrixClient): boolean | null => {
|
||||
/**
|
||||
* Check if one of our own devices is verified via cross signing
|
||||
*
|
||||
* @param client - reference to the MatrixClient
|
||||
* @param deviceId - ID of the device to be checked
|
||||
*
|
||||
* @returns `true` if the device has been correctly cross-signed. `false` if the device is unknown or not correctly
|
||||
* cross-signed. `null` if there was an error fetching the device info.
|
||||
*/
|
||||
export const isDeviceVerified = (client: MatrixClient, deviceId: string): boolean | null => {
|
||||
try {
|
||||
const crossSigningInfo = client.getStoredCrossSigningForUser(client.getSafeUserId());
|
||||
const deviceInfo = client.getStoredDevice(client.getSafeUserId(), device.device_id);
|
||||
|
||||
// no cross-signing or device info available
|
||||
if (!crossSigningInfo || !deviceInfo) return false;
|
||||
|
||||
return crossSigningInfo.checkDeviceTrust(crossSigningInfo, deviceInfo, false, true).isCrossSigningVerified();
|
||||
const trustLevel = client.checkDeviceTrust(client.getSafeUserId(), deviceId);
|
||||
return trustLevel.isCrossSigningVerified();
|
||||
} catch (e) {
|
||||
console.error("Error getting device cross-signing info", e);
|
||||
return null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue