Skip to content

Commit

Permalink
Process all verification event
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Aug 4, 2023
1 parent 2ef7ae7 commit ad24dde
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import { secretStorageContainsCrossSigningKeys } from "./secret-storage";
import { keyFromPassphrase } from "../crypto/key_passphrase";
import { encodeRecoveryKey } from "../crypto/recoverykey";
import { crypto } from "../crypto/crypto";
import { RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification";
import { EventType, MsgType } from "../@types/event";
import { isVerificationEvent, RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification";
import { EventType } from "../@types/event";
import { CryptoEvent } from "../crypto";
import { TypedEventEmitter } from "../models/typed-event-emitter";
import { RustBackupManager } from "./backup";
Expand Down Expand Up @@ -1040,15 +1040,12 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
* @param event - live event
*/
public async onLiveEventFromSync(event: MatrixEvent): Promise<void> {
// Ignore state event
if (event.isState()) return;
// Ignore state event or remote echo
if (event.isState() || event.getUnsigned().transaction_id === null) return;

const processEvent = async (evt: MatrixEvent): Promise<void> => {
// Process only key validation request
if (
evt.getType() === EventType.RoomMessage &&
evt.getContent().msgtype === MsgType.KeyVerificationRequest
) {
// Process only verification event
if (isVerificationEvent(event)) {
await this.onKeyVerificationRequest(evt);
}
};
Expand All @@ -1058,6 +1055,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
// 5 mins
const TIMEOUT_DELAY = 5 * 60 * 1000;

// After 5mins, we are not expecting the event to be decrypted
const timeoutId = setTimeout(() => event.off(MatrixEventEvent.Decrypted, onDecrypted), TIMEOUT_DELAY);

const onDecrypted = (decryptedEvent: MatrixEvent, error?: Error): void => {
Expand All @@ -1067,7 +1065,6 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
event.off(MatrixEventEvent.Decrypted, onDecrypted);
processEvent(decryptedEvent);
};
// After 5mins, we are not expecting the event to be decrypted

event.on(MatrixEventEvent.Decrypted, onDecrypted);
} else {
Expand Down
30 changes: 30 additions & 0 deletions src/rust-crypto/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
import { TypedEventEmitter } from "../models/typed-event-emitter";
import { OutgoingRequest, OutgoingRequestProcessor } from "./OutgoingRequestProcessor";
import { TypedReEmitter } from "../ReEmitter";
import { MatrixEvent } from "../models/event";
import { EventType, MsgType } from "../@types/event";

/**
* An incoming, or outgoing, request to verify a user or a device via cross-signing.
Expand Down Expand Up @@ -700,3 +702,31 @@ export function verificationMethodIdentifierToMethod(method: string): RustSdkCry
}
return meth;
}

/**
* Return true if the event is a verification event
*
* @param event - MatrixEvent
* @returns
*
* @internal
*/
export function isVerificationEvent(event: MatrixEvent): boolean {
switch (event.getType()) {
case EventType.KeyVerificationCancel:
case EventType.KeyVerificationDone:
case EventType.KeyVerificationMac:
case EventType.KeyVerificationStart:
case EventType.KeyVerificationKey:
case EventType.KeyVerificationReady:
case EventType.KeyVerificationAccept: {
return true;
}
case EventType.RoomMessage: {
return event.getContent().msgtype === MsgType.KeyVerificationRequest;
}
default: {
return false;
}
}
}

0 comments on commit ad24dde

Please sign in to comment.