1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Make a number of the crypto APIs asynchronous

Make the following return Promises:

* `MatrixClient.getStoredDevicesForUser`
* `MatrixClient.getStoredDevice`
* `MatrixClient.setDeviceVerified`
* `MatrixClient.setDeviceBlocked`
* `MatrixClient.setDeviceKnown`
* `MatrixClient.getEventSenderDeviceInfo`
* `MatrixClient.isEventSenderVerified`
* `MatrixClient.importRoomKeys`

Remove `listDeviceKeys` altogether: it's been deprecated for ages, and since
applications are going to have to be changed anyway, they might as well use its
replacement (`getStoredDevices`).
This commit is contained in:
Richard van der Hoff
2017-07-18 23:13:45 +01:00
parent d1e91cd702
commit 2ff9a36eed
3 changed files with 39 additions and 86 deletions

View File

@@ -145,13 +145,10 @@ function aliDownloadsKeys() {
expect(bobTestClient.getSigningKey()).toBeTruthy();
const p1 = aliTestClient.client.downloadKeys([bobUserId]).then(function() {
expect(aliTestClient.client.listDeviceKeys(bobUserId)).toEqual([{
id: "bvcxz",
key: bobTestClient.getSigningKey(),
verified: false,
blocked: false,
display_name: null,
}]);
return aliTestClient.client.getStoredDevicesForUser(bobUserId);
}).then((devices) => {
expect(devices.length).toEqual(1);
expect(devices[0].deviceId).toEqual("bvcxz");
});
const p2 = expectAliQueryKeys();
@@ -433,10 +430,11 @@ describe("MatrixClient crypto", function() {
aliTestClient.client.downloadKeys([bobUserId]),
expectAliQueryKeys(),
]);
})
.then(function() {
}).then(function() {
return aliTestClient.client.getStoredDevicesForUser(bobUserId);
}).then((devices) => {
// should get an empty list
expect(aliTestClient.client.listDeviceKeys(bobUserId)).toEqual([]);
expect(devices).toEqual([]);
})
.nodeify(done);
});
@@ -474,9 +472,14 @@ describe("MatrixClient crypto", function() {
aliTestClient.client.downloadKeys([bobUserId, eveUserId]),
aliTestClient.httpBackend.flush("/keys/query", 1),
]).then(function() {
return Promise.all([
aliTestClient.client.getStoredDevicesForUser(bobUserId),
aliTestClient.client.getStoredDevicesForUser(eveUserId),
]);
}).spread((bobDevices, eveDevices) => {
// should get an empty list
expect(aliTestClient.client.listDeviceKeys(bobUserId)).toEqual([]);
expect(aliTestClient.client.listDeviceKeys(eveUserId)).toEqual([]);
expect(bobDevices).toEqual([]);
expect(eveDevices).toEqual([]);
}).nodeify(done);
});
@@ -511,8 +514,10 @@ describe("MatrixClient crypto", function() {
aliTestClient.client.downloadKeys([bobUserId]),
aliTestClient.httpBackend.flush("/keys/query", 1),
]).then(function() {
return aliTestClient.client.getStoredDevicesForUser(bobUserId);
}).then((devices) => {
// should get an empty list
expect(aliTestClient.client.listDeviceKeys(bobUserId)).toEqual([]);
expect(devices).toEqual([]);
}).nodeify(done);
});

View File

@@ -427,31 +427,14 @@ MatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {
return this._crypto.downloadKeys(userIds, forceDownload);
};
/**
* List the stored device keys for a user id
*
* @deprecated prefer {@link module:client#getStoredDevicesForUser}
*
* @param {string} userId the user to list keys for.
*
* @return {object[]} list of devices with "id", "verified", "blocked",
* "key", and "display_name" parameters.
*/
MatrixClient.prototype.listDeviceKeys = function(userId) {
if (this._crypto === null) {
throw new Error("End-to-end encryption disabled");
}
return this._crypto.listDeviceKeys(userId);
};
/**
* Get the stored device keys for a user id
*
* @param {string} userId the user to list keys for.
*
* @return {module:crypto-deviceinfo[]} list of devices
* @return {Promise<module:crypto-deviceinfo[]>} list of devices
*/
MatrixClient.prototype.getStoredDevicesForUser = function(userId) {
MatrixClient.prototype.getStoredDevicesForUser = async function(userId) {
if (this._crypto === null) {
throw new Error("End-to-end encryption disabled");
}
@@ -464,9 +447,9 @@ MatrixClient.prototype.getStoredDevicesForUser = function(userId) {
* @param {string} userId the user to list keys for.
* @param {string} deviceId unique identifier for the device
*
* @return {?module:crypto-deviceinfo} device or null
* @return {Promise<?module:crypto-deviceinfo>} device or null
*/
MatrixClient.prototype.getStoredDevice = function(userId, deviceId) {
MatrixClient.prototype.getStoredDevice = async function(userId, deviceId) {
if (this._crypto === null) {
throw new Error("End-to-end encryption disabled");
}
@@ -482,13 +465,15 @@ MatrixClient.prototype.getStoredDevice = function(userId, deviceId) {
* @param {boolean=} verified whether to mark the device as verified. defaults
* to 'true'.
*
* @returns {Promise}
*
* @fires module:client~event:MatrixClient"deviceVerificationChanged"
*/
MatrixClient.prototype.setDeviceVerified = function(userId, deviceId, verified) {
if (verified === undefined) {
verified = true;
}
_setDeviceVerification(this, userId, deviceId, verified, null);
return _setDeviceVerification(this, userId, deviceId, verified, null);
};
/**
@@ -500,13 +485,15 @@ MatrixClient.prototype.setDeviceVerified = function(userId, deviceId, verified)
* @param {boolean=} blocked whether to mark the device as blocked. defaults
* to 'true'.
*
* @returns {Promise}
*
* @fires module:client~event:MatrixClient"deviceVerificationChanged"
*/
MatrixClient.prototype.setDeviceBlocked = function(userId, deviceId, blocked) {
if (blocked === undefined) {
blocked = true;
}
_setDeviceVerification(this, userId, deviceId, null, blocked);
return _setDeviceVerification(this, userId, deviceId, null, blocked);
};
/**
@@ -518,16 +505,20 @@ MatrixClient.prototype.setDeviceBlocked = function(userId, deviceId, blocked) {
* @param {boolean=} known whether to mark the device as known. defaults
* to 'true'.
*
* @returns {Promise}
*
* @fires module:client~event:MatrixClient"deviceVerificationChanged"
*/
MatrixClient.prototype.setDeviceKnown = function(userId, deviceId, known) {
if (known === undefined) {
known = true;
}
_setDeviceVerification(this, userId, deviceId, null, null, known);
return _setDeviceVerification(this, userId, deviceId, null, null, known);
};
function _setDeviceVerification(client, userId, deviceId, verified, blocked, known) {
async function _setDeviceVerification(
client, userId, deviceId, verified, blocked, known,
) {
if (!client._crypto) {
throw new Error("End-to-End encryption disabled");
}
@@ -568,9 +559,9 @@ MatrixClient.prototype.getGlobalBlacklistUnverifiedDevices = function() {
*
* @param {MatrixEvent} event event to be checked
*
* @return {module:crypto/deviceinfo?}
* @return {Promise<module:crypto/deviceinfo?>}
*/
MatrixClient.prototype.getEventSenderDeviceInfo = function(event) {
MatrixClient.prototype.getEventSenderDeviceInfo = async function(event) {
if (!this._crypto) {
return null;
}
@@ -586,8 +577,8 @@ MatrixClient.prototype.getEventSenderDeviceInfo = function(event) {
* @return {boolean} true if the sender of this event has been verified using
* {@link module:client~MatrixClient#setDeviceVerified|setDeviceVerified}.
*/
MatrixClient.prototype.isEventSenderVerified = function(event) {
const device = this.getEventSenderDeviceInfo(event);
MatrixClient.prototype.isEventSenderVerified = async function(event) {
const device = await this.getEventSenderDeviceInfo(event);
if (!device) {
return false;
}
@@ -641,7 +632,7 @@ MatrixClient.prototype.exportRoomKeys = function() {
*
* @param {Object[]} keys a list of session export objects
*/
MatrixClient.prototype.importRoomKeys = function(keys) {
MatrixClient.prototype.importRoomKeys = async function(keys) {
if (!this._crypto) {
throw new Error("End-to-end encryption disabled");
}

View File

@@ -409,49 +409,6 @@ Crypto.prototype.getStoredDevice = function(userId, deviceId) {
return this._deviceList.getStoredDevice(userId, deviceId);
};
/**
* List the stored device keys for a user id
*
* @deprecated prefer {@link module:crypto#getStoredDevicesForUser}
*
* @param {string} userId the user to list keys for.
*
* @return {object[]} list of devices with "id", "verified", "blocked",
* "key", and "display_name" parameters.
*/
Crypto.prototype.listDeviceKeys = function(userId) {
const devices = this.getStoredDevicesForUser(userId) || [];
const result = [];
for (let i = 0; i < devices.length; ++i) {
const device = devices[i];
const ed25519Key = device.getFingerprint();
if (ed25519Key) {
result.push({
id: device.deviceId,
key: ed25519Key,
verified: Boolean(device.isVerified()),
blocked: Boolean(device.isBlocked()),
display_name: device.getDisplayName(),
});
}
}
// sort by deviceid
result.sort(function(a, b) {
if (a.deviceId < b.deviceId) {
return -1;
}
if (a.deviceId > b.deviceId) {
return 1;
}
return 0;
});
return result;
};
/**
* Update the blocked/verified state of the given device
*