From 49db9d810aa222d246eaf7939855f9b7dbab8a59 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 12 Apr 2023 15:46:45 +0530 Subject: [PATCH] Support sas verification with other users --- src/domain/session/SessionViewModel.js | 18 ++++++++ src/matrix/verification/CrossSigning.ts | 42 +++++++++++++------ .../verification/SAS/channel/RoomChannel.ts | 16 ++++--- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/domain/session/SessionViewModel.js b/src/domain/session/SessionViewModel.js index dc8589d1..dd8a1f4e 100644 --- a/src/domain/session/SessionViewModel.js +++ b/src/domain/session/SessionViewModel.js @@ -55,6 +55,24 @@ export class SessionViewModel extends ViewModel { }))); this._setupNavigation(); this._setupForcedLogoutOnAccessTokenInvalidation(); + this.addTestCode__REMOVE(); + } + + async addTestCode__REMOVE() { + window.run = (userId) => { + return this.logger.run("testRun", async (log) => { + const crossSigning = this._client.session.crossSigning.get(); + const room = this.currentRoomViewModel.room; + const sas = crossSigning.startVerification(userId, room, log); + sas.on("EmojiGenerated", async (stage) => { + console.log("emoji", stage.emoji); + await new Promise(r => setTimeout(r, 2000)); + await stage.setEmojiMatch(true); + }); + console.log("sas", sas); + await sas.verify(); + }); + } } _setupNavigation() { diff --git a/src/matrix/verification/CrossSigning.ts b/src/matrix/verification/CrossSigning.ts index 7e72fe4a..b3bcf596 100644 --- a/src/matrix/verification/CrossSigning.ts +++ b/src/matrix/verification/CrossSigning.ts @@ -19,6 +19,7 @@ import {BaseObservableValue, RetainedObservableValue} from "../../observable/val import {pkSign} from "./common"; import {SASVerification} from "./SAS/SASVerification"; import {ToDeviceChannel} from "./SAS/channel/ToDeviceChannel"; +import {RoomChannel} from "./SAS/channel/RoomChannel"; import {VerificationEventType} from "./SAS/channel/types"; import {ObservableMap} from "../../observable/map"; import {SASRequest} from "./SAS/SASRequest"; @@ -31,6 +32,8 @@ import type {Account} from "../e2ee/Account"; import type {ILogItem} from "../../logging/types"; import type {DeviceMessageHandler} from "../DeviceMessageHandler.js"; import type {SignedValue, DeviceKey} from "../e2ee/common"; +import type {Room} from "../room/Room.js"; +import type {IChannel} from "./SAS/channel/IChannel"; import type * as OlmNamespace from "@matrix-org/olm"; type Olm = typeof OlmNamespace; @@ -177,23 +180,38 @@ export class CrossSigning { return this._isMasterKeyTrusted; } - startVerification(requestOrUserId: SASRequest, log: ILogItem): SASVerification | undefined; - startVerification(requestOrUserId: string, log: ILogItem): SASVerification | undefined; - startVerification(requestOrUserId: string | SASRequest, log: ILogItem): SASVerification | undefined { + startVerification(requestOrUserId: SASRequest, logOrRoom: ILogItem): SASVerification | undefined; + startVerification(requestOrUserId: string, logOrRoom: ILogItem): SASVerification | undefined; + startVerification(requestOrUserId: SASRequest, logOrRoom: Room, _log: ILogItem): SASVerification | undefined; + startVerification(requestOrUserId: string, logOrRoom: Room, _log: ILogItem): SASVerification | undefined; + startVerification(requestOrUserId: string | SASRequest, logOrRoom: Room | ILogItem, _log?: ILogItem): SASVerification | undefined { if (this.sasVerificationInProgress && !this.sasVerificationInProgress.finished) { return; } const otherUserId = requestOrUserId instanceof SASRequest ? requestOrUserId.sender : requestOrUserId; const startingMessage = requestOrUserId instanceof SASRequest ? requestOrUserId.startingMessage : undefined; - const channel = new ToDeviceChannel({ - deviceTracker: this.deviceTracker, - hsApi: this.hsApi, - otherUserId, - clock: this.platform.clock, - deviceMessageHandler: this.deviceMessageHandler, - ourUserDeviceId: this.deviceId, - log - }, startingMessage); + const log = _log ?? logOrRoom; + let channel: IChannel; + if (otherUserId === this.ownUserId) { + channel = new ToDeviceChannel({ + deviceTracker: this.deviceTracker, + hsApi: this.hsApi, + otherUserId, + clock: this.platform.clock, + deviceMessageHandler: this.deviceMessageHandler, + ourUserDeviceId: this.deviceId, + log + }, startingMessage); + } + else { + channel = new RoomChannel({ + room: logOrRoom, + otherUserId, + ourUserId: this.ownUserId, + ourUserDeviceId: this.deviceId, + log, + }, startingMessage); + } this.sasVerificationInProgress = new SASVerification({ olm: this.olm, diff --git a/src/matrix/verification/SAS/channel/RoomChannel.ts b/src/matrix/verification/SAS/channel/RoomChannel.ts index a55b2db5..086f1c54 100644 --- a/src/matrix/verification/SAS/channel/RoomChannel.ts +++ b/src/matrix/verification/SAS/channel/RoomChannel.ts @@ -27,6 +27,7 @@ import {getRelatedEventId, createReference} from "../../../room/timeline/relatio type Options = { otherUserId: string; + ourUserId: string; log: ILogItem; ourUserDeviceId: string; room: Room; @@ -40,6 +41,7 @@ export class RoomChannel extends Disposables implements IChannel { private readonly waitMap: Map> = new Map(); private readonly log: ILogItem; private readonly room: Room; + private readonly ourUserId: string; public otherUserDeviceId: string; public startMessage: any; /** @@ -56,6 +58,7 @@ export class RoomChannel extends Disposables implements IChannel { constructor(options: Options, startingMessage?: any) { super(); this.otherUserId = options.otherUserId; + this.ourUserId = options.ourUserId; this.ourDeviceId = options.ourUserDeviceId; this.log = options.log; this.room = options.room; @@ -143,16 +146,17 @@ export class RoomChannel extends Disposables implements IChannel { private async handleRoomMessage(entry: EventEntry) { const type = entry.content.msgtype ?? entry.eventType; - if (!type.startsWith("m.key.verification")) { + if (!type.startsWith("m.key.verification") || entry.sender === this.ourUserId) { return; } + console.log("entry", entry); await this.log.wrap("RoomChannel.handleRoomMessage", async (log) => { console.log("entry", entry); log.log({ l: "entry", entry }); if (!this.id) { throw new Error("Couldn't find event-id of request message!"); } - if (getRelatedEventId(entry) !== this.id) { + if (getRelatedEventId(entry.event) !== this.id) { /** * When a device receives an unknown transaction_id, it should send an appropriate * m.key.verification.cancel message to the other device indicating as such. @@ -220,9 +224,11 @@ export class RoomChannel extends Disposables implements IChannel { return deferred.promise; } - setStartMessage(event) { - this.startMessage = event; - this._initiatedByUs = event.content.from_device === this.ourDeviceId; + setStartMessage(entry) { + const clone = entry.clone(); + clone.content["m.relates_to"] = clone.event.content["m.relates_to"]; + this.startMessage = clone; + this._initiatedByUs = entry.content.from_device === this.ourDeviceId; } get initiatedByUs(): boolean {