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

Merge pull request #175 from matrix-org/rav/refactor_deviceinfo

Move DeviceInfo and DeviceVerification to separate module
This commit is contained in:
Richard van der Hoff
2016-08-22 10:16:48 +01:00
committed by GitHub
2 changed files with 123 additions and 93 deletions

118
lib/crypto-deviceinfo.js Normal file
View File

@@ -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.<string,string>} keys a map from
* &lt;key type&gt;:&lt;id&gt; -> &lt;base64-encoded key&gt;>
*
* @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;

View File

@@ -29,96 +29,8 @@ var algorithms = require("./crypto-algorithms");
var OLM_ALGORITHM = "m.olm.v1.curve25519-aes-sha2"; var OLM_ALGORITHM = "m.olm.v1.curve25519-aes-sha2";
/** var DeviceInfo = require("./crypto-deviceinfo");
* @enum var DeviceVerification = DeviceInfo.DeviceVerification;
*/
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.<string,string>} keys a map from
* &lt;key type&gt;:&lt;id&gt; -> &lt;base64-encoded key&gt;>
*
* @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;
};
/** /**
* Cryptography bits * Cryptography bits
@@ -259,7 +171,7 @@ function _uploadOneTimeKeys(crypto) {
* @param {bool} forceDownload Always download the keys even if cached. * @param {bool} forceDownload Always download the keys even if cached.
* *
* @return {Promise} A promise which resolves to a map userId->deviceId->{@link * @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) { Crypto.prototype.downloadKeys = function(userIds, forceDownload) {
var self = this; var self = this;
@@ -433,7 +345,7 @@ function _storeDeviceKeys(_olmDevice, userId, deviceId, userStore, deviceResult)
* *
* @param {string} userId the user to list keys for. * @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) { Crypto.prototype.getStoredDevicesForUser = function(userId) {
var devs = this._sessionStore.getEndToEndDevicesForUser(userId); var devs = this._sessionStore.getEndToEndDevicesForUser(userId);
@@ -496,7 +408,7 @@ Crypto.prototype.listDeviceKeys = function(userId) {
* @param {string} algorithm encryption algorithm * @param {string} algorithm encryption algorithm
* @param {string} sender_key curve25519 key to match * @param {string} sender_key curve25519 key to match
* *
* @return {module:crypto~DeviceInfo?} * @return {module:crypto-deviceinfo?}
*/ */
Crypto.prototype.getDeviceByIdentityKey = function(userId, algorithm, sender_key) { Crypto.prototype.getDeviceByIdentityKey = function(userId, algorithm, sender_key) {
if (algorithm !== OLM_ALGORITHM) { if (algorithm !== OLM_ALGORITHM) {