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

Merge remote-tracking branch 'origin/develop' into rav/async_crypto/olmlib

This commit is contained in:
Richard van der Hoff
2017-08-09 18:11:48 +01:00
6 changed files with 42 additions and 37 deletions

View File

@@ -120,29 +120,32 @@ describe("MegolmDecryption", function() {
}, },
}; };
expect(megolmDecryption.hasKeysForKeyRequest(keyRequest)) return megolmDecryption.hasKeysForKeyRequest(
.toBe(true); keyRequest,
).then((hasKeys) => {
expect(hasKeys).toBe(true);
// set up some pre-conditions for the share call // set up some pre-conditions for the share call
const deviceInfo = {}; const deviceInfo = {};
mockCrypto.getStoredDevice.andReturn(deviceInfo); mockCrypto.getStoredDevice.andReturn(deviceInfo);
const awaitEnsureSessions = new Promise((res, rej) => { const awaitEnsureSessions = new Promise((res, rej) => {
mockOlmLib.ensureOlmSessionsForDevices.andCall(() => { mockOlmLib.ensureOlmSessionsForDevices.andCall(() => {
res(); res();
return Promise.resolve({'@alice:foo': {'alidevice': { return Promise.resolve({'@alice:foo': {'alidevice': {
sessionId: 'alisession', sessionId: 'alisession',
}}}); }}});
});
}); });
});
mockBaseApis.sendToDevice = expect.createSpy(); mockBaseApis.sendToDevice = expect.createSpy();
// do the share // do the share
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.then(() => { return awaitEnsureSessions;
}).then(() => {
// check that it called encryptMessageForDevice with // check that it called encryptMessageForDevice with
// appropriate args. // appropriate args.
expect(mockOlmLib.encryptMessageForDevice.calls.length) expect(mockOlmLib.encryptMessageForDevice.calls.length)

View File

@@ -462,7 +462,7 @@ class DeviceListUpdateSerialiser {
let prom = Promise.resolve(); let prom = Promise.resolve();
for (const userId of downloadUsers) { for (const userId of downloadUsers) {
prom = prom.delay(5).then(() => { prom = prom.delay(5).then(() => {
this._processQueryResponseForUser(userId, dk[userId]); return this._processQueryResponseForUser(userId, dk[userId]);
}); });
} }
@@ -486,7 +486,7 @@ class DeviceListUpdateSerialiser {
return deferred.promise; return deferred.promise;
} }
_processQueryResponseForUser(userId, response) { async _processQueryResponseForUser(userId, response) {
console.log('got keys for ' + userId + ':', response); console.log('got keys for ' + userId + ':', response);
// map from deviceid -> deviceinfo for this user // map from deviceid -> deviceinfo for this user
@@ -499,7 +499,7 @@ class DeviceListUpdateSerialiser {
}); });
} }
_updateStoredDeviceKeysForUser( await _updateStoredDeviceKeysForUser(
this._olmDevice, userId, userStore, response || {}, this._olmDevice, userId, userStore, response || {},
); );
@@ -516,7 +516,7 @@ class DeviceListUpdateSerialiser {
} }
function _updateStoredDeviceKeysForUser(_olmDevice, userId, userStore, async function _updateStoredDeviceKeysForUser(_olmDevice, userId, userStore,
userResult) { userResult) {
let updated = false; let updated = false;
@@ -554,7 +554,7 @@ function _updateStoredDeviceKeysForUser(_olmDevice, userId, userStore,
continue; continue;
} }
if (_storeDeviceKeys(_olmDevice, userStore, deviceResult)) { if (await _storeDeviceKeys(_olmDevice, userStore, deviceResult)) {
updated = true; updated = true;
} }
} }
@@ -565,9 +565,9 @@ function _updateStoredDeviceKeysForUser(_olmDevice, userId, userStore,
/* /*
* Process a device in a /query response, and add it to the userStore * Process a device in a /query response, and add it to the userStore
* *
* returns true if a change was made, else false * returns (a promise for) true if a change was made, else false
*/ */
function _storeDeviceKeys(_olmDevice, userStore, deviceResult) { async function _storeDeviceKeys(_olmDevice, userStore, deviceResult) {
if (!deviceResult.keys) { if (!deviceResult.keys) {
// no keys? // no keys?
return false; return false;

View File

@@ -20,6 +20,8 @@ limitations under the License.
* @module * @module
*/ */
import Promise from 'bluebird';
/** /**
* map of registered encryption algorithm classes. A map from string to {@link * map of registered encryption algorithm classes. A map from string to {@link
* module:crypto/algorithms/base.EncryptionAlgorithm|EncryptionAlgorithm} class * module:crypto/algorithms/base.EncryptionAlgorithm|EncryptionAlgorithm} class
@@ -143,11 +145,11 @@ class DecryptionAlgorithm {
* Determine if we have the keys necessary to respond to a room key request * Determine if we have the keys necessary to respond to a room key request
* *
* @param {module:crypto~IncomingRoomKeyRequest} keyRequest * @param {module:crypto~IncomingRoomKeyRequest} keyRequest
* @return {boolean} true if we have the keys and could (theoretically) share * @return {Promise<boolean>} true if we have the keys and could (theoretically) share
* them; else false. * them; else false.
*/ */
hasKeysForKeyRequest(keyRequest) { hasKeysForKeyRequest(keyRequest) {
return false; return Promise.resolve(false);
} }
/** /**

View File

@@ -166,7 +166,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(devicesInRoom) {
// Updates `session` to hold the final OutboundSessionInfo. // Updates `session` to hold the final OutboundSessionInfo.
// //
// returns a promise which resolves once the keyshare is successful. // returns a promise which resolves once the keyshare is successful.
function prepareSession(oldSession) { async function prepareSession(oldSession) {
session = oldSession; session = oldSession;
// need to make a brand new session? // need to make a brand new session?
@@ -184,7 +184,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(devicesInRoom) {
if (!session) { if (!session) {
console.log(`Starting new megolm session for room ${self._roomId}`); console.log(`Starting new megolm session for room ${self._roomId}`);
session = self._prepareNewSession(); session = await self._prepareNewSession();
} }
// now check if we need to share with any devices // now check if we need to share with any devices
@@ -245,7 +245,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(devicesInRoom) {
* *
* @return {module:crypto/algorithms/megolm.OutboundSessionInfo} session * @return {module:crypto/algorithms/megolm.OutboundSessionInfo} session
*/ */
MegolmEncryption.prototype._prepareNewSession = function() { MegolmEncryption.prototype._prepareNewSession = async function() {
const sessionId = this._olmDevice.createOutboundGroupSession(); const sessionId = this._olmDevice.createOutboundGroupSession();
const key = this._olmDevice.getOutboundGroupSessionKey(sessionId); const key = this._olmDevice.getOutboundGroupSessionKey(sessionId);
@@ -539,16 +539,14 @@ utils.inherits(MegolmDecryption, base.DecryptionAlgorithm);
* `algorithms.DecryptionError` if there is a problem decrypting the event. * `algorithms.DecryptionError` if there is a problem decrypting the event.
*/ */
MegolmDecryption.prototype.decryptEvent = function(event) { MegolmDecryption.prototype.decryptEvent = function(event) {
return Promise.try(() => { return this._decryptEvent(event, true);
this._decryptEvent(event, true);
});
}; };
// helper for the real decryptEvent and for _retryDecryption. If // helper for the real decryptEvent and for _retryDecryption. If
// requestKeysOnFail is true, we'll send an m.room_key_request when we fail // requestKeysOnFail is true, we'll send an m.room_key_request when we fail
// to decrypt the event due to missing megolm keys. // to decrypt the event due to missing megolm keys.
MegolmDecryption.prototype._decryptEvent = function(event, requestKeysOnFail) { MegolmDecryption.prototype._decryptEvent = async function(event, requestKeysOnFail) {
const content = event.getWireContent(); const content = event.getWireContent();
if (!content.sender_key || !content.session_id || if (!content.sender_key || !content.session_id ||
@@ -725,7 +723,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
/** /**
* @inheritdoc * @inheritdoc
*/ */
MegolmDecryption.prototype.hasKeysForKeyRequest = function(keyRequest) { MegolmDecryption.prototype.hasKeysForKeyRequest = async function(keyRequest) {
const body = keyRequest.requestBody; const body = keyRequest.requestBody;
return this._olmDevice.hasInboundSessionKeys( return this._olmDevice.hasInboundSessionKeys(

View File

@@ -176,7 +176,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
let payloadString; let payloadString;
try { try {
payloadString = this._decryptMessage(deviceKey, message); payloadString = await this._decryptMessage(deviceKey, message);
} catch (e) { } catch (e) {
throw new base.DecryptionError( throw new base.DecryptionError(
"Bad Encrypted Message", { "Bad Encrypted Message", {
@@ -239,7 +239,9 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
* *
* @return {string} payload, if decrypted successfully. * @return {string} payload, if decrypted successfully.
*/ */
OlmDecryption.prototype._decryptMessage = function(theirDeviceIdentityKey, message) { OlmDecryption.prototype._decryptMessage = async function(
theirDeviceIdentityKey, message,
) {
const sessionIds = this._olmDevice.getSessionIdsForDevice(theirDeviceIdentityKey); const sessionIds = this._olmDevice.getSessionIdsForDevice(theirDeviceIdentityKey);
// try each session in turn. // try each session in turn.

View File

@@ -1131,7 +1131,7 @@ Crypto.prototype._processReceivedRoomKeyRequest = async function(req) {
return; return;
} }
if (!decryptor.hasKeysForKeyRequest(req)) { if (!await decryptor.hasKeysForKeyRequest(req)) {
console.log( console.log(
`room key request for unknown session ${roomId} / ` + `room key request for unknown session ${roomId} / ` +
body.session_id, body.session_id,