1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Make some OlmDevice megolm methods async

* OlmDevice.hasInboundSessionKeys
* OlmDevice.getInboundGroupSessionKey

The latter means that MegolmDecryption.shareKeysWithDevice takes longer before
it sends out the keyshare, so means the unit test needed an update
This commit is contained in:
Richard van der Hoff
2017-08-10 15:01:56 +01:00
parent 337c9cbea3
commit 8a0f73bf81
3 changed files with 22 additions and 16 deletions

View File

@@ -129,12 +129,16 @@ describe("MegolmDecryption", function() {
const deviceInfo = {}; const deviceInfo = {};
mockCrypto.getStoredDevice.andReturn(deviceInfo); mockCrypto.getStoredDevice.andReturn(deviceInfo);
const awaitEnsureSessions = new Promise((res, rej) => { mockOlmLib.ensureOlmSessionsForDevices.andReturn(
mockOlmLib.ensureOlmSessionsForDevices.andCall(() => { Promise.resolve({'@alice:foo': {'alidevice': {
res();
return Promise.resolve({'@alice:foo': {'alidevice': {
sessionId: 'alisession', sessionId: 'alisession',
}}}); }}}),
);
const awaitEncryptForDevice = new Promise((res, rej) => {
mockOlmLib.encryptMessageForDevice.andCall(() => {
res();
return Promise.resolve();
}); });
}); });
@@ -144,7 +148,7 @@ describe("MegolmDecryption", function() {
megolmDecryption.shareKeysWithDevice(keyRequest); megolmDecryption.shareKeysWithDevice(keyRequest);
// it's asynchronous, so we have to wait a bit // it's asynchronous, so we have to wait a bit
return awaitEnsureSessions; return awaitEncryptForDevice;
}).then(() => { }).then(() => {
// check that it called encryptMessageForDevice with // check that it called encryptMessageForDevice with
// appropriate args. // appropriate args.

View File

@@ -849,9 +849,9 @@ OlmDevice.prototype.decryptGroupMessage = async function(
* @param {string} senderKey base64-encoded curve25519 key of the sender * @param {string} senderKey base64-encoded curve25519 key of the sender
* @param {sring} sessionId session identifier * @param {sring} sessionId session identifier
* *
* @returns {boolean} true if we have the keys to this session * @returns {Promise<boolean>} true if we have the keys to this session
*/ */
OlmDevice.prototype.hasInboundSessionKeys = function(roomId, senderKey, sessionId) { OlmDevice.prototype.hasInboundSessionKeys = async function(roomId, senderKey, sessionId) {
const s = this._sessionStore.getEndToEndInboundGroupSession( const s = this._sessionStore.getEndToEndInboundGroupSession(
senderKey, sessionId, senderKey, sessionId,
); );
@@ -880,14 +880,16 @@ OlmDevice.prototype.hasInboundSessionKeys = function(roomId, senderKey, sessionI
* @param {string} senderKey base64-encoded curve25519 key of the sender * @param {string} senderKey base64-encoded curve25519 key of the sender
* @param {string} sessionId session identifier * @param {string} sessionId session identifier
* *
* @returns {{chain_index: number, key: string, * @returns {Promise<{chain_index: number, key: string,
* forwarding_curve25519_key_chain: Array<string>, * forwarding_curve25519_key_chain: Array<string>,
* sender_claimed_ed25519_key: string * sender_claimed_ed25519_key: string
* }} * }>}
* details of the session key. The key is a base64-encoded megolm key in * details of the session key. The key is a base64-encoded megolm key in
* export format. * export format.
*/ */
OlmDevice.prototype.getInboundGroupSessionKey = function(roomId, senderKey, sessionId) { OlmDevice.prototype.getInboundGroupSessionKey = async function(
roomId, senderKey, sessionId,
) {
function getKey(session, sessionData) { function getKey(session, sessionData) {
const messageIndex = session.first_known_index(); const messageIndex = session.first_known_index();

View File

@@ -725,7 +725,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
/** /**
* @inheritdoc * @inheritdoc
*/ */
MegolmDecryption.prototype.hasKeysForKeyRequest = async function(keyRequest) { MegolmDecryption.prototype.hasKeysForKeyRequest = function(keyRequest) {
const body = keyRequest.requestBody; const body = keyRequest.requestBody;
return this._olmDevice.hasInboundSessionKeys( return this._olmDevice.hasInboundSessionKeys(
@@ -766,10 +766,10 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
+ userId + ":" + deviceId, + userId + ":" + deviceId,
); );
const payload = this._buildKeyForwardingMessage( return this._buildKeyForwardingMessage(
body.room_id, body.sender_key, body.session_id, body.room_id, body.sender_key, body.session_id,
); );
}).then((payload) => {
const encryptedContent = { const encryptedContent = {
algorithm: olmlib.OLM_ALGORITHM, algorithm: olmlib.OLM_ALGORITHM,
sender_key: this._olmDevice.deviceCurve25519Key, sender_key: this._olmDevice.deviceCurve25519Key,
@@ -797,10 +797,10 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
}).done(); }).done();
}; };
MegolmDecryption.prototype._buildKeyForwardingMessage = function( MegolmDecryption.prototype._buildKeyForwardingMessage = async function(
roomId, senderKey, sessionId, roomId, senderKey, sessionId,
) { ) {
const key = this._olmDevice.getInboundGroupSessionKey( const key = await this._olmDevice.getInboundGroupSessionKey(
roomId, senderKey, sessionId, roomId, senderKey, sessionId,
); );