aboutsummaryrefslogtreecommitdiffstats
path: root/lang/js/src/Helpers.js
diff options
context:
space:
mode:
authorMaximilian Krambach <[email protected]>2018-04-23 15:18:46 +0000
committerMaximilian Krambach <[email protected]>2018-04-23 15:18:46 +0000
commitd62f66b1fb47f2075770d896f672748a4136e70b (patch)
tree57cd622c1bfa8e3b9c22ef5a21b14915c3223c65 /lang/js/src/Helpers.js
parentjs: encrypt improvement and decrypt method (diff)
downloadgpgme-d62f66b1fb47f2075770d896f672748a4136e70b.tar.gz
gpgme-d62f66b1fb47f2075770d896f672748a4136e70b.zip
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.
Diffstat (limited to 'lang/js/src/Helpers.js')
-rw-r--r--lang/js/src/Helpers.js54
1 files changed, 35 insertions, 19 deletions
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<String>} input Input value.
+ * @param {Key |Array<Key>| GPGME_Key | Array<GPGME_Key>|String|Array<String>} input
+ * @param {Boolean} nocheck if set, an empty result is acceptable
* @returns {Array<String>} 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);
};