1 /* gpgme.js - Javascript integration for gpgme
2 * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
4 * This file is part of GPGME.
6 * GPGME is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * GPGME is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1+
21 * Maximilian Krambach <mkrambach@intevation.de>
24 import { permittedOperations } from './permittedOperations';
25 import { gpgme_error } from './Errors';
26 import { Connection } from './Connection';
29 * Initializes a message for gnupg, validating the message's purpose with
30 * {@link permittedOperations} first
31 * @param {String} operation
32 * @returns {GPGME_Message} The Message object
34 export function createMessage (operation){
35 if (typeof (operation) !== 'string'){
36 throw gpgme_error('PARAM_WRONG');
38 if (permittedOperations.hasOwnProperty(operation)){
39 return new GPGME_Message(operation);
41 throw gpgme_error('MSG_WRONG_OP');
46 * A Message collects, validates and handles all information required to
47 * successfully establish a meaningful communication with gpgme-json via
48 * [Connection.post]{@link Connection#post}. The definition on which
49 * communication is available can be found in {@link permittedOperations}.
53 export class GPGME_Message {
55 constructor (operation){
60 this._expected = null;
68 if (value === 'uint8' || value === 'base64'){
69 this._expected = value;
74 return this._expected;
77 * The maximum size of responses from gpgme in bytes. As of September 2018,
78 * most browsers will only accept answers up to 1 MB of size.
79 * Everything above that threshold will not pass through
80 * nativeMessaging; answers that are larger need to be sent in parts.
81 * The lower limit is set to 10 KB. Messages smaller than the threshold
82 * will not encounter problems, larger messages will be received in
83 * chunks. If the value is not explicitly specified, 1023 KB is used.
85 set chunksize (value){
87 Number.isInteger(value) &&
91 this._msg.chunksize = value;
96 return this._msg.chunksize;
100 * Returns the prepared message after their parameters and the completion
101 * of required parameters have been checked.
102 * @returns {Object|null} Object to be posted to gnupg, or null if
106 if (this.isComplete() === true){
114 * Sets a parameter for the message. It validates with
115 * {@link permittedOperations}
116 * @param {String} param Parameter to set
117 * @param {any} value Value to set
118 * @returns {Boolean} True if the parameter was set successfully.
119 * Throws errors if the parameters don't match the message operation
121 setParameter ( param,value ){
122 if (!param || typeof (param) !== 'string'){
123 throw gpgme_error('PARAM_WRONG');
125 let po = permittedOperations[this._msg.op];
127 throw gpgme_error('MSG_WRONG_OP');
130 if (po.required.hasOwnProperty(param)){
131 poparam = po.required[param];
132 } else if (po.optional.hasOwnProperty(param)){
133 poparam = po.optional[param];
135 throw gpgme_error('PARAM_WRONG');
137 // check incoming value for correctness
138 let checktype = function (val){
139 switch (typeof (val)){
141 if (poparam.allowed.indexOf(typeof (val)) >= 0
145 throw gpgme_error('PARAM_WRONG');
148 poparam.allowed.indexOf('number') >= 0
149 && isNaN(value) === false){
152 throw gpgme_error('PARAM_WRONG');
155 if (poparam.allowed.indexOf('boolean') >= 0){
158 throw gpgme_error('PARAM_WRONG');
160 if (Array.isArray(val)){
161 if (poparam.array_allowed !== true){
162 throw gpgme_error('PARAM_WRONG');
164 for (let i=0; i < val.length; i++){
165 let res = checktype(val[i]);
170 if (val.length > 0) {
173 } else if (val instanceof Uint8Array){
174 if (poparam.allowed.indexOf('Uint8Array') >= 0){
177 throw gpgme_error('PARAM_WRONG');
179 throw gpgme_error('PARAM_WRONG');
183 throw gpgme_error('PARAM_WRONG');
186 let typechecked = checktype(value);
187 if (typechecked !== true){
190 if (poparam.hasOwnProperty('allowed_data')){
191 if (poparam.allowed_data.indexOf(value) < 0){
192 return gpgme_error('PARAM_WRONG');
195 this._msg[param] = value;
201 * Check if the message has the minimum requirements to be sent, that is
202 * all 'required' parameters according to {@link permittedOperations}.
203 * @returns {Boolean} true if message is complete.
209 let reqParams = Object.keys(
210 permittedOperations[this._msg.op].required);
211 let msg_params = Object.keys(this._msg);
212 for (let i=0; i < reqParams.length; i++){
213 if (msg_params.indexOf(reqParams[i]) < 0){
221 * Sends the Message via nativeMessaging and resolves with the answer.
222 * @returns {Promise<Object>} GPGME response
227 return new Promise(function (resolve, reject) {
228 if (me.isComplete() === true) {
230 let conn = new Connection;
231 conn.post(me).then(function (response) {
233 }, function (reason) {
238 reject(gpgme_error('MSG_INCOMPLETE'));