'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.UnknownDeviceError = exports.DecryptionError = exports.DecryptionAlgorithm = exports.EncryptionAlgorithm = exports.DECRYPTION_CLASSES = exports.ENCRYPTION_CLASSES = undefined;

var _keys = require('babel-runtime/core-js/object/keys');

var _keys2 = _interopRequireDefault(_keys);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

exports.registerAlgorithm = registerAlgorithm;

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * map of registered encryption algorithm classes. A map from string to {@link
 * module:crypto/algorithms/base.EncryptionAlgorithm|EncryptionAlgorithm} class
 *
 * @type {Object.<string, function(new: module:crypto/algorithms/base.EncryptionAlgorithm)>}
 */
var ENCRYPTION_CLASSES = exports.ENCRYPTION_CLASSES = {};

/**
 * map of registered encryption algorithm classes. Map from string to {@link
 * module:crypto/algorithms/base.DecryptionAlgorithm|DecryptionAlgorithm} class
 *
 * @type {Object.<string, function(new: module:crypto/algorithms/base.DecryptionAlgorithm)>}
 */
/*
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.
*/

/**
 * Internal module. Defines the base classes of the encryption implementations
 *
 * @module
 */

var DECRYPTION_CLASSES = exports.DECRYPTION_CLASSES = {};

/**
 * base type for encryption implementations
 *
 * @alias module:crypto/algorithms/base.EncryptionAlgorithm
 *
 * @param {object} params parameters
 * @param {string} params.userId  The UserID for the local user
 * @param {string} params.deviceId The identifier for this device.
 * @param {module:crypto} params.crypto crypto core
 * @param {module:crypto/OlmDevice} params.olmDevice olm.js wrapper
 * @param {module:base-apis~MatrixBaseApis} baseApis base matrix api interface
 * @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
 */

var EncryptionAlgorithm = function () {
    function EncryptionAlgorithm(params) {
        (0, _classCallCheck3.default)(this, EncryptionAlgorithm);

        this._userId = params.userId;
        this._deviceId = params.deviceId;
        this._crypto = params.crypto;
        this._olmDevice = params.olmDevice;
        this._baseApis = params.baseApis;
        this._roomId = params.roomId;
    }

    /**
     * Encrypt a message event
     *
     * @method module:crypto/algorithms/base.EncryptionAlgorithm.encryptMessage
     * @abstract
     *
     * @param {module:models/room} room
     * @param {string} eventType
     * @param {object} plaintext event content
     *
     * @return {module:client.Promise} Promise which resolves to the new event body
     */

    /**
     * Called when the membership of a member of the room changes.
     *
     * @param {module:models/event.MatrixEvent} event  event causing the change
     * @param {module:models/room-member} member  user whose membership changed
     * @param {string=} oldMembership  previous membership
     * @public
     */


    (0, _createClass3.default)(EncryptionAlgorithm, [{
        key: 'onRoomMembership',
        value: function onRoomMembership(event, member, oldMembership) {}
    }]);
    return EncryptionAlgorithm;
}();

exports.EncryptionAlgorithm = EncryptionAlgorithm; // https://github.com/jsdoc3/jsdoc/issues/1272

/**
 * base type for decryption implementations
 *
 * @alias module:crypto/algorithms/base.DecryptionAlgorithm
 * @param {object} params parameters
 * @param {string} params.userId  The UserID for the local user
 * @param {module:crypto} params.crypto crypto core
 * @param {module:crypto/OlmDevice} params.olmDevice olm.js wrapper
 * @param {module:base-apis~MatrixBaseApis} baseApis base matrix api interface
 * @param {string=} params.roomId The ID of the room we will be receiving
 *     from. Null for to-device events.
 */

var DecryptionAlgorithm = function () {
    function DecryptionAlgorithm(params) {
        (0, _classCallCheck3.default)(this, DecryptionAlgorithm);

        this._userId = params.userId;
        this._crypto = params.crypto;
        this._olmDevice = params.olmDevice;
        this._baseApis = params.baseApis;
        this._roomId = params.roomId;
    }

    /**
     * Decrypt an event
     *
     * @method module:crypto/algorithms/base.DecryptionAlgorithm#decryptEvent
     * @abstract
     *
     * @param {MatrixEvent} event undecrypted event
     *
     * @return {Promise<module:crypto~EventDecryptionResult>} promise which
     * resolves once we have finished decrypting. Rejects with an
     * `algorithms.DecryptionError` if there is a problem decrypting the event.
     */

    /**
     * Handle a key event
     *
     * @method module:crypto/algorithms/base.DecryptionAlgorithm#onRoomKeyEvent
     *
     * @param {module:models/event.MatrixEvent} params event key event
     */


    (0, _createClass3.default)(DecryptionAlgorithm, [{
        key: 'onRoomKeyEvent',
        value: function onRoomKeyEvent(params) {}
        // ignore by default


        /**
         * Import a room key
         *
         * @param {module:crypto/OlmDevice.MegolmSessionData} session
         */

    }, {
        key: 'importRoomKey',
        value: function importRoomKey(session) {}
        // ignore by default


        /**
         * Determine if we have the keys necessary to respond to a room key request
         *
         * @param {module:crypto~IncomingRoomKeyRequest} keyRequest
         * @return {Promise<boolean>} true if we have the keys and could (theoretically) share
         *  them; else false.
         */

    }, {
        key: 'hasKeysForKeyRequest',
        value: function hasKeysForKeyRequest(keyRequest) {
            return _bluebird2.default.resolve(false);
        }

        /**
         * Send the response to a room key request
         *
         * @param {module:crypto~IncomingRoomKeyRequest} keyRequest
         */

    }, {
        key: 'shareKeysWithDevice',
        value: function shareKeysWithDevice(keyRequest) {
            throw new Error("shareKeysWithDevice not supported for this DecryptionAlgorithm");
        }
    }]);
    return DecryptionAlgorithm;
}();

exports.DecryptionAlgorithm = DecryptionAlgorithm; // https://github.com/jsdoc3/jsdoc/issues/1272

/**
 * Exception thrown when decryption fails
 *
 * @alias module:crypto/algorithms/base.DecryptionError
 * @param {string} msg user-visible message describing the problem
 *
 * @param {Object=} details key/value pairs reported in the logs but not shown
 *   to the user.
 *
 * @extends Error
 */

var DecryptionError = function (_Error) {
    (0, _inherits3.default)(DecryptionError, _Error);

    function DecryptionError(code, msg, details) {
        (0, _classCallCheck3.default)(this, DecryptionError);

        var _this = (0, _possibleConstructorReturn3.default)(this, (DecryptionError.__proto__ || (0, _getPrototypeOf2.default)(DecryptionError)).call(this, msg));

        _this.code = code;
        _this.name = 'DecryptionError';
        _this.detailedString = _detailedStringForDecryptionError(_this, details);
        return _this;
    }

    return DecryptionError;
}(Error);

exports.DecryptionError = DecryptionError; // https://github.com/jsdoc3/jsdoc/issues/1272

function _detailedStringForDecryptionError(err, details) {
    var result = err.name + '[msg: ' + err.message;

    if (details) {
        result += ', ' + (0, _keys2.default)(details).map(function (k) {
            return k + ': ' + details[k];
        }).join(', ');
    }

    result += ']';

    return result;
}

/**
 * Exception thrown specifically when we want to warn the user to consider
 * the security of their conversation before continuing
 *
 * @param {string} msg message describing the problem
 * @param {Object} devices userId -> {deviceId -> object}
 *      set of unknown devices per user we're warning about
 * @extends Error
 */

var UnknownDeviceError = exports.UnknownDeviceError = function (_Error2) {
    (0, _inherits3.default)(UnknownDeviceError, _Error2);

    function UnknownDeviceError(msg, devices) {
        (0, _classCallCheck3.default)(this, UnknownDeviceError);

        var _this2 = (0, _possibleConstructorReturn3.default)(this, (UnknownDeviceError.__proto__ || (0, _getPrototypeOf2.default)(UnknownDeviceError)).call(this, msg));

        _this2.name = "UnknownDeviceError";
        _this2.devices = devices;
        return _this2;
    }

    return UnknownDeviceError;
}(Error);

/**
 * Registers an encryption/decryption class for a particular algorithm
 *
 * @param {string} algorithm algorithm tag to register for
 *
 * @param {class} encryptor {@link
 *     module:crypto/algorithms/base.EncryptionAlgorithm|EncryptionAlgorithm}
 *     implementation
 *
 * @param {class} decryptor {@link
 *     module:crypto/algorithms/base.DecryptionAlgorithm|DecryptionAlgorithm}
 *     implementation
 */


function registerAlgorithm(algorithm, encryptor, decryptor) {
    ENCRYPTION_CLASSES[algorithm] = encryptor;
    DECRYPTION_CLASSES[algorithm] = decryptor;
}
//# sourceMappingURL=base.js.map