aboutsummaryrefslogtreecommitdiffstats
path: root/lang/js/src
diff options
context:
space:
mode:
authorMaximilian Krambach <[email protected]>2018-04-26 15:13:34 +0000
committerMaximilian Krambach <[email protected]>2018-04-26 15:13:34 +0000
commit1f7b19512cfa7e1b153b99d6a2b40bad82a5496e (patch)
tree73a0392bd738eb5023b904e3ab7b7b4e0a820768 /lang/js/src
parentjs: First testing and improvements (diff)
downloadgpgme-1f7b19512cfa7e1b153b99d6a2b40bad82a5496e.tar.gz
gpgme-1f7b19512cfa7e1b153b99d6a2b40bad82a5496e.zip
js: created TestExtension and smaller fixes
-- * Extensions: - Moved testapplication to Demoextension - Created BrowserTestExtension. Includes mocha and chai. For running tests that cannot be run outside a WebExtension Both Extensions can be found zipped in build/extensions after running build_extensions.sh * Code changes: - src/Config: Place for the configuration - small fixes raised during testing in Keyring.js, Message.js, - src/gpgmejs_openpgpjs.js don't offer direct GpgME object to the outside, as it only causes confusion - index.js init() now checks the config for validity * Tests: - Reordered tests in test/. - Input values are now in a separate file which may be of use for bulk testing * moved the build directory from dist to build
Diffstat (limited to 'lang/js/src')
-rw-r--r--lang/js/src/Config.js31
-rw-r--r--lang/js/src/Keyring.js1
-rw-r--r--lang/js/src/Message.js2
-rw-r--r--lang/js/src/gpgmejs_openpgpjs.js14
-rw-r--r--lang/js/src/index.js70
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 {