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