Use non-deprecated method of re-processing MatrixRTCSession keys (#2646)

* Handle case of encryption key for an index to be undefined

As per https://github.com/matrix-org/matrix-js-sdk/pull/4423 the key can be undefined and so we should handle this rather than waiting for SubtleCrypto.importKey() to fail.

* Use release version of matrix-js-sdk

Diff is baa6d13506...v34.7.0

* Use RTCSession. reemitEncryptionKeys()

* Add some test coverage whilst we are here

* Add some test coverage whilst we are here

* Lint
This commit is contained in:
Hugh Nimmo-Smith
2024-10-11 16:34:45 +01:00
committed by GitHub
parent 8272c54177
commit 8a84c6c45e
4 changed files with 79 additions and 13 deletions

View File

@@ -0,0 +1,72 @@
/*
Copyright 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
*/
import { describe, expect, test, vi } from "vitest";
import {
MatrixRTCSession,
MatrixRTCSessionEvent,
} from "matrix-js-sdk/src/matrixrtc";
import { KeyProviderEvent } from "livekit-client";
import { MatrixKeyProvider } from "./matrixKeyProvider";
function mockRTCSession(): MatrixRTCSession {
return {
on: vi.fn(),
off: vi.fn(),
reemitEncryptionKeys: vi.fn(),
} as unknown as MatrixRTCSession;
}
describe("matrixKeyProvider", () => {
test("initializes", () => {
const keyProvider = new MatrixKeyProvider();
expect(keyProvider).toBeTruthy();
});
test("listens for key requests and emits existing keys", () => {
const keyProvider = new MatrixKeyProvider();
const session = mockRTCSession();
keyProvider.setRTCSession(session);
expect(session.on).toHaveBeenCalledWith(
MatrixRTCSessionEvent.EncryptionKeyChanged,
expect.any(Function),
);
expect(session.off).not.toHaveBeenCalled();
});
test("stops listening when session changes", () => {
const keyProvider = new MatrixKeyProvider();
const session1 = mockRTCSession();
const session2 = mockRTCSession();
keyProvider.setRTCSession(session1);
expect(session1.off).not.toHaveBeenCalled();
keyProvider.setRTCSession(session2);
expect(session1.off).toHaveBeenCalledWith(
MatrixRTCSessionEvent.EncryptionKeyChanged,
expect.any(Function),
);
});
test("emits existing keys", () => {
const keyProvider = new MatrixKeyProvider();
const setKeyListener = vi.fn();
keyProvider.on(KeyProviderEvent.SetKey, setKeyListener);
const session = mockRTCSession();
keyProvider.setRTCSession(session);
expect(session.reemitEncryptionKeys).toHaveBeenCalled();
});
});

View File

@@ -35,15 +35,8 @@ export class MatrixKeyProvider extends BaseKeyProvider {
);
// The new session could be aware of keys of which the old session wasn't,
// so emit a key changed event.
for (const [
participant,
encryptionKeys,
] of this.rtcSession.getEncryptionKeys()) {
for (const [index, encryptionKey] of encryptionKeys.entries()) {
this.onEncryptionKeyChanged(encryptionKey, index, participant);
}
}
// so emit key changed events
this.rtcSession.reemitEncryptionKeys();
}
private onEncryptionKeyChanged = (