From 6ab25e40d904007755c5d999bf66ae264236e745 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Wed, 18 Apr 2018 16:38:06 +0200 Subject: js: encrypt improvement and decrypt method * Compatibility class gpgme_openpgpjs offers an API that should accept openpgpjs syntax, throwing errors if a parameter is unexpected/not implemented * tried to be more generic in methods * waiting for multiple answers if 'more' is in the answer * more consistency checking on sending and receiving * updated the example extension -- --- lang/js/src/Helpers.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 lang/js/src/Helpers.js (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js new file mode 100644 index 00000000..eeb7a3c4 --- /dev/null +++ b/lang/js/src/Helpers.js @@ -0,0 +1,84 @@ +/* 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 . + * SPDX-License-Identifier: LGPL-2.1+ + */ + +/** + * Tries to return an array of fingerprints, either from input fingerprints or + * from Key objects + * @param {String|Array} input Input value. + * @returns {Array} Array of fingerprints. + */ +export function toKeyIdArray(input){ + if (!input){ + return []; + // TODO: Warning or error here? Did we expect something or is "nothing" okay? + } + if (input instanceof Array){ + let result = []; + for (let i=0; i < input.length; i++){ + if (isFingerprint(input[i]) === true){ + result.push(input[i]); + } else { + //TODO error? + console.log('gpgmejs/Helpers.js Warning: '+ + input[i] + + ' is not a valid key fingerprint and will not be used'); + } + } + return result; + } else if (isFingerprint(input) === true) { + return [input]; + } + console.log('gpgmejs/Helpers.js Warning: ' + input + + ' is not a valid key fingerprint and will not be used'); + return []; +}; + +/** + * check if values are valid hexadecimal values of a specified length + * @param {*} key input value. + * @param {int} len the expected length of the value + */ +function hextest(key, len){ + if (!key || typeof(key) !== "string"){ + return false; + } + if (key.length !== len){ + return false; + } + let regexp= /^[0-9a-fA-F]*$/i; + return regexp.test(key); +}; + +/** + * check if the input is a valid Hex string with a length of 40 + */ +export function isFingerprint(string){ + return hextest(string, 40); +}; + +//TODO needed anywhere? +function isLongId(string){ + return hextest(string, 16); +}; + +//TODO needed anywhere? +function isShortId(string){ + return hextest(string, 8); +}; -- cgit v1.2.3 From d62f66b1fb47f2075770d896f672748a4136e70b Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Mon, 23 Apr 2018 17:18:46 +0200 Subject: js: Key handling stubs, Error handling, refactoring -- * Error handling: introduced GPGMEJS_Error class that handles errors at a more centralized and consistent position * src/Connection.js: The nativeMessaging port now opens per session instead of per message. Some methods were added that reflect this change - added methods disconnect() and reconnect() - added connection status query * src/gpgmejs.js - stub for key deletion - error handling - high level API for changing connection status * src/gpgmejs_openpgpjs.js - added stubs for Key/Keyring handling according to current state of discussion. It is still subject to change * src/Helpers.js - toKeyIdArray creates an array of KeyIds, now accepting fingerprints, GPGMEJS_Key objects and openpgp Key objects. * Key objects (src/Key.js) Querying information about a key directly from gnupg. Currently a stub, only the Key.fingerprint is functional. * Keyring queries (src/Keyring.js): Listing and searching keys. Currently a stub. --- lang/js/src/Helpers.js | 54 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index eeb7a3c4..922ca06c 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -1,3 +1,5 @@ +import { GPGMEJS_Error } from "./Errors"; + /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * @@ -21,33 +23,46 @@ /** * Tries to return an array of fingerprints, either from input fingerprints or * from Key objects - * @param {String|Array} input Input value. + * @param {Key |Array| GPGME_Key | Array|String|Array} input + * @param {Boolean} nocheck if set, an empty result is acceptable * @returns {Array} Array of fingerprints. */ -export function toKeyIdArray(input){ + +export function toKeyIdArray(input, nocheck){ if (!input){ - return []; - // TODO: Warning or error here? Did we expect something or is "nothing" okay? + return (nocheck ===true)? [] : new GPGMEJS_Error('NO_KEYS'); + } + if (!Array.isArray(input)){ + input = [input]; } - if (input instanceof Array){ - let result = []; - for (let i=0; i < input.length; i++){ + let result = []; + for (let i=0; i < input.length; i++){ + if (typeof(input[i]) === 'string'){ if (isFingerprint(input[i]) === true){ result.push(input[i]); } else { - //TODO error? - console.log('gpgmejs/Helpers.js Warning: '+ - input[i] + - ' is not a valid key fingerprint and will not be used'); + GPGMEJS_Error + } + } else if (typeof(input[i]) === 'object'){ + let fpr = ''; + if (input[i] instanceof GPGME_Key){ + fpr = input[i].fingerprint; + } else if (input[i].hasOwnProperty(primaryKey) && + input[i].primaryKey.hasOwnProperty(getFingerprint)){ + fpr = input[i].primaryKey.getFingerprint(); + } + if (isFingerprint(fpr) === true){ + result.push(fpr); } + } else { + return new GPGMEJS_Error('WRONGTYPE'); } + } + if (result.length === 0){ + return (nocheck===true)? [] : new GPGMEJS_Error('NO_KEYS'); + } else { return result; - } else if (isFingerprint(input) === true) { - return [input]; } - console.log('gpgmejs/Helpers.js Warning: ' + input + - ' is not a valid key fingerprint and will not be used'); - return []; }; /** @@ -72,13 +87,14 @@ function hextest(key, len){ export function isFingerprint(string){ return hextest(string, 40); }; - -//TODO needed anywhere? +/** + * check if the input is a valid Hex string with a length of 16 + */ function isLongId(string){ return hextest(string, 16); }; -//TODO needed anywhere? +// TODO still not needed anywhere function isShortId(string){ return hextest(string, 8); }; -- cgit v1.2.3 From c72adc00965fe4fcedd9d18609211021a091b28b Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Wed, 25 Apr 2018 10:54:24 +0200 Subject: js: change in Error behaviour -- * Error objects will now return the error code if defined as error type in src/Errors.js, or do a console.log if it is a warning. Errors from the native gpgme-json will be marked as GNUPG_ERROR. --- lang/js/src/Helpers.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index 922ca06c..d9750ba7 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -1,5 +1,3 @@ -import { GPGMEJS_Error } from "./Errors"; - /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * @@ -19,18 +17,19 @@ import { GPGMEJS_Error } from "./Errors"; * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ +import { GPGMEJS_Error } from "./Errors"; /** * Tries to return an array of fingerprints, either from input fingerprints or * from Key objects * @param {Key |Array| GPGME_Key | Array|String|Array} input - * @param {Boolean} nocheck if set, an empty result is acceptable * @returns {Array} Array of fingerprints. */ export function toKeyIdArray(input, nocheck){ if (!input){ - return (nocheck ===true)? [] : new GPGMEJS_Error('NO_KEYS'); + GPGMEJS_Error('MSG_NO_KEYS'); + return []; } if (!Array.isArray(input)){ input = [input]; @@ -41,7 +40,7 @@ export function toKeyIdArray(input, nocheck){ if (isFingerprint(input[i]) === true){ result.push(input[i]); } else { - GPGMEJS_Error + GPGMEJS_Error('MSG_NOT_A_FPR'); } } else if (typeof(input[i]) === 'object'){ let fpr = ''; @@ -53,13 +52,16 @@ export function toKeyIdArray(input, nocheck){ } if (isFingerprint(fpr) === true){ result.push(fpr); + } else { + GPGMEJS_Error('MSG_NOT_A_FPR'); } } else { - return new GPGMEJS_Error('WRONGTYPE'); + return GPGMEJS_Error('PARAM_WRONG'); } } if (result.length === 0){ - return (nocheck===true)? [] : new GPGMEJS_Error('NO_KEYS'); + GPGMEJS_Error('MSG_NO_KEYS'); + return []; } else { return result; } -- cgit v1.2.3 From 1fb310cabe578625f96fce5d84ff6f0092c08d24 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Wed, 25 Apr 2018 15:59:36 +0200 Subject: js: Configuration and Error handling -- * gpgmejs_openpgpjs - unsuported values with no negative consequences can now reject, warn or be ignored, according to config.unconsidered_params - cleanup of unsupported/supported parameters and TODOS * A src/index.js init() now accepts a configuration object * Errors will now be derived from Error, offering more info and a stacktrace. * Fixed Connection.post() timeout triggering on wrong cases * Added comments in permittedOperations.js, which gpgme interactions are still unimplemented and should be added next --- lang/js/src/Helpers.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index d9750ba7..841c0eda 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -17,7 +17,7 @@ * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ -import { GPGMEJS_Error } from "./Errors"; +import { gpgme_error } from "./Errors"; /** * Tries to return an array of fingerprints, either from input fingerprints or @@ -28,7 +28,7 @@ import { GPGMEJS_Error } from "./Errors"; export function toKeyIdArray(input, nocheck){ if (!input){ - GPGMEJS_Error('MSG_NO_KEYS'); + gpgme_error('MSG_NO_KEYS'); return []; } if (!Array.isArray(input)){ @@ -40,7 +40,7 @@ export function toKeyIdArray(input, nocheck){ if (isFingerprint(input[i]) === true){ result.push(input[i]); } else { - GPGMEJS_Error('MSG_NOT_A_FPR'); + gpgme_error('MSG_NOT_A_FPR'); } } else if (typeof(input[i]) === 'object'){ let fpr = ''; @@ -53,14 +53,14 @@ export function toKeyIdArray(input, nocheck){ if (isFingerprint(fpr) === true){ result.push(fpr); } else { - GPGMEJS_Error('MSG_NOT_A_FPR'); + gpgme_error('MSG_NOT_A_FPR'); } } else { - return GPGMEJS_Error('PARAM_WRONG'); + return gpgme_error('PARAM_WRONG'); } } if (result.length === 0){ - GPGMEJS_Error('MSG_NO_KEYS'); + gpgme_error('MSG_NO_KEYS'); return []; } else { return result; -- cgit v1.2.3 From 3685913bf510a14b8cb324d980217d90489e6453 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Wed, 25 Apr 2018 19:45:39 +0200 Subject: js: First testing and improvements -- * Introduced Mocha/chai as testsuite. After development build 'npm test' should run the unit tests. Functionality exclusive to Browsers/WebExtensions cannot be run this way, so some other testing is still needed. - package.json: Added required development packages - .babelrc indirect configuration for mocha. ES6 transpiling needs some babel configuration, but mocha has no setting for it. - test/mocha.opts Vonfiguration for mocha runs * Fixed errors: - Helpers.js toKeyIdArray; isLongId is now exported - Key.js Key constructor failed - Message.js will not throw an Error during construction, a new message is now created with createMessage, which can return an Error or a GPGME_Message object * Tests: - test/Helpers: exports from Helpers.js, GPGME_Error handling - test/Message: first init test with bad parameters --- lang/js/src/Helpers.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index 841c0eda..9a69f851 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -18,6 +18,7 @@ * SPDX-License-Identifier: LGPL-2.1+ */ import { gpgme_error } from "./Errors"; +import { GPGME_Key } from "./Key"; /** * Tries to return an array of fingerprints, either from input fingerprints or @@ -26,7 +27,7 @@ import { gpgme_error } from "./Errors"; * @returns {Array} Array of fingerprints. */ -export function toKeyIdArray(input, nocheck){ +export function toKeyIdArray(input){ if (!input){ gpgme_error('MSG_NO_KEYS'); return []; @@ -46,7 +47,7 @@ export function toKeyIdArray(input, nocheck){ let fpr = ''; if (input[i] instanceof GPGME_Key){ fpr = input[i].fingerprint; - } else if (input[i].hasOwnProperty(primaryKey) && + } else if (input[i].hasOwnProperty('primaryKey') && input[i].primaryKey.hasOwnProperty(getFingerprint)){ fpr = input[i].primaryKey.getFingerprint(); } @@ -92,7 +93,7 @@ export function isFingerprint(string){ /** * check if the input is a valid Hex string with a length of 16 */ -function isLongId(string){ +export function isLongId(string){ return hextest(string, 16); }; -- cgit v1.2.3 From c755287ba845c4cbbf1d50e5aafecb2e687c7ac9 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Thu, 3 May 2018 18:03:22 +0200 Subject: js: Added browser testing for unit tests -- * Added unittests to be run inside a Browser. To be able to access the non-exposed functions and classes, a testing bundle will be created, containing the tests (unittests.js) and the items to be tested. * src/Helpelpers, src/Key, src/Keyring: fixed some errors found during testing. --- lang/js/src/Helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index 9a69f851..ea056fff 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -91,9 +91,9 @@ export function isFingerprint(string){ return hextest(string, 40); }; /** - * check if the input is a valid Hex string with a length of 16 + * TODO no usage; check if the input is a valid Hex string with a length of 16 */ -export function isLongId(string){ +function isLongId(string){ return hextest(string, 16); }; -- cgit v1.2.3 From cf075846fb48c8d71937100d2c45069d37d54a38 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Fri, 4 May 2018 12:56:59 +0200 Subject: js: fixing errors found by testing -- * Key.js: Error code for wrong parameter in createKey should be "PARAM_WRONG" * Helpers.js: The property openpgpjs-like Objects were checked for in toKeyIdArray was not defined. * src/permittedOperations.js: updated more expectations and assumptions for the native API --- lang/js/src/Helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index ea056fff..fd0e7200 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -48,7 +48,7 @@ export function toKeyIdArray(input){ if (input[i] instanceof GPGME_Key){ fpr = input[i].fingerprint; } else if (input[i].hasOwnProperty('primaryKey') && - input[i].primaryKey.hasOwnProperty(getFingerprint)){ + input[i].primaryKey.hasOwnProperty('getFingerprint')){ fpr = input[i].primaryKey.getFingerprint(); } if (isFingerprint(fpr) === true){ -- cgit v1.2.3 From 7a73d88aba106d571f121dc3230864c81a76e5db Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Fri, 25 May 2018 19:02:18 +0200 Subject: js: implement Key handling (1) -- * Keys can now be queried for information. Onne version queries gnug directly (asynchronous Promise in javascript terms), the cached version refreshes on demand. * Small fixes: src/Connection.js joins answers that stay json properly now --- lang/js/src/Helpers.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index fd0e7200..b26f40fb 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -90,10 +90,11 @@ function hextest(key, len){ export function isFingerprint(string){ return hextest(string, 40); }; + /** - * TODO no usage; check if the input is a valid Hex string with a length of 16 + * check if the input is a valid Hex string with a length of 16 */ -function isLongId(string){ +export function isLongId(string){ return hextest(string, 16); }; -- cgit v1.2.3 From 0356a667c5a8b4fdb4404cebb57475ed3f39ade9 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Wed, 6 Jun 2018 11:57:41 +0200 Subject: js: implement import/delete Key, some fixes -- * Keyring.js - implemented importKey: importing one or more armored public key blocks. - implemented deleteKey: deleting a public Key from gpg. * Key.js renamed property Key.armor to Key.armored * Helpers.js: toKeyIDArray does not complain anymore if there are no keys. Not having Keys in e.g. signing keys in encrypt is legitimate and common, the complaints were getting spammy * Errors.js: gpgme_errors now always pass an optional additional message, for easier debugging in minified code * Connection.js: Fix in gpgme-json responses containing objects * eslintrc.json: Start using eslint. A cleanup to conform to it is not done yet * Added further tests for the new functionality --- lang/js/src/Helpers.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index b26f40fb..5064d03e 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -29,7 +29,6 @@ import { GPGME_Key } from "./Key"; export function toKeyIdArray(input){ if (!input){ - gpgme_error('MSG_NO_KEYS'); return []; } if (!Array.isArray(input)){ @@ -61,7 +60,6 @@ export function toKeyIdArray(input){ } } if (result.length === 0){ - gpgme_error('MSG_NO_KEYS'); return []; } else { return result; -- cgit v1.2.3 From bfd3799d39df265882deedeee083fd5246a2f35d Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Wed, 6 Jun 2018 13:05:53 +0200 Subject: js: code cleanup (eslint) -- * trying to stick to eslint from now on for readability * As some attribution was lost in previous git confusions, I added my name into some of the licence headers --- lang/js/src/Helpers.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index 5064d03e..b01fbc30 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -16,14 +16,18 @@ * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ + * + * Author(s): + * Maximilian Krambach */ -import { gpgme_error } from "./Errors"; -import { GPGME_Key } from "./Key"; + +import { gpgme_error } from './Errors'; +import { GPGME_Key } from './Key'; /** * Tries to return an array of fingerprints, either from input fingerprints or - * from Key objects - * @param {Key |Array| GPGME_Key | Array|String|Array} input + * from Key objects (openpgp Keys or GPGME_Keys are both expected) + * @param {Object |Array| String|Array} input * @returns {Array} Array of fingerprints. */ @@ -48,7 +52,7 @@ export function toKeyIdArray(input){ fpr = input[i].fingerprint; } else if (input[i].hasOwnProperty('primaryKey') && input[i].primaryKey.hasOwnProperty('getFingerprint')){ - fpr = input[i].primaryKey.getFingerprint(); + fpr = input[i].primaryKey.getFingerprint(); } if (isFingerprint(fpr) === true){ result.push(fpr); @@ -64,7 +68,7 @@ export function toKeyIdArray(input){ } else { return result; } -}; +} /** * check if values are valid hexadecimal values of a specified length @@ -72,7 +76,7 @@ export function toKeyIdArray(input){ * @param {int} len the expected length of the value */ function hextest(key, len){ - if (!key || typeof(key) !== "string"){ + if (!key || typeof(key) !== 'string'){ return false; } if (key.length !== len){ @@ -80,23 +84,18 @@ function hextest(key, len){ } let regexp= /^[0-9a-fA-F]*$/i; return regexp.test(key); -}; +} /** * check if the input is a valid Hex string with a length of 40 */ export function isFingerprint(string){ return hextest(string, 40); -}; +} /** * check if the input is a valid Hex string with a length of 16 */ export function isLongId(string){ return hextest(string, 16); -}; - -// TODO still not needed anywhere -function isShortId(string){ - return hextest(string, 8); -}; +} -- cgit v1.2.3 From 4015f5b4983c8a4590aa71776880d8bc42c7918d Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Tue, 10 Jul 2018 14:32:26 +0200 Subject: js: documentation -- * Fixed errors: - src/Message.js post(): Set chunksize to defined default value instead of hardcoded - src/Keys.js: added getHasSecret() to refreshKey operation. * Reviewed and updated the documentation * non-documentation changes which do not affect functionality: - src/Errors: disabled a console.warn that is only useful for debugging - helpers.js: renamed "string" to "value" in isFingerprint and isLongId to avoid confusion - src/Keyring: prepare_sync, search are both explicitly set to false by default --- lang/js/src/Helpers.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index b01fbc30..0fd14994 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -26,11 +26,11 @@ import { GPGME_Key } from './Key'; /** * Tries to return an array of fingerprints, either from input fingerprints or - * from Key objects (openpgp Keys or GPGME_Keys are both expected) - * @param {Object |Array| String|Array} input - * @returns {Array} Array of fingerprints. + * from Key objects (openpgp Keys or GPGME_Keys are both accepted). + * + * @param {Object | Array | String | Array} input + * @returns {Array} Array of fingerprints, or an empty array */ - export function toKeyIdArray(input){ if (!input){ return []; @@ -44,6 +44,8 @@ export function toKeyIdArray(input){ if (isFingerprint(input[i]) === true){ result.push(input[i]); } else { + // MSG_NOT_A_FPR is just a console warning if warning enabled + // in src/Errors.js gpgme_error('MSG_NOT_A_FPR'); } } else if (typeof(input[i]) === 'object'){ @@ -71,9 +73,11 @@ export function toKeyIdArray(input){ } /** - * check if values are valid hexadecimal values of a specified length - * @param {*} key input value. + * Check if values are valid hexadecimal values of a specified length + * @param {String} key input value. * @param {int} len the expected length of the value + * @returns {Boolean} true if value passes test + * @private */ function hextest(key, len){ if (!key || typeof(key) !== 'string'){ @@ -87,15 +91,21 @@ function hextest(key, len){ } /** - * check if the input is a valid Hex string with a length of 40 + * check if the input is a valid Fingerprint + * (Hex string with a length of 40 characters) + * @param {String} value to check + * @returns {Boolean} true if value passes test */ -export function isFingerprint(string){ - return hextest(string, 40); +export function isFingerprint(value){ + return hextest(value, 40); } /** - * check if the input is a valid Hex string with a length of 16 + * check if the input is a valid gnupg long ID (Hex string with a length of 16 + * characters) + * @param {String} value to check + * @returns {Boolean} true if value passes test */ -export function isLongId(string){ - return hextest(string, 16); +export function isLongId(value){ + return hextest(value, 16); } -- cgit v1.2.3 From 754e799d35fd62d7a979452f44342934659908c7 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Fri, 17 Aug 2018 14:40:27 +0200 Subject: js: disallow bulk set data on key from outside -- * src/Key.js Key class is not exported anymore, as it should not be used directly anywhere. setKeyData is no more a method of the Key, (optional) data are now validated and set on Key creation and on updates, both from within this module, thus no longer exposing setKeyData to the outside. * createKey now gained an optional parameter which allows to set Key data at this point. --- lang/js/src/Helpers.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index 0fd14994..accc2af5 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -22,7 +22,6 @@ */ import { gpgme_error } from './Errors'; -import { GPGME_Key } from './Key'; /** * Tries to return an array of fingerprints, either from input fingerprints or @@ -50,7 +49,7 @@ export function toKeyIdArray(input){ } } else if (typeof(input[i]) === 'object'){ let fpr = ''; - if (input[i] instanceof GPGME_Key){ + if (input[i].hasOwnProperty('fingerprint')){ fpr = input[i].fingerprint; } else if (input[i].hasOwnProperty('primaryKey') && input[i].primaryKey.hasOwnProperty('getFingerprint')){ -- cgit v1.2.3 From 74684f24c663af12c88b196fecd5f44863b893e4 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Fri, 17 Aug 2018 19:20:35 +0200 Subject: js: decode arriving gpg message strings -- * Arriving strings (i.e. user id names, error messages) are not always in javascript encoding. This is an attempt to go through the whole gpgme answer (with the exception of payload data) and to fix the encoding of these --- lang/js/src/Helpers.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index accc2af5..379015f2 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -108,3 +108,30 @@ export function isFingerprint(value){ export function isLongId(value){ return hextest(value, 16); } + +/** + * Recursively decodes input (utf8) to output (utf-16; javascript) strings + * @param {Object | Array | String} property + */ +export function decode(property){ + if (typeof property === 'string'){ + return decodeURIComponent(escape(property)); + } else if (Array.isArray(property)){ + let res = []; + for (let arr=0; arr < property.length; arr++){ + res.push(decode(property[arr])); + } + return res; + } else if (typeof property === 'object'){ + const keys = Object.keys(property); + if (keys.length){ + let res = {}; + for (let k=0; k < keys.length; k++ ){ + res[keys[k]] = decode(property[keys[k]]); + } + return res; + } + return property; + } + return property; +} \ No newline at end of file -- cgit v1.2.3 From dd32daad0bb21e3d5567326d0b2e548ff8510431 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Mon, 20 Aug 2018 15:12:01 +0200 Subject: js: add and apply eslint rules -- * mainly spacing, see .eslintrc.json for details --- lang/js/src/Helpers.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'lang/js/src/Helpers.js') diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index 379015f2..ba4277ab 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -30,7 +30,7 @@ import { gpgme_error } from './Errors'; * @param {Object | Array | String | Array} input * @returns {Array} Array of fingerprints, or an empty array */ -export function toKeyIdArray(input){ +export function toKeyIdArray (input){ if (!input){ return []; } @@ -39,7 +39,7 @@ export function toKeyIdArray(input){ } let result = []; for (let i=0; i < input.length; i++){ - if (typeof(input[i]) === 'string'){ + if (typeof (input[i]) === 'string'){ if (isFingerprint(input[i]) === true){ result.push(input[i]); } else { @@ -47,7 +47,7 @@ export function toKeyIdArray(input){ // in src/Errors.js gpgme_error('MSG_NOT_A_FPR'); } - } else if (typeof(input[i]) === 'object'){ + } else if (typeof (input[i]) === 'object'){ let fpr = ''; if (input[i].hasOwnProperty('fingerprint')){ fpr = input[i].fingerprint; @@ -78,8 +78,8 @@ export function toKeyIdArray(input){ * @returns {Boolean} true if value passes test * @private */ -function hextest(key, len){ - if (!key || typeof(key) !== 'string'){ +function hextest (key, len){ + if (!key || typeof (key) !== 'string'){ return false; } if (key.length !== len){ @@ -95,7 +95,7 @@ function hextest(key, len){ * @param {String} value to check * @returns {Boolean} true if value passes test */ -export function isFingerprint(value){ +export function isFingerprint (value){ return hextest(value, 40); } @@ -105,7 +105,7 @@ export function isFingerprint(value){ * @param {String} value to check * @returns {Boolean} true if value passes test */ -export function isLongId(value){ +export function isLongId (value){ return hextest(value, 16); } @@ -113,7 +113,7 @@ export function isLongId(value){ * Recursively decodes input (utf8) to output (utf-16; javascript) strings * @param {Object | Array | String} property */ -export function decode(property){ +export function decode (property){ if (typeof property === 'string'){ return decodeURIComponent(escape(property)); } else if (Array.isArray(property)){ -- cgit v1.2.3