Skip to content

Commit 39714bf

Browse files
authored
Add getShowSasCallbacks, getShowQrCodeCallbacks to VerifierBase (#3422)
* Add `getShowSasCallbacks`, `getShowQrCodeCallbacks` to VerifierBase ... to avoid some type-casting * Integration test for QR code verification Followup to #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
1 parent f5f6100 commit 39714bf

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

spec/integ/crypto/verification.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
170170
// there should now be a verifier
171171
const verifier: VerificationBase = request.verifier!;
172172
expect(verifier).toBeDefined();
173+
expect(verifier.getShowSasCallbacks()).toBeNull();
173174

174175
// start off the verification process: alice will send an `accept`
175176
const verificationPromise = verifier.verify();
@@ -205,6 +206,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
205206
verifier.once(VerifierEvent.ShowSas, resolve);
206207
});
207208

209+
// `getShowSasCallbacks` is an alternative way to get the callbacks
210+
expect(verifier.getShowSasCallbacks()).toBe(showSas);
211+
expect(verifier.getReciprocateQrCodeCallbacks()).toBeNull();
212+
208213
// user confirms that the emoji match, and alice sends a 'mac'
209214
[requestBody] = await Promise.all([expectSendToDeviceMessage("m.key.verification.mac"), showSas.confirm()]);
210215
toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
@@ -320,13 +325,18 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
320325
// there should now be a verifier
321326
const verifier: VerificationBase = request.verifier!;
322327
expect(verifier).toBeDefined();
328+
expect(verifier.getReciprocateQrCodeCallbacks()).toBeNull();
323329

324330
// ... which we call .verify on, which emits a ShowReciprocateQr event
325331
const verificationPromise = verifier.verify();
326332
const reciprocateQRCodeCallbacks = await new Promise<ShowQrCodeCallbacks>((resolve) => {
327333
verifier.once(VerifierEvent.ShowReciprocateQr, resolve);
328334
});
329335

336+
// getReciprocateQrCodeCallbacks() is an alternative way to get the callbacks
337+
expect(verifier.getReciprocateQrCodeCallbacks()).toBe(reciprocateQRCodeCallbacks);
338+
expect(verifier.getShowSasCallbacks()).toBeNull();
339+
330340
// Alice confirms she is happy
331341
reciprocateQRCodeCallbacks.confirm();
332342

src/crypto/verification/Base.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ import { IVerificationChannel } from "./request/Channel";
2929
import { MatrixClient } from "../../client";
3030
import { VerificationRequest } from "./request/VerificationRequest";
3131
import { TypedEventEmitter } from "../../models/typed-event-emitter";
32-
import { VerifierEvent, VerifierEventHandlerMap } from "../../crypto-api/verification";
32+
import {
33+
ShowQrCodeCallbacks,
34+
ShowSasCallbacks,
35+
VerifierEvent,
36+
VerifierEventHandlerMap,
37+
} from "../../crypto-api/verification";
3338

3439
const timeoutException = new Error("Verification timed out");
3540

@@ -373,4 +378,24 @@ export class VerificationBase<
373378
public get events(): string[] | undefined {
374379
return undefined;
375380
}
381+
382+
/**
383+
* Get the details for an SAS verification, if one is in progress
384+
*
385+
* Returns `null`, unless this verifier is for a SAS-based verification and we are waiting for the user to confirm
386+
* the SAS matches.
387+
*/
388+
public getShowSasCallbacks(): ShowSasCallbacks | null {
389+
return null;
390+
}
391+
392+
/**
393+
* Get the details for reciprocating QR code verification, if one is in progress
394+
*
395+
* Returns `null`, unless this verifier is for reciprocating a QR-code-based verification (ie, the other user has
396+
* already scanned our QR code), and we are waiting for the user to confirm.
397+
*/
398+
public getReciprocateQrCodeCallbacks(): ShowQrCodeCallbacks | null {
399+
return null;
400+
}
376401
}

src/crypto/verification/QRCode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ export class ReciprocateQRCode extends Base {
119119
}
120120
});
121121
};
122+
123+
public getReciprocateQrCodeCallbacks(): ShowQrCodeCallbacks | null {
124+
return this.reciprocateQREvent ?? null;
125+
}
122126
}
123127

124128
const CODE_VERSION = 0x02; // the version of binary QR codes we support

src/crypto/verification/SAS.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,8 @@ export class SAS extends Base {
480480
}
481481
});
482482
}
483+
484+
public getShowSasCallbacks(): ShowSasCallbacks | null {
485+
return this.sasEvent ?? null;
486+
}
483487
}

0 commit comments

Comments
 (0)