Skip to content

Commit

Permalink
Element-R: Implement requestOwnUserVerification
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Jun 23, 2023
1 parent 40498f0 commit 7c8ce9b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
38 changes: 38 additions & 0 deletions spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import fetchMock from "fetch-mock-jest";
import { MockResponse } from "fetch-mock";
import "fake-indexeddb/auto";

import { IDBFactory } from "fake-indexeddb";

import { createClient, CryptoEvent, MatrixClient } from "../../../src";
import {
canAcceptVerificationRequest,
Expand Down Expand Up @@ -67,6 +69,13 @@ beforeAll(async () => {
await global.Olm.init();
});

afterEach(() => {
// reset fake-indexeddb after each test, to make sure we don't leak connections
// cf https://github.com/dumbmatter/fakeIndexedDB#wipingresetting-the-indexeddb-for-a-fresh-state
// eslint-disable-next-line no-global-assign
indexedDB = new IDBFactory();
});

// restore the original global.crypto
afterAll(() => {
if (previousCrypto === undefined) {
Expand Down Expand Up @@ -304,6 +313,35 @@ function runTests(backend: string, initCrypto: InitCrypto, methods: string[] | u
olmSAS.free();
});

it("Can make a verification request to *all* devices", async () => {
// we need an existing cross-signing key for this
e2eKeyResponder.addCrossSigningData(SIGNED_CROSS_SIGNING_KEYS_DATA);
await waitForDeviceList();

// have alice initiate a verification. She should send a m.key.verification.request
const [requestBody, request] = await Promise.all([
expectSendToDeviceMessage("m.key.verification.request"),
aliceClient.getCrypto()!.requestOwnUserVerification(),
]);

const transactionId = request.transactionId;
expect(transactionId).toBeDefined();
expect(request.phase).toEqual(VerificationPhase.Requested);

// and now the request should be visible via `getVerificationRequestsToDeviceInProgress`
{
const requests = aliceClient.getCrypto()!.getVerificationRequestsToDeviceInProgress(TEST_USER_ID);
expect(requests.length).toEqual(1);
expect(requests[0].transactionId).toEqual(transactionId);
}

// legacy crypto picks devices individually; rust crypto uses a broadcast message
const toDeviceMessage =
requestBody.messages[TEST_USER_ID]["*"] ?? requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
expect(toDeviceMessage.from_device).toEqual(aliceClient.deviceId);
expect(toDeviceMessage.transaction_id).toEqual(transactionId);
});

oldBackendOnly("can verify another via QR code with an untrusted cross-signing key", async () => {
// QRCode fails if we don't yet have the cross-signing keys, so make sure we have them now.
e2eKeyResponder.addCrossSigningData(SIGNED_CROSS_SIGNING_KEYS_DATA);
Expand Down
11 changes: 6 additions & 5 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,18 +580,19 @@ export class RustCrypto implements CryptoBackend {
* @returns a VerificationRequest when the request has been sent to the other party.
*/
public async requestOwnUserVerification(): Promise<VerificationRequest> {
/* something like this, but currently untested
const user: RustSdkCryptoJs.OwnUserIdentity = await this.olmMachine.getIdentity(
const userIdentity: RustSdkCryptoJs.OwnUserIdentity | undefined = await this.olmMachine.getIdentity(
new RustSdkCryptoJs.UserId(this.userId),
);
if (userIdentity === undefined) {
throw new Error("cannot request verification for this device when there is no existing cross-signing key");
}

const [request, outgoingRequest]: [RustSdkCryptoJs.VerificationRequest, RustSdkCryptoJs.ToDeviceRequest] =
await user.requestVerification(
await userIdentity.requestVerification(
this.supportedVerificationMethods?.map(verificationMethodIdentifierToMethod),
);
await this.outgoingRequestProcessor.makeOutgoingRequest(outgoingRequest);
return new RustVerificationRequest(request, this.outgoingRequestProcessor);
*/
throw new Error("not implemented");
}

/**
Expand Down

0 comments on commit 7c8ce9b

Please sign in to comment.