1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

ElementR: Check key backup when user identity changes (#3760)

Fixes vector-im/element-web#26244
This commit is contained in:
Florian Duros
2023-10-03 14:38:51 +02:00
committed by GitHub
parent 6a761af867
commit 2e4276437a
2 changed files with 44 additions and 0 deletions

View File

@@ -850,6 +850,44 @@ describe("RustCrypto", () => {
expect(userVerificationStatus.wasCrossSigningVerified()).toBeFalsy();
});
});
describe("onUserIdentityUpdated", () => {
it("raises KeyBackupStatus event when identify change", async () => {
const mockHttpApi = new MatrixHttpApi(new TypedEventEmitter<HttpApiEvent, HttpApiEventHandlerMap>(), {
baseUrl: "http://server/",
prefix: "",
onlyData: true,
});
const olmMachine = {
getIdentity: jest.fn(),
// Force the backup to be trusted by the olmMachine
verifyBackup: jest.fn().mockResolvedValue({ trusted: jest.fn().mockReturnValue(true) }),
isBackupEnabled: jest.fn().mockReturnValue(true),
getBackupKeys: jest.fn(),
enableBackupV1: jest.fn(),
} as unknown as Mocked<RustSdkCryptoJs.OlmMachine>;
const rustCrypto = new RustCrypto(
olmMachine,
mockHttpApi,
testData.TEST_USER_ID,
testData.TEST_DEVICE_ID,
{} as ServerSideSecretStorage,
{} as CryptoCallbacks,
);
// Return the key backup
fetchMock.get("path:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA);
// Wait for the key backup to be available
const keyBackupStatusPromise = new Promise<boolean>((resolve) =>
rustCrypto.once(CryptoEvent.KeyBackupStatus, resolve),
);
await rustCrypto.onUserIdentityUpdated(new RustSdkCryptoJs.UserId(testData.TEST_USER_ID));
expect(await keyBackupStatusPromise).toBe(true);
});
});
});
/** build a basic RustCrypto instance for testing

View File

@@ -1339,6 +1339,12 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
public async onUserIdentityUpdated(userId: RustSdkCryptoJs.UserId): Promise<void> {
const newVerification = await this.getUserVerificationStatus(userId.toString());
this.emit(CryptoEvent.UserTrustStatusChanged, userId.toString(), newVerification);
// If our own user identity has changed, we may now trust the key backup where we did not before.
// So, re-check the key backup status and enable it if available.
if (userId.toString() === this.userId) {
await this.checkKeyBackupAndEnable();
}
}
/**