You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-12 08:42:46 +03:00
E2E: Download our own devicelist on startup
Make sure we get a list of our own devices when starting a new client. Fixes https://github.com/vector-im/riot-web/issues/2676.
This commit is contained in:
@@ -55,8 +55,8 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
|
|||||||
this._deviceId = deviceId;
|
this._deviceId = deviceId;
|
||||||
|
|
||||||
this._initialSyncCompleted = false;
|
this._initialSyncCompleted = false;
|
||||||
// userId -> deviceId -> true
|
// userId -> true
|
||||||
this._pendingNewDevices = {};
|
this._pendingUsersWithNewDevices = {};
|
||||||
|
|
||||||
this._olmDevice = new OlmDevice(sessionStore);
|
this._olmDevice = new OlmDevice(sessionStore);
|
||||||
|
|
||||||
@@ -77,19 +77,30 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
|
|||||||
this._deviceKeys["curve25519:" + this._deviceId] =
|
this._deviceKeys["curve25519:" + this._deviceId] =
|
||||||
this._olmDevice.deviceCurve25519Key;
|
this._olmDevice.deviceCurve25519Key;
|
||||||
|
|
||||||
|
var myDevices = this._sessionStore.getEndToEndDevicesForUser(
|
||||||
|
this._userId
|
||||||
|
);
|
||||||
|
|
||||||
|
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
|
// add our own deviceinfo to the sessionstore
|
||||||
var deviceInfo = {
|
var deviceInfo = {
|
||||||
keys: this._deviceKeys,
|
keys: this._deviceKeys,
|
||||||
algorithms: this._supportedAlgorithms,
|
algorithms: this._supportedAlgorithms,
|
||||||
verified: DeviceVerification.VERIFIED,
|
verified: DeviceVerification.VERIFIED,
|
||||||
};
|
};
|
||||||
var myDevices = this._sessionStore.getEndToEndDevicesForUser(
|
|
||||||
this._userId
|
|
||||||
) || {};
|
|
||||||
myDevices[this._deviceId] = deviceInfo;
|
myDevices[this._deviceId] = deviceInfo;
|
||||||
this._sessionStore.storeEndToEndDevicesForUser(
|
this._sessionStore.storeEndToEndDevicesForUser(
|
||||||
this._userId, myDevices
|
this._userId, myDevices
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
_registerEventHandlers(this, eventEmitter);
|
_registerEventHandlers(this, eventEmitter);
|
||||||
|
|
||||||
@@ -1134,8 +1145,7 @@ Crypto.prototype._onNewDeviceEvent = function(event) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._pendingNewDevices[userId] = this._pendingNewDevices[userId] || {};
|
this._pendingUsersWithNewDevices[userId] = true;
|
||||||
this._pendingNewDevices[userId][deviceId] = true;
|
|
||||||
|
|
||||||
// we delay handling these until the intialsync has completed, so that we
|
// we delay handling these until the intialsync has completed, so that we
|
||||||
// can do all of them together.
|
// can do all of them together.
|
||||||
@@ -1150,10 +1160,7 @@ Crypto.prototype._onNewDeviceEvent = function(event) {
|
|||||||
Crypto.prototype._flushNewDeviceRequests = function() {
|
Crypto.prototype._flushNewDeviceRequests = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var pending = this._pendingNewDevices;
|
var users = utils.keys(this._pendingUsersWithNewDevices);
|
||||||
var users = utils.keys(pending).filter(function(u) {
|
|
||||||
return utils.keys(pending[u]).length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (users.length === 0) {
|
if (users.length === 0) {
|
||||||
return;
|
return;
|
||||||
@@ -1163,7 +1170,7 @@ Crypto.prototype._flushNewDeviceRequests = function() {
|
|||||||
|
|
||||||
// we've kicked off requests to these users: remove their
|
// we've kicked off requests to these users: remove their
|
||||||
// pending flag for now.
|
// pending flag for now.
|
||||||
this._pendingNewDevices = {};
|
this._pendingUsersWithNewDevices = {};
|
||||||
|
|
||||||
users.map(function(u) {
|
users.map(function(u) {
|
||||||
r[u] = r[u].catch(function(e) {
|
r[u] = r[u].catch(function(e) {
|
||||||
@@ -1175,8 +1182,7 @@ Crypto.prototype._flushNewDeviceRequests = function() {
|
|||||||
// mean that we will do another download in the future, but won't
|
// mean that we will do another download in the future, but won't
|
||||||
// tight-loop.
|
// tight-loop.
|
||||||
//
|
//
|
||||||
self._pendingNewDevices[u] = self._pendingNewDevices[u] || {};
|
self._pendingUsersWithNewDevices[u] = true;
|
||||||
utils.update(self._pendingNewDevices[u], pending[u]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -383,6 +383,15 @@ function recvMessage(httpBackend, client, sender, message) {
|
|||||||
|
|
||||||
function aliStartClient() {
|
function aliStartClient() {
|
||||||
expectAliKeyUpload().catch(test_utils.failTest);
|
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);
|
startClient(aliHttpBackend, aliClient);
|
||||||
return aliHttpBackend.flush().then(function() {
|
return aliHttpBackend.flush().then(function() {
|
||||||
console.log("Ali client started");
|
console.log("Ali client started");
|
||||||
@@ -391,6 +400,15 @@ function aliStartClient() {
|
|||||||
|
|
||||||
function bobStartClient() {
|
function bobStartClient() {
|
||||||
expectBobKeyUpload().catch(test_utils.failTest);
|
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);
|
startClient(bobHttpBackend, bobClient);
|
||||||
return bobHttpBackend.flush().then(function() {
|
return bobHttpBackend.flush().then(function() {
|
||||||
console.log("Bob client started");
|
console.log("Bob client started");
|
||||||
|
@@ -59,12 +59,25 @@ function TestClient(userId, deviceId, accessToken) {
|
|||||||
/**
|
/**
|
||||||
* start the client, and wait for it to initialise.
|
* start the client, and wait for it to initialise.
|
||||||
*
|
*
|
||||||
|
* @param {object?} deviceQueryResponse the list of our existing devices to return from
|
||||||
|
* the /query request. Defaults to empty device list
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
TestClient.prototype.start = function() {
|
TestClient.prototype.start = function(existingDevices) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.httpBackend.when("GET", "/pushrules").respond(200, {});
|
this.httpBackend.when("GET", "/pushrules").respond(200, {});
|
||||||
this.httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
|
this.httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
|
||||||
|
|
||||||
|
this.httpBackend.when('POST', '/keys/query').respond(200, function(path, content) {
|
||||||
|
expect(content.device_keys[self.userId]).toEqual({});
|
||||||
|
var res = existingDevices;
|
||||||
|
if (!res) {
|
||||||
|
res = { device_keys: {} };
|
||||||
|
res.device_keys[self.userId] = {};
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
});
|
||||||
this.httpBackend.when("POST", "/keys/upload").respond(200, function(path, content) {
|
this.httpBackend.when("POST", "/keys/upload").respond(200, function(path, content) {
|
||||||
expect(content.one_time_keys).not.toBeDefined();
|
expect(content.one_time_keys).not.toBeDefined();
|
||||||
expect(content.device_keys).toBeDefined();
|
expect(content.device_keys).toBeDefined();
|
||||||
|
Reference in New Issue
Block a user