Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to get outgoing room key requests for a given event #2930

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions spec/integ/matrix-client-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,4 +679,45 @@ describe("MatrixClient crypto", () => {
});
await httpBackend.flushAllExpected();
});

it("Checks for outgoing room key requests for a given event's session", async () => {
const eventA0 = new MatrixEvent({
sender: "@bob:example.com",
room_id: "!someroom",
content: {
algorithm: 'm.megolm.v1.aes-sha2',
session_id: "sessionid",
sender_key: "senderkey",
},
});
const eventA1 = new MatrixEvent({
sender: "@bob:example.com",
room_id: "!someroom",
content: {
algorithm: 'm.megolm.v1.aes-sha2',
session_id: "sessionid",
sender_key: "senderkey",
},
});
const eventB = new MatrixEvent({
sender: "@bob:example.com",
room_id: "!someroom",
content: {
algorithm: 'm.megolm.v1.aes-sha2',
session_id: "othersessionid",
sender_key: "senderkey",
},
});
const nonEncryptedEvent = new MatrixEvent({
sender: "@bob:example.com",
room_id: "!someroom",
content: {},
});

aliTestClient.client.crypto?.onSyncCompleted({});
await aliTestClient.client.cancelAndResendEventRoomKeyRequest(eventA0);
expect(await aliTestClient.client.getOutgoingRoomKeyRequest(eventA1)).not.toBeNull();
expect(await aliTestClient.client.getOutgoingRoomKeyRequest(eventB)).toBeNull();
expect(await aliTestClient.client.getOutgoingRoomKeyRequest(nonEncryptedEvent)).toBeNull();
});
});
29 changes: 28 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import {
IMegolmSessionData,
isCryptoAvailable,
VerificationMethod,
IRoomKeyRequestBody,
} from './crypto';
import { DeviceInfo, IDevice } from "./crypto/deviceinfo";
import { decodeRecoveryKey } from './crypto/recoverykey';
Expand Down Expand Up @@ -184,7 +185,7 @@ import {
RuleId,
} from "./@types/PushRules";
import { IThreepid } from "./@types/threepids";
import { CryptoStore } from "./crypto/store/base";
import { CryptoStore, OutgoingRoomKeyRequest } from "./crypto/store/base";
import {
GroupCall,
IGroupCallDataChannelOptions,
Expand Down Expand Up @@ -2631,6 +2632,32 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return device.isVerified();
}

/**
* Get outgoing room key request for this event if there is one.
* @param {MatrixEvent} event The event to check for
*
* @return {Promise} A room key request, or null if there is none
*/
public getOutgoingRoomKeyRequest(event: MatrixEvent): Promise<OutgoingRoomKeyRequest | null> {
if (!this.crypto) {
throw new Error("End-to-End encryption disabled");
}
const wireContent = event.getWireContent();
const requestBody: IRoomKeyRequestBody = {
session_id: wireContent.session_id,
sender_key: wireContent.sender_key,
algorithm: wireContent.algorithm,
room_id: event.getRoomId()!,
};
if (
!requestBody.session_id
|| !requestBody.sender_key
|| !requestBody.algorithm
|| !requestBody.room_id
) return Promise.resolve(null);
return this.crypto.cryptoStore.getOutgoingRoomKeyRequest(requestBody);
}

/**
* Cancel a room key request for this event if one is ongoing and resend the
* request.
Expand Down