Support sas verification with other users

This commit is contained in:
RMidhunSuresh 2023-04-12 15:46:45 +05:30
parent 660db4ced3
commit 49db9d810a
3 changed files with 59 additions and 17 deletions

View File

@ -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() {

View File

@ -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,

View File

@ -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<string, Deferred<any>> = 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 {