Skip to content

Commit

Permalink
Merge pull request #302 from matrix-org/rav/send_to_our_devices
Browse files Browse the repository at this point in the history
E2E: Download our own devicelist on startup
  • Loading branch information
richvdh authored Dec 7, 2016
2 parents f997b4a + ec12484 commit 99089c0
Show file tree
Hide file tree
Showing 4 changed files with 424 additions and 253 deletions.
48 changes: 27 additions & 21 deletions lib/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
this._deviceId = deviceId;

this._initialSyncCompleted = false;
// userId -> deviceId -> true
this._pendingNewDevices = {};
// userId -> true
this._pendingUsersWithNewDevices = {};

this._olmDevice = new OlmDevice(sessionStore);

Expand All @@ -77,20 +77,31 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
this._deviceKeys["curve25519:" + this._deviceId] =
this._olmDevice.deviceCurve25519Key;

// add our own deviceinfo to the sessionstore
var deviceInfo = {
keys: this._deviceKeys,
algorithms: this._supportedAlgorithms,
verified: DeviceVerification.VERIFIED,
};
var myDevices = this._sessionStore.getEndToEndDevicesForUser(
this._userId
) || {};
myDevices[this._deviceId] = deviceInfo;
this._sessionStore.storeEndToEndDevicesForUser(
this._userId, myDevices
);

if (!myDevices) {
// we don't yet have a list of our own devices; make sure we
// get one when we flush the pendingUsersWithNewDevices.
this._pendingUsersWithNewDevices[this._userId] = true;
myDevices = {};
}

if (!myDevices[this._deviceId]) {
// add our own deviceinfo to the sessionstore
var deviceInfo = {
keys: this._deviceKeys,
algorithms: this._supportedAlgorithms,
verified: DeviceVerification.VERIFIED,
};

myDevices[this._deviceId] = deviceInfo;
this._sessionStore.storeEndToEndDevicesForUser(
this._userId, myDevices
);
}

_registerEventHandlers(this, eventEmitter);

// map from userId -> deviceId -> roomId -> timestamp
Expand Down Expand Up @@ -1134,8 +1145,7 @@ Crypto.prototype._onNewDeviceEvent = function(event) {
return;
}

this._pendingNewDevices[userId] = this._pendingNewDevices[userId] || {};
this._pendingNewDevices[userId][deviceId] = true;
this._pendingUsersWithNewDevices[userId] = true;

// we delay handling these until the intialsync has completed, so that we
// can do all of them together.
Expand All @@ -1150,10 +1160,7 @@ Crypto.prototype._onNewDeviceEvent = function(event) {
Crypto.prototype._flushNewDeviceRequests = function() {
var self = this;

var pending = this._pendingNewDevices;
var users = utils.keys(pending).filter(function(u) {
return utils.keys(pending[u]).length > 0;
});
var users = utils.keys(this._pendingUsersWithNewDevices);

if (users.length === 0) {
return;
Expand All @@ -1163,7 +1170,7 @@ Crypto.prototype._flushNewDeviceRequests = function() {

// we've kicked off requests to these users: remove their
// pending flag for now.
this._pendingNewDevices = {};
this._pendingUsersWithNewDevices = {};

users.map(function(u) {
r[u] = r[u].catch(function(e) {
Expand All @@ -1175,8 +1182,7 @@ Crypto.prototype._flushNewDeviceRequests = function() {
// mean that we will do another download in the future, but won't
// tight-loop.
//
self._pendingNewDevices[u] = self._pendingNewDevices[u] || {};
utils.update(self._pendingNewDevices[u], pending[u]);
self._pendingUsersWithNewDevices[u] = true;
});
});

Expand Down
3 changes: 2 additions & 1 deletion lib/crypto/olmlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ module.exports.ensureOlmSessionsForDevices = function(
return baseApis.claimOneTimeKeys(
devicesWithoutSession, oneTimeKeyAlgorithm
).then(function(res) {
var otk_res = res.one_time_keys || {};
for (var userId in devicesByUser) {
if (!devicesByUser.hasOwnProperty(userId)) { continue; }
var userRes = res.one_time_keys[userId] || {};
var userRes = otk_res[userId] || {};
var devices = devicesByUser[userId];
for (var j = 0; j < devices.length; j++) {
var deviceInfo = devices[j];
Expand Down
18 changes: 18 additions & 0 deletions spec/integ/matrix-client-crypto.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ function recvMessage(httpBackend, client, sender, message) {

function aliStartClient() {
expectAliKeyUpload().catch(test_utils.failTest);

// ali will try to query her own keys on start
aliHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) {
expect(content.device_keys[aliUserId]).toEqual({});
var result = {};
result[aliUserId] = {};
return {device_keys: result};
});

startClient(aliHttpBackend, aliClient);
return aliHttpBackend.flush().then(function() {
console.log("Ali client started");
Expand All @@ -391,6 +400,15 @@ function aliStartClient() {

function bobStartClient() {
expectBobKeyUpload().catch(test_utils.failTest);

// bob will try to query his own keys on start
bobHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) {
expect(content.device_keys[bobUserId]).toEqual({});
var result = {};
result[bobUserId] = {};
return {device_keys: result};
});

startClient(bobHttpBackend, bobClient);
return bobHttpBackend.flush().then(function() {
console.log("Bob client started");
Expand Down
Loading

0 comments on commit 99089c0

Please sign in to comment.