Skip to content

Commit 85e0ddf

Browse files
committed
Implement VerificationRequest.cancel
1 parent 326a13b commit 85e0ddf

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

spec/integ/crypto/verification.spec.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
290290
await verificationPromise;
291291
expect(request.phase).toEqual(VerificationPhase.Done);
292292

293+
// at this point, cancelling should do nothing.
294+
await request.cancel();
295+
expect(request.phase).toEqual(VerificationPhase.Done);
296+
293297
// we're done with the temporary keypair
294298
olmSAS.free();
295299
});
@@ -406,11 +410,41 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
406410
await verificationPromise;
407411
expect(request.phase).toEqual(VerificationPhase.Done);
408412
});
413+
});
414+
415+
describe("cancellation", () => {
416+
beforeEach(async () => {
417+
// pretend that we have another device, which we will start verifying
418+
e2eKeyResponder.addDeviceKeys(TEST_USER_ID, TEST_DEVICE_ID, SIGNED_TEST_DEVICE_DATA);
409419

410-
it("can cancel during the SAS phase", async () => {
411420
aliceClient = await startTestClient();
412421
await waitForDeviceList();
422+
});
423+
424+
it("can cancel during the Ready phase", async () => {
425+
// have alice initiate a verification. She should send a m.key.verification.request
426+
const [, request] = await Promise.all([
427+
expectSendToDeviceMessage("m.key.verification.request"),
428+
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
429+
]);
430+
const transactionId = request.transactionId!;
413431

432+
// The dummy device replies with an m.key.verification.ready...
433+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
434+
await waitForVerificationRequestChanged(request);
435+
436+
// now alice changes her mind
437+
const [requestBody] = await Promise.all([
438+
expectSendToDeviceMessage("m.key.verification.cancel"),
439+
request.cancel(),
440+
]);
441+
const toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
442+
expect(toDeviceMessage.transaction_id).toEqual(transactionId);
443+
expect(toDeviceMessage.code).toEqual("m.user");
444+
expect(request.phase).toEqual(VerificationPhase.Cancelled);
445+
});
446+
447+
it("can cancel during the SAS phase", async () => {
414448
// have alice initiate a verification. She should send a m.key.verification.request
415449
const [, request] = await Promise.all([
416450
expectSendToDeviceMessage("m.key.verification.request"),

src/crypto-api/verification.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export interface VerificationRequest
108108
* Cancels the request, sending a cancellation to the other party
109109
*
110110
* @param params - Details for the cancellation, including `reason` (defaults to "User declined"), and `code`
111-
* (defaults to `m.user`).
111+
* (defaults to `m.user`). **Deprecated**: this parameter is ignored by the Rust cryptography implementation.
112112
*
113113
* @returns Promise which resolves when the event has been sent.
114114
*/

src/rust-crypto/verification.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class RustVerificationRequest
4242

4343
public constructor(
4444
private readonly inner: RustSdkCryptoJs.VerificationRequest,
45-
outgoingRequestProcessor: OutgoingRequestProcessor,
45+
private readonly outgoingRequestProcessor: OutgoingRequestProcessor,
4646
) {
4747
super();
4848

@@ -210,8 +210,11 @@ export class RustVerificationRequest
210210
*
211211
* @returns Promise which resolves when the event has been sent.
212212
*/
213-
public cancel(params?: { reason?: string; code?: string }): Promise<void> {
214-
throw new Error("not implemented");
213+
public async cancel(params?: { reason?: string; code?: string }): Promise<void> {
214+
const req: undefined | OutgoingRequest = this.inner.cancel();
215+
if (req) {
216+
await this.outgoingRequestProcessor.makeOutgoingRequest(req);
217+
}
215218
}
216219

217220
/**

0 commit comments

Comments
 (0)