1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

crypto/algorithms/base.js: Convert to es6

Convert base to an es6 module with es6 classes, for clarity and to help with
jsdoccing.

Complications are:

* jsdoc gets confused by `export class`, so the exports are separated.

* turns out that extending Error is a bit difficult, so instanceof doesn't work
  on derived Error classes. This only really affects us in one place (app-side
  code shouldn't be doing instanceofs anyway), so just use `name` instead.
This commit is contained in:
Richard van der Hoff
2017-05-23 14:32:13 +01:00
parent 0945ba9e90
commit b26c1c57dc
3 changed files with 113 additions and 120 deletions

View File

@@ -567,7 +567,7 @@ function _decryptEvent(client, event) {
console.warn( console.warn(
`Error decrypting event (id=${event.getId()}): ${e}`, `Error decrypting event (id=${event.getId()}): ${e}`,
); );
if (!(e instanceof Crypto.DecryptionError)) { if (e.name !== "DecryptionError") {
throw e; throw e;
} }
_badEncryptedMessage(event, e.message); _badEncryptedMessage(event, e.message);

View File

@@ -13,14 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
"use strict";
/** /**
* Internal module. Defines the base classes of the encryption implementations * Internal module. Defines the base classes of the encryption implementations
* *
* @module crypto/algorithms/base * @module
*/ */
const utils = require("../../utils");
/** /**
* map of registered encryption algorithm classes. A map from string to {@link * map of registered encryption algorithm classes. A map from string to {@link
@@ -28,7 +26,7 @@ const utils = require("../../utils");
* *
* @type {Object.<string, function(new: module:crypto/algorithms/base.EncryptionAlgorithm)>} * @type {Object.<string, function(new: module:crypto/algorithms/base.EncryptionAlgorithm)>}
*/ */
module.exports.ENCRYPTION_CLASSES = {}; export const ENCRYPTION_CLASSES = {};
/** /**
* map of registered encryption algorithm classes. Map from string to {@link * map of registered encryption algorithm classes. Map from string to {@link
@@ -36,12 +34,11 @@ module.exports.ENCRYPTION_CLASSES = {};
* *
* @type {Object.<string, function(new: module:crypto/algorithms/base.DecryptionAlgorithm)>} * @type {Object.<string, function(new: module:crypto/algorithms/base.DecryptionAlgorithm)>}
*/ */
module.exports.DECRYPTION_CLASSES = {}; export const DECRYPTION_CLASSES = {};
/** /**
* base type for encryption implementations * base type for encryption implementations
* *
* @constructor
* @alias module:crypto/algorithms/base.EncryptionAlgorithm * @alias module:crypto/algorithms/base.EncryptionAlgorithm
* *
* @param {object} params parameters * @param {object} params parameters
@@ -53,21 +50,20 @@ module.exports.DECRYPTION_CLASSES = {};
* @param {string} params.roomId The ID of the room we will be sending to * @param {string} params.roomId The ID of the room we will be sending to
* @param {object} params.config The body of the m.room.encryption event * @param {object} params.config The body of the m.room.encryption event
*/ */
const EncryptionAlgorithm = function(params) { class EncryptionAlgorithm {
constructor(params) {
this._userId = params.userId; this._userId = params.userId;
this._deviceId = params.deviceId; this._deviceId = params.deviceId;
this._crypto = params.crypto; this._crypto = params.crypto;
this._olmDevice = params.olmDevice; this._olmDevice = params.olmDevice;
this._baseApis = params.baseApis; this._baseApis = params.baseApis;
this._roomId = params.roomId; this._roomId = params.roomId;
}; }
/** */
module.exports.EncryptionAlgorithm = EncryptionAlgorithm;
/** /**
* Encrypt a message event * Encrypt a message event
* *
* @method module:crypto/algorithms/base.EncryptionAlgorithm#encryptMessage * @method module:crypto/algorithms/base.EncryptionAlgorithm.encryptMessage
* @abstract * @abstract
* *
* @param {module:models/room} room * @param {module:models/room} room
@@ -83,15 +79,16 @@ module.exports.EncryptionAlgorithm = EncryptionAlgorithm;
* @param {module:models/event.MatrixEvent} event event causing the change * @param {module:models/event.MatrixEvent} event event causing the change
* @param {module:models/room-member} member user whose membership changed * @param {module:models/room-member} member user whose membership changed
* @param {string=} oldMembership previous membership * @param {string=} oldMembership previous membership
* @public
*/ */
EncryptionAlgorithm.prototype.onRoomMembership = function( onRoomMembership(event, member, oldMembership) {
event, member, oldMembership, }
) {}; }
export {EncryptionAlgorithm}; // https://github.com/jsdoc3/jsdoc/issues/1272
/** /**
* base type for decryption implementations * base type for decryption implementations
* *
* @constructor
* @alias module:crypto/algorithms/base.DecryptionAlgorithm * @alias module:crypto/algorithms/base.DecryptionAlgorithm
* *
* @param {object} params parameters * @param {object} params parameters
@@ -101,14 +98,13 @@ EncryptionAlgorithm.prototype.onRoomMembership = function(
* @param {string=} params.roomId The ID of the room we will be receiving * @param {string=} params.roomId The ID of the room we will be receiving
* from. Null for to-device events. * from. Null for to-device events.
*/ */
const DecryptionAlgorithm = function(params) { class DecryptionAlgorithm {
constructor(params) {
this._userId = params.userId; this._userId = params.userId;
this._crypto = params.crypto; this._crypto = params.crypto;
this._olmDevice = params.olmDevice; this._olmDevice = params.olmDevice;
this._roomId = params.roomId; this._roomId = params.roomId;
}; }
/** */
module.exports.DecryptionAlgorithm = DecryptionAlgorithm;
/** /**
* Decrypt an event * Decrypt an event
@@ -132,24 +128,25 @@ module.exports.DecryptionAlgorithm = DecryptionAlgorithm;
* *
* @param {module:models/event.MatrixEvent} params event key event * @param {module:models/event.MatrixEvent} params event key event
*/ */
DecryptionAlgorithm.prototype.onRoomKeyEvent = function(params) { onRoomKeyEvent(params) {
// ignore by default // ignore by default
}; }
/** /**
* Import a room key * Import a room key
* *
* @param {module:crypto/OlmDevice.MegolmSessionData} session * @param {module:crypto/OlmDevice.MegolmSessionData} session
*/ */
DecryptionAlgorithm.prototype.importRoomKey = function(session) { importRoomKey(session) {
// ignore by default // ignore by default
}; }
}
export {DecryptionAlgorithm}; // https://github.com/jsdoc3/jsdoc/issues/1272
/** /**
* Exception thrown when decryption fails * Exception thrown when decryption fails
* *
* @alias module:crypto/algorithms/base.DecryptionError * @alias module:crypto/algorithms/base.DecryptionError
* @constructor
* @param {string} msg user-visible message describing the problem * @param {string} msg user-visible message describing the problem
* *
* @param {Object=} details key/value pairs reported in the logs but not shown * @param {Object=} details key/value pairs reported in the logs but not shown
@@ -157,18 +154,19 @@ DecryptionAlgorithm.prototype.importRoomKey = function(session) {
* *
* @extends Error * @extends Error
*/ */
const DecryptionError = function(msg, details) { class DecryptionError extends Error {
constructor(msg, details) {
super(msg);
this.name = 'DecryptionError'; this.name = 'DecryptionError';
this.message = msg;
this.details = details; this.details = details;
}; }
utils.inherits(DecryptionError, Error);
/** override the string used when logging /**
* override the string used when logging
* *
* @returns {String} * @returns {String}
*/ */
DecryptionError.prototype.toString = function() { toString() {
let result = this.name + '[msg: ' + this.message; let result = this.name + '[msg: ' + this.message;
if (this.details) { if (this.details) {
@@ -181,26 +179,26 @@ DecryptionError.prototype.toString = function() {
result += ']'; result += ']';
return result; return result;
}; }
}
module.exports.DecryptionError = DecryptionError; export {DecryptionError}; // https://github.com/jsdoc3/jsdoc/issues/1272
/** /**
* Exception thrown specifically when we want to warn the user to consider * Exception thrown specifically when we want to warn the user to consider
* the security of their conversation before continuing * the security of their conversation before continuing
* *
* @constructor
* @param {string} msg message describing the problem * @param {string} msg message describing the problem
* @param {Object} devices userId -> {deviceId -> object} * @param {Object} devices userId -> {deviceId -> object}
* set of unknown devices per user we're warning about * set of unknown devices per user we're warning about
* @extends Error * @extends Error
*/ */
module.exports.UnknownDeviceError = function(msg, devices) { export class UnknownDeviceError extends Error {
constructor(msg, devices) {
super(msg);
this.name = "UnknownDeviceError"; this.name = "UnknownDeviceError";
this.message = msg;
this.devices = devices; this.devices = devices;
}; }
utils.inherits(module.exports.UnknownDeviceError, Error); }
/** /**
* Registers an encryption/decryption class for a particular algorithm * Registers an encryption/decryption class for a particular algorithm
@@ -215,7 +213,7 @@ utils.inherits(module.exports.UnknownDeviceError, Error);
* module:crypto/algorithms/base.DecryptionAlgorithm|DecryptionAlgorithm} * module:crypto/algorithms/base.DecryptionAlgorithm|DecryptionAlgorithm}
* implementation * implementation
*/ */
module.exports.registerAlgorithm = function(algorithm, encryptor, decryptor) { export function registerAlgorithm(algorithm, encryptor, decryptor) {
module.exports.ENCRYPTION_CLASSES[algorithm] = encryptor; ENCRYPTION_CLASSES[algorithm] = encryptor;
module.exports.DECRYPTION_CLASSES[algorithm] = decryptor; DECRYPTION_CLASSES[algorithm] = decryptor;
}; }

View File

@@ -1121,11 +1121,6 @@ Crypto.prototype._signObject = function(obj) {
obj.signatures = sigs; obj.signatures = sigs;
}; };
/**
* @see module:crypto/algorithms/base.DecryptionError
*/
Crypto.DecryptionError = algorithms.DecryptionError;
/** */ /** */
module.exports = Crypto; module.exports = Crypto;