diff --git a/lib/crypto-deviceinfo.js b/lib/crypto-deviceinfo.js new file mode 100644 index 000000000..c7d08b024 --- /dev/null +++ b/lib/crypto-deviceinfo.js @@ -0,0 +1,118 @@ +/* +Copyright 2016 OpenMarket Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +"use strict"; + + +/** + * @module crypto-deviceinfo + */ + +/** + * Information about a user's device + * + * @constructor + * @alias module:crypto-deviceinfo + * + * @property {string} deviceId the ID of this device + * + * @property {string[]} algorithms list of algorithms supported by this device + * + * @property {Object.} keys a map from + * <key type>:<id> -> <base64-encoded key>> + * + * @property {module:crypto-deviceinfo.DeviceVerification} verified + * whether the device has been verified by the user + * + * @property {Object} unsigned additional data from the homeserver + * + * @param {string} deviceId id of the device + */ +function DeviceInfo(deviceId) { + // you can't change the deviceId + Object.defineProperty(this, 'deviceId', { + enumerable: true, + value: deviceId, + }); + + this.algorithms = []; + this.keys = {}; + this.verified = DeviceVerification.UNVERIFIED; + this.unsigned = {}; +} + +/** + * rehydrate a DeviceInfo from the session store + * + * @param {object} obj raw object from session store + * @param {string} deviceId id of the device + * + * @return {module:crypto~DeviceInfo} new DeviceInfo + */ +DeviceInfo.fromStorage = function(obj, deviceId) { + var res = new DeviceInfo(deviceId); + for (var prop in obj) { + if (obj.hasOwnProperty(prop)) { + res[prop] = obj[prop]; + } + } + return res; +}; + +/** + * Prepare a DeviceInfo for JSON serialisation in the session store + * + * @return {object} deviceinfo with non-serialised members removed + */ +DeviceInfo.prototype.toStorage = function() { + return { + algorithms: this.algorithms, + keys: this.keys, + verified: this.verified, + unsigned: this.unsigned, + }; +}; + +/** + * Get the fingerprint for this device (ie, the Ed25519 key) + * + * @return {string} base64-encoded fingerprint of this device + */ +DeviceInfo.prototype.getFingerprint = function() { + return this.keys["ed25519:" + this.deviceId]; +}; + +/** + * Get the configured display name for this device, if any + * + * @return {string?} displayname + */ +DeviceInfo.prototype.getDisplayname = function() { + return this.unsigned.device_display_name || null; +}; + +/** + * @enum + */ +DeviceInfo.DeviceVerification = { + VERIFIED: 1, + UNVERIFIED: 0, + BLOCKED: -1, +}; + +var DeviceVerification = DeviceInfo.DeviceVerification; + +/** */ +module.exports = DeviceInfo; diff --git a/lib/crypto.js b/lib/crypto.js index 8828092bb..5e4ef775e 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -29,96 +29,8 @@ var algorithms = require("./crypto-algorithms"); var OLM_ALGORITHM = "m.olm.v1.curve25519-aes-sha2"; -/** - * @enum - */ -var DeviceVerification = { - VERIFIED: 1, - UNVERIFIED: 0, - BLOCKED: -1, -}; - -/** - * Information about a user's device - * - * @constructor - * - * @property {string} deviceId the ID of this device - * - * @property {string[]} algorithms list of algorithms supported by this device - * - * @property {Object.} keys a map from - * <key type>:<id> -> <base64-encoded key>> - * - * @property {module:crypto~DeviceVerification} verified whether the device has been - * verified by the user - * - * @property {Object} unsigned additional data from the homeserver - * - * @param {string} deviceId id of the device - */ -function DeviceInfo(deviceId) { - // you can't change the deviceId - Object.defineProperty(this, 'deviceId', { - enumerable: true, - value: deviceId, - }); - - this.algorithms = []; - this.keys = {}; - this.verified = DeviceVerification.UNVERIFIED; - this.unsigned = {}; -} - -/** - * rehydrate a DeviceInfo from the session store - * - * @param {object} obj raw object from session store - * @param {string} deviceId id of the device - * - * @return {module:crypto~DeviceInfo} new DeviceInfo - */ -DeviceInfo.fromStorage = function(obj, deviceId) { - var res = new DeviceInfo(deviceId); - for (var prop in obj) { - if (obj.hasOwnProperty(prop)) { - res[prop] = obj[prop]; - } - } - return res; -}; - -/** - * Prepare a DeviceInfo for JSON serialisation in the session store - * - * @return {object} deviceinfo with non-serialised members removed - */ -DeviceInfo.prototype.toStorage = function() { - return { - algorithms: this.algorithms, - keys: this.keys, - verified: this.verified, - unsigned: this.unsigned, - }; -}; - -/** - * Get the fingerprint for this device (ie, the Ed25519 key) - * - * @return {string} base64-encoded fingerprint of this device - */ -DeviceInfo.prototype.getFingerprint = function() { - return this.keys["ed25519:" + this.deviceId]; -}; - -/** - * Get the configured display name for this device, if any - * - * @return {string?} displayname - */ -DeviceInfo.prototype.getDisplayname = function() { - return this.unsigned.device_display_name || null; -}; +var DeviceInfo = require("./crypto-deviceinfo"); +var DeviceVerification = DeviceInfo.DeviceVerification; /** * Cryptography bits @@ -259,7 +171,7 @@ function _uploadOneTimeKeys(crypto) { * @param {bool} forceDownload Always download the keys even if cached. * * @return {Promise} A promise which resolves to a map userId->deviceId->{@link - * module:crypto~DeviceInfo|DeviceInfo}. + * module:crypto-deviceinfo|DeviceInfo}. */ Crypto.prototype.downloadKeys = function(userIds, forceDownload) { var self = this; @@ -433,7 +345,7 @@ function _storeDeviceKeys(_olmDevice, userId, deviceId, userStore, deviceResult) * * @param {string} userId the user to list keys for. * - * @return {module:crypto~DeviceInfo[]} list of devices + * @return {module:crypto-deviceinfo[]} list of devices */ Crypto.prototype.getStoredDevicesForUser = function(userId) { var devs = this._sessionStore.getEndToEndDevicesForUser(userId); @@ -496,7 +408,7 @@ Crypto.prototype.listDeviceKeys = function(userId) { * @param {string} algorithm encryption algorithm * @param {string} sender_key curve25519 key to match * - * @return {module:crypto~DeviceInfo?} + * @return {module:crypto-deviceinfo?} */ Crypto.prototype.getDeviceByIdentityKey = function(userId, algorithm, sender_key) { if (algorithm !== OLM_ALGORITHM) {