Skip to content

Commit

Permalink
ElementR: Check key backup when user identity changes (#3760)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros authored Oct 3, 2023
1 parent 6a761af commit 2e42764
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions spec/unit/rust-crypto/rust-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down

0 comments on commit 2e42764

Please sign in to comment.