mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2025-02-02 07:31:38 +01:00
WIP +1
This commit is contained in:
parent
d81864e901
commit
e6ea003bef
@ -17,17 +17,28 @@ import {StartVerificationStage} from "./stages/StartVerificationStage";
|
|||||||
import type {ILogItem} from "../../../logging/types";
|
import type {ILogItem} from "../../../logging/types";
|
||||||
import type {Room} from "../../room/Room.js";
|
import type {Room} from "../../room/Room.js";
|
||||||
import type {BaseSASVerificationStage, UserData} from "./stages/BaseSASVerificationStage";
|
import type {BaseSASVerificationStage, UserData} from "./stages/BaseSASVerificationStage";
|
||||||
|
import {WaitForIncomingMessageStage} from "./stages/WaitForIncomingMessageStage";
|
||||||
|
|
||||||
export class SASVerification {
|
export class SASVerification {
|
||||||
private stages: BaseSASVerificationStage[] = [];
|
private startStage: BaseSASVerificationStage;
|
||||||
|
|
||||||
constructor(private room: Room, private ourUser: UserData, otherUserId: string, log: ILogItem) {
|
constructor(private room: Room, private ourUser: UserData, otherUserId: string, log: ILogItem) {
|
||||||
this.stages.push(new StartVerificationStage(room, ourUser, otherUserId, log));
|
const options = { room, ourUser, otherUserId, log };
|
||||||
|
let stage: BaseSASVerificationStage = new StartVerificationStage(options);
|
||||||
|
this.startStage = stage;
|
||||||
|
|
||||||
|
stage.setNextStage(new WaitForIncomingMessageStage("m.key.verification.ready", options));
|
||||||
|
stage = stage.nextStage;
|
||||||
|
|
||||||
|
stage.setNextStage(new WaitForIncomingMessageStage("m.key.verification.start", options));
|
||||||
|
stage = stage.nextStage;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start() {
|
async start() {
|
||||||
for (const stage of this.stages) {
|
let stage = this.startStage;
|
||||||
await stage.completeStage();
|
do {
|
||||||
}
|
await stage.completeStage();
|
||||||
|
stage = stage.nextStage;
|
||||||
|
} while (stage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,13 +36,14 @@ export abstract class BaseSASVerificationStage extends Disposables {
|
|||||||
protected log: ILogItem;
|
protected log: ILogItem;
|
||||||
protected requestEventId: string;
|
protected requestEventId: string;
|
||||||
protected previousResult: undefined | any;
|
protected previousResult: undefined | any;
|
||||||
|
protected _nextStage: BaseSASVerificationStage;
|
||||||
|
|
||||||
constructor(options: Options) {
|
constructor(options: Options) {
|
||||||
super();
|
super();
|
||||||
this.room = options.room;
|
this.room = options.room;
|
||||||
this.ourUser = options.ourUser;
|
this.ourUser = options.ourUser;
|
||||||
this.otherUserId = options.otherUserId;
|
this.otherUserId = options.otherUserId;
|
||||||
this.log = options.log;
|
this.log = options.log;
|
||||||
}
|
}
|
||||||
|
|
||||||
setRequestEventId(id: string) {
|
setRequestEventId(id: string) {
|
||||||
@ -55,7 +56,14 @@ export abstract class BaseSASVerificationStage extends Disposables {
|
|||||||
this.previousResult = result;
|
this.previousResult = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setNextStage(stage: BaseSASVerificationStage) {
|
||||||
|
this._nextStage = stage;
|
||||||
|
}
|
||||||
|
|
||||||
|
get nextStage(): BaseSASVerificationStage {
|
||||||
|
return this._nextStage;
|
||||||
|
}
|
||||||
|
|
||||||
abstract get type(): string;
|
abstract get type(): string;
|
||||||
abstract completeStage(): undefined | Record<string, any>;
|
abstract completeStage(): Promise<any>;
|
||||||
abstract get nextStage(): BaseSASVerificationStage;
|
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
|
import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
|
||||||
|
import {FragmentBoundaryEntry} from "../../../room/timeline/entries/FragmentBoundaryEntry.js";
|
||||||
|
|
||||||
// From element-web
|
// From element-web
|
||||||
// type KeyAgreement = "curve25519-hkdf-sha256" | "curve25519";
|
// type KeyAgreement = "curve25519-hkdf-sha256" | "curve25519";
|
||||||
@ -31,9 +32,6 @@ import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
|
|||||||
// const SAS_LIST = Object.keys(sasGenerators);
|
// const SAS_LIST = Object.keys(sasGenerators);
|
||||||
export class StartVerificationStage extends BaseSASVerificationStage {
|
export class StartVerificationStage extends BaseSASVerificationStage {
|
||||||
|
|
||||||
private readyMessagePromise: Promise<any>;
|
|
||||||
private startMessagePromise: Promise<any>;
|
|
||||||
|
|
||||||
async completeStage() {
|
async completeStage() {
|
||||||
await this.log.wrap("StartVerificationStage.completeStage", async (log) => {
|
await this.log.wrap("StartVerificationStage.completeStage", async (log) => {
|
||||||
const content = {
|
const content = {
|
||||||
@ -43,44 +41,38 @@ export class StartVerificationStage extends BaseSASVerificationStage {
|
|||||||
"msgtype": "m.key.verification.request",
|
"msgtype": "m.key.verification.request",
|
||||||
"to": this.otherUserId,
|
"to": this.otherUserId,
|
||||||
};
|
};
|
||||||
|
const promise = this.trackEventId();
|
||||||
await this.room.sendEvent("m.room.message", content, null, log);
|
await this.room.sendEvent("m.room.message", content, null, log);
|
||||||
const [readyContent, startContent] = await this.fetchMessageEventsFromTimeline();
|
const eventId = await promise;
|
||||||
console.log("readyContent", readyContent, "startContent", startContent);
|
console.log("eventId", eventId);
|
||||||
|
this.setRequestEventId(eventId);
|
||||||
this.dispose();
|
this.dispose();
|
||||||
});
|
});
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fetchMessageEventsFromTimeline() {
|
private trackEventId(): Promise<string> {
|
||||||
let readyResolve, startResolve;
|
return new Promise(resolve => {
|
||||||
this.readyMessagePromise = new Promise(r => { readyResolve = r; });
|
this.track(
|
||||||
this.startMessagePromise = new Promise(r => { startResolve = r; });
|
this.room._timeline.entries.subscribe({
|
||||||
this.track(
|
onAdd: (_, entry) => {
|
||||||
this.room._timeline.entries.subscribe({
|
if (entry instanceof FragmentBoundaryEntry) {
|
||||||
onAdd: (_, entry) => {
|
return;
|
||||||
if (entry.eventType === "m.key.verification.ready") {
|
}
|
||||||
readyResolve(entry.content);
|
if (!entry.isPending &&
|
||||||
}
|
entry.content["msgtype"] === "m.key.verification.request" &&
|
||||||
else if (entry.eventType === "m.key.verification.start") {
|
entry.content["from_device"] === this.ourUser.deviceId) {
|
||||||
startResolve(entry.content);
|
console.log("found event", entry);
|
||||||
}
|
resolve(entry.id);
|
||||||
},
|
}
|
||||||
onRemove: () => {
|
},
|
||||||
|
onRemove: () => { /**noop*/ },
|
||||||
},
|
onUpdate: () => { /**noop*/ },
|
||||||
onUpdate: () => {
|
})
|
||||||
|
);
|
||||||
},
|
});
|
||||||
})
|
|
||||||
);
|
|
||||||
return Promise.all([this.readyMessagePromise, this.startMessagePromise]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get type() {
|
get type() {
|
||||||
return "m.key.verification.request";
|
return "m.key.verification.request";
|
||||||
}
|
}
|
||||||
|
|
||||||
get nextStage(): BaseSASVerificationStage {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
import {BaseSASVerificationStage, Options} from "./BaseSASVerificationStage";
|
import {BaseSASVerificationStage, Options} from "./BaseSASVerificationStage";
|
||||||
|
import {FragmentBoundaryEntry} from "../../../room/timeline/entries/FragmentBoundaryEntry.js";
|
||||||
|
|
||||||
export class WaitForIncomingMessageStage extends BaseSASVerificationStage {
|
export class WaitForIncomingMessageStage extends BaseSASVerificationStage {
|
||||||
constructor(private messageType: string, options: Options) {
|
constructor(private messageType: string, options: Options) {
|
||||||
@ -30,7 +31,6 @@ export class WaitForIncomingMessageStage extends BaseSASVerificationStage {
|
|||||||
});
|
});
|
||||||
this.dispose();
|
this.dispose();
|
||||||
});
|
});
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fetchMessageEventsFromTimeline() {
|
private fetchMessageEventsFromTimeline() {
|
||||||
@ -43,7 +43,7 @@ export class WaitForIncomingMessageStage extends BaseSASVerificationStage {
|
|||||||
// We only care about incoming / remote message events
|
// We only care about incoming / remote message events
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (entry.eventType === this.messageType &&
|
if (entry.type === this.messageType &&
|
||||||
entry.content["m.relates_to"]["event_id"] === this.requestEventId) {
|
entry.content["m.relates_to"]["event_id"] === this.requestEventId) {
|
||||||
resolve(entry.content);
|
resolve(entry.content);
|
||||||
}
|
}
|
||||||
@ -55,7 +55,10 @@ export class WaitForIncomingMessageStage extends BaseSASVerificationStage {
|
|||||||
const remoteEntries = this.room._timeline.remoteEntries;
|
const remoteEntries = this.room._timeline.remoteEntries;
|
||||||
// In case we were slow and the event is already added to the timeline,
|
// In case we were slow and the event is already added to the timeline,
|
||||||
for (const entry of remoteEntries) {
|
for (const entry of remoteEntries) {
|
||||||
if (entry.eventType === this.messageType &&
|
if (entry instanceof FragmentBoundaryEntry) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (entry.type === this.messageType &&
|
||||||
entry.content["m.relates_to"]["event_id"] === this.requestEventId) {
|
entry.content["m.relates_to"]["event_id"] === this.requestEventId) {
|
||||||
resolve(entry.content);
|
resolve(entry.content);
|
||||||
}
|
}
|
||||||
@ -66,9 +69,5 @@ export class WaitForIncomingMessageStage extends BaseSASVerificationStage {
|
|||||||
get type() {
|
get type() {
|
||||||
return this.messageType;
|
return this.messageType;
|
||||||
}
|
}
|
||||||
|
|
||||||
get nextStage(): BaseSASVerificationStage {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user