Skip to content

Commit 2294b8f

Browse files
committed
Implement VerificationRequest.cancel
1 parent 9f0c515 commit 2294b8f

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
@@ -277,6 +277,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
277277
await verificationPromise;
278278
expect(request.phase).toEqual(VerificationPhase.Done);
279279

280+
// at this point, cancelling should do nothing.
281+
await request.cancel();
282+
expect(request.phase).toEqual(VerificationPhase.Done);
283+
280284
// we're done with the temporary keypair
281285
olmSAS.free();
282286
});
@@ -393,11 +397,41 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
393397
await verificationPromise;
394398
expect(request.phase).toEqual(VerificationPhase.Done);
395399
});
400+
});
401+
402+
describe("cancellation", () => {
403+
beforeEach(async () => {
404+
// pretend that we have another device, which we will start verifying
405+
e2eKeyResponder.addDeviceKeys(TEST_USER_ID, TEST_DEVICE_ID, SIGNED_TEST_DEVICE_DATA);
396406

397-
it("can cancel during the SAS phase", async () => {
398407
aliceClient = await startTestClient();
399408
await waitForDeviceList();
409+
});
410+
411+
it("can cancel during the Ready phase", async () => {
412+
// have alice initiate a verification. She should send a m.key.verification.request
413+
const [, request] = await Promise.all([
414+
expectSendToDeviceMessage("m.key.verification.request"),
415+
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
416+
]);
417+
const transactionId = request.transactionId!;
400418

419+
// The dummy device replies with an m.key.verification.ready...
420+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
421+
await waitForVerificationRequestChanged(request);
422+
423+
// now alice changes her mind
424+
const [requestBody] = await Promise.all([
425+
expectSendToDeviceMessage("m.key.verification.cancel"),
426+
request.cancel(),
427+
]);
428+
const toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
429+
expect(toDeviceMessage.transaction_id).toEqual(transactionId);
430+
expect(toDeviceMessage.code).toEqual("m.user");
431+
expect(request.phase).toEqual(VerificationPhase.Cancelled);
432+
});
433+
434+
it("can cancel during the SAS phase", async () => {
401435
// have alice initiate a verification. She should send a m.key.verification.request
402436
const [, request] = await Promise.all([
403437
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)