diff options
Diffstat (limited to '')
| -rw-r--r-- | lang/js/src/Config.js | 31 | ||||
| -rw-r--r-- | lang/js/src/Keyring.js | 1 | ||||
| -rw-r--r-- | lang/js/src/Message.js | 2 | ||||
| -rw-r--r-- | lang/js/src/gpgmejs_openpgpjs.js | 14 | ||||
| -rw-r--r-- | lang/js/src/index.js | 70 | 
5 files changed, 84 insertions, 34 deletions
diff --git a/lang/js/src/Config.js b/lang/js/src/Config.js new file mode 100644 index 00000000..e18728de --- /dev/null +++ b/lang/js/src/Config.js @@ -0,0 +1,31 @@ +/* gpgme.js - Javascript integration for gpgme + * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see <http://www.gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1+ + */ + +export const availableConf = { +    api_style: ['gpgme', 'gpgme_openpgpjs'], +    null_expire_is_never: [true, false], +    unconsidered_params: ['warn','reject', 'ignore'], +}; + +export const defaultConf = { +    api_style: 'gpgme', +    null_expire_is_never: false, +    unconsidered_params: 'reject', +};
\ No newline at end of file diff --git a/lang/js/src/Keyring.js b/lang/js/src/Keyring.js index 470eeeec..364bfb46 100644 --- a/lang/js/src/Keyring.js +++ b/lang/js/src/Keyring.js @@ -22,6 +22,7 @@ import {createMessage} from './Message'  import {GPGME_Key} from './Key'  import { isFingerprint, isLongId } from './Helpers';  import { gpgme_error } from './Errors'; +import { Connection } from './Connection';  export class GPGME_Keyring {      constructor(connection){ diff --git a/lang/js/src/Message.js b/lang/js/src/Message.js index 4d242277..9e7a8835 100644 --- a/lang/js/src/Message.js +++ b/lang/js/src/Message.js @@ -36,7 +36,7 @@ export function createMessage(operation){   * ./permittedOperations.   * @param {String} operation   */ -class GPGME_Message { +export class GPGME_Message {      //TODO getter      constructor(operation){ diff --git a/lang/js/src/gpgmejs_openpgpjs.js b/lang/js/src/gpgmejs_openpgpjs.js index 4e5e1ea0..cc2afde1 100644 --- a/lang/js/src/gpgmejs_openpgpjs.js +++ b/lang/js/src/gpgmejs_openpgpjs.js @@ -50,18 +50,12 @@              if (!this._GPGME){                  this._GpgME = new GpgME(connection, config);              } -            if (!this._Keyring){ -                this._Keyring = new GPGME_Keyring_openpgpmode(connection); +            if (!this._keyring){ +                this._keyring = new GPGME_Keyring_openpgpmode(connection);              }          }      } -    get GpgME(){ -        if (this._GpGME){ -            return this._GpGME; -        } -    } -      /**       * Encrypt Message       * Supported: @@ -115,7 +109,7 @@                  return Promise.reject(GPMGEJS_Error('NOT_IMPLEMENTED'));              }          } -        return this.GpgME.encrypt(data, translateKeyInput(publicKeys), wildcard); +        return this._GpgME.encrypt(data, translateKeyInput(publicKeys), wildcard);      }      /** Decrypt Message @@ -152,7 +146,7 @@                  return Promise.reject(GPMGEJS_Error('NOT_IMPLEMENTED'));              }          } -        return this.GpgME.decrypt(message); +        return this._GpgME.decrypt(message);          // TODO: translate between:          // openpgp:          // { data:Uint8Array|String, diff --git a/lang/js/src/index.js b/lang/js/src/index.js index 48904316..4de98457 100644 --- a/lang/js/src/index.js +++ b/lang/js/src/index.js @@ -22,36 +22,60 @@ import { GpgME } from "./gpgmejs";  import { gpgme_error } from "./Errors";  import { GpgME_openpgpmode } from "./gpgmejs_openpgpjs";  import { Connection } from "./Connection"; +import { defaultConf, availableConf } from "./Config";  /**   * Initializes a nativeMessaging Connection and returns a GPGMEjs object - * @param {*} conf Configuration. TBD + * @param {Object} config Configuration. See Config.js for available parameters. Still TODO   */ -function init( config = { -    api_style: 'gpgme', //  | gpgme_openpgpjs -    null_expire_is_never: true, // Boolean -    unconsidered_params: 'warn'//'warn' || 'reject' -    }){ -        return new Promise(function(resolve, reject){ -            let connection = new Connection; -            // TODO: Delayed reaction is ugly. We need to listen to the port's -            // event listener in isConnected, but this takes some time (<5ms) to -            // disconnect if there is no successfull connection. -            let delayedreaction = function(){ -                if (connection.isConnected === true){ -                    let gpgme = null; -                    if (config.api_style && config.api_style === 'gpgme_openpgpjs'){ -                        resolve( -                            new GpgME_openpgpmode(connection, config)); -                    } else { -                        resolve(new GpgME(connection)); -                    } +function init(config){ +    let _conf = parseconfiguration(config); +    if (_conf instanceof Error){ +        return Promise.reject(_conf); +    } +    return new Promise(function(resolve, reject){ +        let connection = new Connection; +        // TODO: Delayed reaction is ugly. We need to listen to the port's +        // event listener in isConnected, but this takes some time (<5ms) to +        // disconnect if there is no successfull connection. +        let delayedreaction = function(){ +            if (connection.isConnected === true){ +                if (_conf.api_style && _conf.api_style === 'gpgme_openpgpjs'){ +                    resolve(new GpgME_openpgpmode(connection, _conf));                  } else { -                    reject(gpgme_error('CONN_NO_CONNECT')); +                    resolve(new GpgME(connection));                  } -            }; -            setTimeout(delayedreaction, 5); +            } else { +                reject(gpgme_error('CONN_NO_CONNECT')); +            } +        }; +        setTimeout(delayedreaction, 5);      }); +} + +function parseconfiguration(config){ +    if (!config){ +        return defaultConf; +    } +    if ( typeof(config) !== 'object'){ +        return gpgme_error('PARAM_WRONG'); +    }; +    let result_config = defaultConf; +    let conf_keys = Object.keys(config); +    for (let i=0; i < conf_keys; i++){ +        if (availableConf.hasOwnProperty(conf_keys[i])){ +            let value = config[conf_keys[i]]; +            if (availableConf[conf_keys[i]].indexOf(value) < 0){ +                return gpgme_error('PARAM_WRONG'); +            } else { +                result_config[conf_keys[i]] = value; +            } +        } +        else { +            return gpgme_error('PARAM_WRONG'); +        } +    } +    return result_config;  };  export default {  | 
