Skip to content

Commit

Permalink
Add getShowSasCallbacks, getShowQrCodeCallbacks to VerifierBase (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
richvdh authored and toger5 committed Jun 7, 2023
1 parent a21736c commit 9afad10
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -320,13 +325,18 @@ 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();
const reciprocateQRCodeCallbacks = await new Promise<ShowQrCodeCallbacks>((resolve) => {
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();

Expand Down
27 changes: 26 additions & 1 deletion src/crypto/verification/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/crypto/verification/QRCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/verification/SAS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,8 @@ export class SAS extends Base {
}
});
}

public getShowSasCallbacks(): ShowSasCallbacks | null {
return this.sasEvent ?? null;
}
}

0 comments on commit 9afad10

Please sign in to comment.