Blame lang/js/src/Message.js

Packit Service 30b792
/* gpgme.js - Javascript integration for gpgme
Packit Service 30b792
 * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
Packit Service 30b792
 *
Packit Service 30b792
 * This file is part of GPGME.
Packit Service 30b792
 *
Packit Service 30b792
 * GPGME is free software; you can redistribute it and/or modify it
Packit Service 30b792
 * under the terms of the GNU Lesser General Public License as
Packit Service 30b792
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 30b792
 * the License, or (at your option) any later version.
Packit Service 30b792
 *
Packit Service 30b792
 * GPGME is distributed in the hope that it will be useful, but
Packit Service 30b792
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 30b792
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 30b792
 * Lesser General Public License for more details.
Packit Service 30b792
 *
Packit Service 30b792
 * You should have received a copy of the GNU Lesser General Public
Packit Service 30b792
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit Service 30b792
 * SPDX-License-Identifier: LGPL-2.1+
Packit Service 30b792
 *
Packit Service 30b792
 * Author(s):
Packit Service 30b792
 *     Maximilian Krambach <mkrambach@intevation.de>
Packit Service 30b792
 */
Packit Service 30b792
Packit Service 30b792
import { permittedOperations } from './permittedOperations';
Packit Service 30b792
import { gpgme_error } from './Errors';
Packit Service 30b792
import { Connection } from './Connection';
Packit Service 30b792
Packit Service 30b792
/**
Packit Service 30b792
 * Initializes a message for gnupg, validating the message's purpose with
Packit Service 30b792
 *   {@link permittedOperations} first
Packit Service 30b792
 * @param {String} operation
Packit Service 30b792
 * @returns {GPGME_Message} The Message object
Packit Service 30b792
 */
Packit Service 30b792
export function createMessage (operation){
Packit Service 30b792
    if (typeof (operation) !== 'string'){
Packit Service 30b792
        throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
    }
Packit Service 30b792
    if (permittedOperations.hasOwnProperty(operation)){
Packit Service 30b792
        return new GPGME_Message(operation);
Packit Service 30b792
    } else {
Packit Service 30b792
        throw gpgme_error('MSG_WRONG_OP');
Packit Service 30b792
    }
Packit Service 30b792
}
Packit Service 30b792
Packit Service 30b792
/**
Packit Service 30b792
 * A Message collects, validates and handles all information required to
Packit Service 30b792
 * successfully establish a meaningful communication with gpgme-json via
Packit Service 30b792
 * [Connection.post]{@link Connection#post}. The definition on which
Packit Service 30b792
 * communication is available can be found in {@link permittedOperations}.
Packit Service 30b792
 * @class
Packit Service 30b792
 * @protected
Packit Service 30b792
 */
Packit Service 30b792
export class GPGME_Message {
Packit Service 30b792
Packit Service 30b792
    constructor (operation){
Packit Service 30b792
        this._msg = {
Packit Service 30b792
            op: operation,
Packit Service 30b792
            chunksize: 1023* 1024
Packit Service 30b792
        };
Packit Service 30b792
        this._expected = null;
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    get operation (){
Packit Service 30b792
        return this._msg.op;
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    set expected (value){
Packit Service 30b792
        if (value === 'uint8' || value === 'base64'){
Packit Service 30b792
            this._expected = value;
Packit Service 30b792
        }
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    get expected () {
Packit Service 30b792
        return this._expected;
Packit Service 30b792
    }
Packit Service 30b792
    /**
Packit Service 30b792
     * The maximum size of responses from gpgme in bytes. As of September 2018,
Packit Service 30b792
     * most browsers will only accept answers up to 1 MB of size.
Packit Service 30b792
     * Everything above that threshold will not pass through
Packit Service 30b792
     * nativeMessaging; answers that are larger need to be sent in parts.
Packit Service 30b792
     * The lower limit is set to 10 KB. Messages smaller than the threshold
Packit Service 30b792
     * will not encounter problems, larger messages will be received in
Packit Service 30b792
     * chunks. If the value is not explicitly specified, 1023 KB is used.
Packit Service 30b792
     */
Packit Service 30b792
    set chunksize (value){
Packit Service 30b792
        if (
Packit Service 30b792
            Number.isInteger(value) &&
Packit Service 30b792
            value > 10 * 1024 &&
Packit Service 30b792
            value <= 1024 * 1024
Packit Service 30b792
        ){
Packit Service 30b792
            this._msg.chunksize = value;
Packit Service 30b792
        }
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    get chunksize (){
Packit Service 30b792
        return this._msg.chunksize;
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    /**
Packit Service 30b792
     * Returns the prepared message after their parameters and the completion
Packit Service 30b792
     * of required parameters have been checked.
Packit Service 30b792
     * @returns {Object|null} Object to be posted to gnupg, or null if
Packit Service 30b792
     * incomplete
Packit Service 30b792
     */
Packit Service 30b792
    get message () {
Packit Service 30b792
        if (this.isComplete() === true){
Packit Service 30b792
            return this._msg;
Packit Service 30b792
        } else {
Packit Service 30b792
            return null;
Packit Service 30b792
        }
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    /**
Packit Service 30b792
     * Sets a parameter for the message. It validates with
Packit Service 30b792
     * {@link permittedOperations}
Packit Service 30b792
     * @param {String} param Parameter to set
Packit Service 30b792
     * @param {any} value Value to set
Packit Service 30b792
     * @returns {Boolean} True if the parameter was set successfully.
Packit Service 30b792
     * Throws errors if the parameters don't match the message operation
Packit Service 30b792
     */
Packit Service 30b792
    setParameter ( param,value ){
Packit Service 30b792
        if (!param || typeof (param) !== 'string'){
Packit Service 30b792
            throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
        }
Packit Service 30b792
        let po = permittedOperations[this._msg.op];
Packit Service 30b792
        if (!po){
Packit Service 30b792
            throw gpgme_error('MSG_WRONG_OP');
Packit Service 30b792
        }
Packit Service 30b792
        let poparam = null;
Packit Service 30b792
        if (po.required.hasOwnProperty(param)){
Packit Service 30b792
            poparam = po.required[param];
Packit Service 30b792
        } else if (po.optional.hasOwnProperty(param)){
Packit Service 30b792
            poparam = po.optional[param];
Packit Service 30b792
        } else {
Packit Service 30b792
            throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
        }
Packit Service 30b792
        // check incoming value for correctness
Packit Service 30b792
        let checktype = function (val){
Packit Service 30b792
            switch (typeof (val)){
Packit Service 30b792
            case 'string':
Packit Service 30b792
                if (poparam.allowed.indexOf(typeof (val)) >= 0
Packit Service 30b792
                        && val.length > 0) {
Packit Service 30b792
                    return true;
Packit Service 30b792
                }
Packit Service 30b792
                throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
            case 'number':
Packit Service 30b792
                if (
Packit Service 30b792
                    poparam.allowed.indexOf('number') >= 0
Packit Service 30b792
                        && isNaN(value) === false){
Packit Service 30b792
                    return true;
Packit Service 30b792
                }
Packit Service 30b792
                throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
Packit Service 30b792
            case 'boolean':
Packit Service 30b792
                if (poparam.allowed.indexOf('boolean') >= 0){
Packit Service 30b792
                    return true;
Packit Service 30b792
                }
Packit Service 30b792
                throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
            case 'object':
Packit Service 30b792
                if (Array.isArray(val)){
Packit Service 30b792
                    if (poparam.array_allowed !== true){
Packit Service 30b792
                        throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
                    }
Packit Service 30b792
                    for (let i=0; i < val.length; i++){
Packit Service 30b792
                        let res = checktype(val[i]);
Packit Service 30b792
                        if (res !== true){
Packit Service 30b792
                            return res;
Packit Service 30b792
                        }
Packit Service 30b792
                    }
Packit Service 30b792
                    if (val.length > 0) {
Packit Service 30b792
                        return true;
Packit Service 30b792
                    }
Packit Service 30b792
                } else if (val instanceof Uint8Array){
Packit Service 30b792
                    if (poparam.allowed.indexOf('Uint8Array') >= 0){
Packit Service 30b792
                        return true;
Packit Service 30b792
                    }
Packit Service 30b792
                    throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
                } else {
Packit Service 30b792
                    throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
                }
Packit Service 30b792
                break;
Packit Service 30b792
            default:
Packit Service 30b792
                throw gpgme_error('PARAM_WRONG');
Packit Service 30b792
            }
Packit Service 30b792
        };
Packit Service 30b792
        let typechecked = checktype(value);
Packit Service 30b792
        if (typechecked !== true){
Packit Service 30b792
            return typechecked;
Packit Service 30b792
        }
Packit Service 30b792
        if (poparam.hasOwnProperty('allowed_data')){
Packit Service 30b792
            if (poparam.allowed_data.indexOf(value) < 0){
Packit Service 30b792
                return gpgme_error('PARAM_WRONG');
Packit Service 30b792
            }
Packit Service 30b792
        }
Packit Service 30b792
        this._msg[param] = value;
Packit Service 30b792
        return true;
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
Packit Service 30b792
    /**
Packit Service 30b792
     * Check if the message has the minimum requirements to be sent, that is
Packit Service 30b792
     * all 'required' parameters according to {@link permittedOperations}.
Packit Service 30b792
     * @returns {Boolean} true if message is complete.
Packit Service 30b792
     */
Packit Service 30b792
    isComplete (){
Packit Service 30b792
        if (!this._msg.op){
Packit Service 30b792
            return false;
Packit Service 30b792
        }
Packit Service 30b792
        let reqParams = Object.keys(
Packit Service 30b792
            permittedOperations[this._msg.op].required);
Packit Service 30b792
        let msg_params = Object.keys(this._msg);
Packit Service 30b792
        for (let i=0; i < reqParams.length; i++){
Packit Service 30b792
            if (msg_params.indexOf(reqParams[i]) < 0){
Packit Service 30b792
                return false;
Packit Service 30b792
            }
Packit Service 30b792
        }
Packit Service 30b792
        return true;
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    /**
Packit Service 30b792
     * Sends the Message via nativeMessaging and resolves with the answer.
Packit Service 30b792
     * @returns {Promise<Object>} GPGME response
Packit Service 30b792
     * @async
Packit Service 30b792
     */
Packit Service 30b792
    post (){
Packit Service 30b792
        let me = this;
Packit Service 30b792
        return new Promise(function (resolve, reject) {
Packit Service 30b792
            if (me.isComplete() === true) {
Packit Service 30b792
Packit Service 30b792
                let conn  = new Connection;
Packit Service 30b792
                conn.post(me).then(function (response) {
Packit Service 30b792
                    resolve(response);
Packit Service 30b792
                }, function (reason) {
Packit Service 30b792
                    reject(reason);
Packit Service 30b792
                });
Packit Service 30b792
            }
Packit Service 30b792
            else {
Packit Service 30b792
                reject(gpgme_error('MSG_INCOMPLETE'));
Packit Service 30b792
            }
Packit Service 30b792
        });
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
}