diff --git a/lib/client.js b/lib/client.js index 1c0c26148..e4cb2cbe0 100644 --- a/lib/client.js +++ b/lib/client.js @@ -401,6 +401,21 @@ function _setDeviceVerification(client, userId, deviceId, verified, blocked) { client.emit("deviceVerificationChanged", userId, deviceId); } +/** + * Get e2e information on the device that sent an event + * + * @param {MatrixEvent} event event to be checked + * + * @return {module:crypto/deviceinfo?} + */ +MatrixClient.prototype.getEventSenderDeviceInfo = function(event) { + if (!this._crypto) { + return null; + } + + return this._crypto.getEventSenderDeviceInfo(event); +}; + /** * Check if the sender of an event is verified * @@ -410,21 +425,11 @@ function _setDeviceVerification(client, userId, deviceId, verified, blocked) { * {@link module:client~MatrixClient#setDeviceVerified|setDeviceVerified}. */ MatrixClient.prototype.isEventSenderVerified = function(event) { - if (!this._crypto) { + var device = this.getEventSenderDeviceInfo(event); + if (!device) { return false; } - - var sender_key = event.getSenderKey(); - - if (!sender_key) { - return false; - } - - var algorithm = event.getWireContent().algorithm; - - return this._crypto.isSenderKeyVerified( - event.getSender(), algorithm, sender_key - ); + return device.isVerified(); }; /** diff --git a/lib/crypto/index.js b/lib/crypto/index.js index fb65ffa8d..32bb1c70c 100644 --- a/lib/crypto/index.js +++ b/lib/crypto/index.js @@ -611,15 +611,19 @@ Crypto.prototype.getOlmSessionsForUser = function(userId) { /** - * Identify a device by curve25519 identity key and determine its verification state + * Get the device which sent an event * - * @param {string} userId owner of the device - * @param {string} algorithm encryption algorithm - * @param {string} sender_key curve25519 key to match + * @param {module:models/event.MatrixEvent} event event to be checked * - * @return {boolean} true if the device is verified + * @return {module:crypto/deviceinfo?} */ -Crypto.prototype.isSenderKeyVerified = function(userId, algorithm, sender_key) { +Crypto.prototype.getEventSenderDeviceInfo = function(event) { + var sender_key = event.getSenderKey(); + var algorithm = event.getWireContent().algorithm; + + if (!sender_key || !algorithm) { + return null; + } // sender_key is the curve25519 public key of the device, that the event // purports to have been sent from. It's assumed that, by the time we get here, @@ -633,11 +637,9 @@ Crypto.prototype.isSenderKeyVerified = function(userId, algorithm, sender_key) { // So, all we need to do here is look up the device by sender and // curve25519 key and determine the state of the verification flag. - var device = this.getDeviceByIdentityKey(userId, algorithm, sender_key); - if (!device) { - return false; - } - return device.verified == DeviceVerification.VERIFIED; + return this.getDeviceByIdentityKey( + event.getSender(), algorithm, sender_key + ); };