From 9afad108eff9d90d7a7faa29104db72583261591 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Tue, 6 Jun 2023 12:16:19 +0100 Subject: [PATCH] Add `getShowSasCallbacks`, `getShowQrCodeCallbacks` to VerifierBase (#3422) * Add `getShowSasCallbacks`, `getShowQrCodeCallbacks` to VerifierBase ... to avoid some type-casting * Integration test for QR code verification Followup to https://github.com/matrix-org/matrix-js-sdk/pull/3436: another integration test, this time using the QR code flow * Rename method ... it turns out not to be used quite as I thought. * tests for new methods * Use Object.defineProperty, and restore afterwards Apparently global.crypto exists in some environments * apply ts-ignore * More test coverage * fix bad merge --- spec/integ/crypto/verification.spec.ts | 10 ++++++++++ src/crypto/verification/Base.ts | 27 +++++++++++++++++++++++++- src/crypto/verification/QRCode.ts | 4 ++++ src/crypto/verification/SAS.ts | 4 ++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/spec/integ/crypto/verification.spec.ts b/spec/integ/crypto/verification.spec.ts index fdcc4bf1a4c..f4690294515 100644 --- a/spec/integ/crypto/verification.spec.ts +++ b/spec/integ/crypto/verification.spec.ts @@ -170,6 +170,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st // there should now be a verifier const verifier: VerificationBase = request.verifier!; expect(verifier).toBeDefined(); + expect(verifier.getShowSasCallbacks()).toBeNull(); // start off the verification process: alice will send an `accept` const verificationPromise = verifier.verify(); @@ -205,6 +206,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st verifier.once(VerifierEvent.ShowSas, resolve); }); + // `getShowSasCallbacks` is an alternative way to get the callbacks + expect(verifier.getShowSasCallbacks()).toBe(showSas); + expect(verifier.getReciprocateQrCodeCallbacks()).toBeNull(); + // user confirms that the emoji match, and alice sends a 'mac' [requestBody] = await Promise.all([expectSendToDeviceMessage("m.key.verification.mac"), showSas.confirm()]); toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID]; @@ -320,6 +325,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st // there should now be a verifier const verifier: VerificationBase = request.verifier!; expect(verifier).toBeDefined(); + expect(verifier.getReciprocateQrCodeCallbacks()).toBeNull(); // ... which we call .verify on, which emits a ShowReciprocateQr event const verificationPromise = verifier.verify(); @@ -327,6 +333,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st verifier.once(VerifierEvent.ShowReciprocateQr, resolve); }); + // getReciprocateQrCodeCallbacks() is an alternative way to get the callbacks + expect(verifier.getReciprocateQrCodeCallbacks()).toBe(reciprocateQRCodeCallbacks); + expect(verifier.getShowSasCallbacks()).toBeNull(); + // Alice confirms she is happy reciprocateQRCodeCallbacks.confirm(); diff --git a/src/crypto/verification/Base.ts b/src/crypto/verification/Base.ts index 89bead3aa3e..de6a0f836b0 100644 --- a/src/crypto/verification/Base.ts +++ b/src/crypto/verification/Base.ts @@ -29,7 +29,12 @@ import { IVerificationChannel } from "./request/Channel"; import { MatrixClient } from "../../client"; import { VerificationRequest } from "./request/VerificationRequest"; import { TypedEventEmitter } from "../../models/typed-event-emitter"; -import { VerifierEvent, VerifierEventHandlerMap } from "../../crypto-api/verification"; +import { + ShowQrCodeCallbacks, + ShowSasCallbacks, + VerifierEvent, + VerifierEventHandlerMap, +} from "../../crypto-api/verification"; const timeoutException = new Error("Verification timed out"); @@ -373,4 +378,24 @@ export class VerificationBase< public get events(): string[] | undefined { return undefined; } + + /** + * Get the details for an SAS verification, if one is in progress + * + * Returns `null`, unless this verifier is for a SAS-based verification and we are waiting for the user to confirm + * the SAS matches. + */ + public getShowSasCallbacks(): ShowSasCallbacks | null { + return null; + } + + /** + * Get the details for reciprocating QR code verification, if one is in progress + * + * Returns `null`, unless this verifier is for reciprocating a QR-code-based verification (ie, the other user has + * already scanned our QR code), and we are waiting for the user to confirm. + */ + public getReciprocateQrCodeCallbacks(): ShowQrCodeCallbacks | null { + return null; + } } diff --git a/src/crypto/verification/QRCode.ts b/src/crypto/verification/QRCode.ts index 38feade12db..a51db2e34e6 100644 --- a/src/crypto/verification/QRCode.ts +++ b/src/crypto/verification/QRCode.ts @@ -119,6 +119,10 @@ export class ReciprocateQRCode extends Base { } }); }; + + public getReciprocateQrCodeCallbacks(): ShowQrCodeCallbacks | null { + return this.reciprocateQREvent ?? null; + } } const CODE_VERSION = 0x02; // the version of binary QR codes we support diff --git a/src/crypto/verification/SAS.ts b/src/crypto/verification/SAS.ts index 61f096ca452..f15adf585f0 100644 --- a/src/crypto/verification/SAS.ts +++ b/src/crypto/verification/SAS.ts @@ -480,4 +480,8 @@ export class SAS extends Base { } }); } + + public getShowSasCallbacks(): ShowSasCallbacks | null { + return this.sasEvent ?? null; + } }