Skip to content

Commit

Permalink
Add method to get outgoing room key requests for a given event (#2930)
Browse files Browse the repository at this point in the history
* Add method to get outgoing room key requests for a given event

* Write test, fix typo

* Add test case for non-encrypted event
  • Loading branch information
duxovni authored Dec 1, 2022
1 parent a0f3e5d commit 3870e33
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
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

0 comments on commit 3870e33

Please sign in to comment.