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

Make methods in crypto/algorithms async #519

Merged
merged 3 commits into from
Aug 9, 2017
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: 22 additions & 19 deletions spec/unit/crypto/algorithms/megolm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,32 @@ describe("MegolmDecryption", function() {
},
};

expect(megolmDecryption.hasKeysForKeyRequest(keyRequest))
.toBe(true);

// set up some pre-conditions for the share call
const deviceInfo = {};
mockCrypto.getStoredDevice.andReturn(deviceInfo);

const awaitEnsureSessions = new Promise((res, rej) => {
mockOlmLib.ensureOlmSessionsForDevices.andCall(() => {
res();
return Promise.resolve({'@alice:foo': {'alidevice': {
sessionId: 'alisession',
}}});
return megolmDecryption.hasKeysForKeyRequest(
keyRequest,
).then((hasKeys) => {
expect(hasKeys).toBe(true);

// set up some pre-conditions for the share call
const deviceInfo = {};
mockCrypto.getStoredDevice.andReturn(deviceInfo);

const awaitEnsureSessions = new Promise((res, rej) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You never return this from the promise chain which is returned to the test framework, so this test isn't waiting for all the stuff inside here to run.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore me, indentation on this diff threw me.

mockOlmLib.ensureOlmSessionsForDevices.andCall(() => {
res();
return Promise.resolve({'@alice:foo': {'alidevice': {
sessionId: 'alisession',
}}});
});
});
});

mockBaseApis.sendToDevice = expect.createSpy();
mockBaseApis.sendToDevice = expect.createSpy();

// do the share
megolmDecryption.shareKeysWithDevice(keyRequest);
// do the share
megolmDecryption.shareKeysWithDevice(keyRequest);

// it's asynchronous, so we have to wait a bit
return awaitEnsureSessions.then(() => {
// it's asynchronous, so we have to wait a bit
return awaitEnsureSessions;
}).then(() => {
// check that it called encryptMessageForDevice with
// appropriate args.
expect(mockOlmLib.encryptMessageForDevice.calls.length)
Expand Down
6 changes: 4 additions & 2 deletions src/crypto/algorithms/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ limitations under the License.
* @module
*/

import Promise from 'bluebird';

/**
* map of registered encryption algorithm classes. A map from string to {@link
* module:crypto/algorithms/base.EncryptionAlgorithm|EncryptionAlgorithm} class
Expand Down Expand Up @@ -143,11 +145,11 @@ class DecryptionAlgorithm {
* Determine if we have the keys necessary to respond to a room key request
*
* @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.
*/
hasKeysForKeyRequest(keyRequest) {
return false;
return Promise.resolve(false);
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/crypto/algorithms/megolm.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(devicesInRoom) {
// Updates `session` to hold the final OutboundSessionInfo.
//
// returns a promise which resolves once the keyshare is successful.
function prepareSession(oldSession) {
async function prepareSession(oldSession) {
session = oldSession;

// need to make a brand new session?
Expand All @@ -184,7 +184,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(devicesInRoom) {

if (!session) {
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
Expand Down Expand Up @@ -245,7 +245,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(devicesInRoom) {
*
* @return {module:crypto/algorithms/megolm.OutboundSessionInfo} session
*/
MegolmEncryption.prototype._prepareNewSession = function() {
MegolmEncryption.prototype._prepareNewSession = async function() {
const sessionId = this._olmDevice.createOutboundGroupSession();
const key = this._olmDevice.getOutboundGroupSessionKey(sessionId);

Expand Down Expand Up @@ -535,16 +535,14 @@ utils.inherits(MegolmDecryption, base.DecryptionAlgorithm);
* `algorithms.DecryptionError` if there is a problem decrypting the event.
*/
MegolmDecryption.prototype.decryptEvent = function(event) {
return Promise.try(() => {
this._decryptEvent(event, true);
});
return this._decryptEvent(event, true);
};


// helper for the real decryptEvent and for _retryDecryption. If
// requestKeysOnFail is true, we'll send an m.room_key_request when we fail
// 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();

if (!content.sender_key || !content.session_id ||
Expand Down Expand Up @@ -721,7 +719,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
/**
* @inheritdoc
*/
MegolmDecryption.prototype.hasKeysForKeyRequest = function(keyRequest) {
MegolmDecryption.prototype.hasKeysForKeyRequest = async function(keyRequest) {
const body = keyRequest.requestBody;

return this._olmDevice.hasInboundSessionKeys(
Expand Down
6 changes: 4 additions & 2 deletions src/crypto/algorithms/olm.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
let payloadString;

try {
payloadString = this._decryptMessage(deviceKey, message);
payloadString = await this._decryptMessage(deviceKey, message);
} catch (e) {
throw new base.DecryptionError(
"Bad Encrypted Message", {
Expand Down Expand Up @@ -235,7 +235,9 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
*
* @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);

// try each session in turn.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ Crypto.prototype._processReceivedRoomKeyRequest = async function(req) {
return;
}

if (!decryptor.hasKeysForKeyRequest(req)) {
if (!await decryptor.hasKeysForKeyRequest(req)) {
console.log(
`room key request for unknown session ${roomId} / ` +
body.session_id,
Expand Down