diff options
author | Maximilian Krambach <[email protected]> | 2018-08-22 16:37:46 +0000 |
---|---|---|
committer | Maximilian Krambach <[email protected]> | 2018-08-22 16:37:46 +0000 |
commit | f0409bbdafcbd4f8b0be099a6b3ce0d5352c9bcd (patch) | |
tree | fc94d23b0abef3c9e7c73d8a0165262f16e575e7 /lang/js/src/gpgmejs.js | |
parent | js: improve decryption performance (diff) | |
download | gpgme-f0409bbdafcbd4f8b0be099a6b3ce0d5352c9bcd.tar.gz gpgme-f0409bbdafcbd4f8b0be099a6b3ce0d5352c9bcd.zip |
js: make method parameters objects
--
* As requested by using parties, the options to be passed into the
methods are now objects, with the objects' properties better
describing what they do, and to avoid the need to type several nulls
in a method call if one wants the last parameter.
- src/Keyring.js, src/gpgme.js: Changed parameters and their
validations
- BrowserTest/*.js Had to adapt quite some calls to the new format
Diffstat (limited to 'lang/js/src/gpgmejs.js')
-rw-r--r-- | lang/js/src/gpgmejs.js | 147 |
1 files changed, 86 insertions, 61 deletions
diff --git a/lang/js/src/gpgmejs.js b/lang/js/src/gpgmejs.js index 513e4a56..e0398423 100644 --- a/lang/js/src/gpgmejs.js +++ b/lang/js/src/gpgmejs.js @@ -110,52 +110,63 @@ export class GpgME { /** * Encrypt (and optionally sign) data - * @param {String|Object} data text/data to be encrypted as String. Also - * accepts Objects with a getText method - * @param {inputKeys} publicKeys + * @param {Object} options + * @param {String|Object} options.data text/data to be encrypted as String. + * Also accepts Objects with a getText method + * @param {inputKeys} options.publicKeys * Keys used to encrypt the message - * @param {inputKeys} secretKeys (optional) Keys used to sign the + * @param {inputKeys} opions.secretKeys (optional) Keys used to sign the * message. If Keys are present, the operation requested is assumed * to be 'encrypt and sign' - * @param {Boolean} base64 (optional) The data will be interpreted as - * base64 encoded data. - * @param {Boolean} armor (optional) Request the output as armored + * @param {Boolean} options.base64 (optional) The data will be interpreted + * as base64 encoded data. + * @param {Boolean} options.armor (optional) Request the output as armored * block. - * @param {Boolean} wildcard (optional) If true, recipient information - * will not be added to the message. + * @param {Boolean} options.wildcard (optional) If true, recipient + * information will not be added to the message. * @param {Object} additional use additional valid gpg options as * defined in {@link permittedOperations} * @returns {Promise<encrypt_result>} Object containing the encrypted * message and additional info. * @async */ - encrypt (data, publicKeys, secretKeys, base64=false, armor=true, - wildcard=false, additional = {}){ + encrypt (options){ + if (!options || (typeof options !== 'object')){ + return Promise.reject(gpgme_error('PARAM_WRONG')); + } + if (!options.hasOwnProperty('data') + || !options.hasOwnProperty('publicKeys') + ){ + return Promise.reject(gpgme_error('MSG_INCOMPLETE')); + } let msg = createMessage('encrypt'); if (msg instanceof Error){ return Promise.reject(msg); } - msg.setParameter('armor', armor); - msg.setParameter('always-trust', true); - if (base64 === true) { + if (!options.hasOwnProperty('armor')){ + options.armor = true; + } + msg.setParameter('armor', options.armor); + + if (options.base64 === true) { msg.setParameter('base64', true); } - let pubkeys = toKeyIdArray(publicKeys); + let pubkeys = toKeyIdArray(options.publicKeys); msg.setParameter('keys', pubkeys); - let sigkeys = toKeyIdArray(secretKeys); + let sigkeys = toKeyIdArray(options.secretKeys); if (sigkeys.length > 0) { msg.setParameter('signing_keys', sigkeys); } - putData(msg, data); - if (wildcard === true){ + putData(msg, options.data); + if (options.wildcard === true){ msg.setParameter('throw-keyids', true); } - if (additional){ - let additional_Keys = Object.keys(additional); + if (options.additional){ + let additional_Keys = Object.keys(options.additional); for (let k = 0; k < additional_Keys.length; k++) { try { msg.setParameter(additional_Keys[k], - additional[additional_Keys[k]]); + options.additional[additional_Keys[k]]); } catch (error){ return Promise.reject(error); @@ -171,17 +182,21 @@ export class GpgME { /** * Decrypts a Message - * @param {String|Object} data text/data to be decrypted. Accepts + * @param {Object} options + * @param {String|Object} options.data text/data to be decrypted. Accepts * Strings and Objects with a getText method - * @param {Boolean} base64 (optional) false if the data is an armored - * block, true if it is base64 encoded binary data - * @param {Boolean} binary (optional) if true, treat the decoded data as - * binary, and return the data as Uint8Array + * @param {Boolean} options.base64 (optional) false if the data is an + * armored block, true if it is base64 encoded binary data + * @param {Boolean} options.binary (optional) if true, treat the decoded + * data as binary, and return the data as Uint8Array * @returns {Promise<decrypt_result>} Decrypted Message and information * @async */ - decrypt (data, base64=false, binary){ - if (data === undefined){ + decrypt (options){ + if (!options || (typeof options !== 'object')){ + return Promise.reject('PARAM_WRONG'); + } + if (!options.data){ return Promise.reject(gpgme_error('MSG_EMPTY')); } let msg = createMessage('decrypt'); @@ -189,13 +204,13 @@ export class GpgME { if (msg instanceof Error){ return Promise.reject(msg); } - if (base64 === true){ + if (options.base64 === true){ msg.setParameter('base64', true); } - if (binary === true){ + if (options.binary === true){ msg.expected = 'binary'; } - putData(msg, data); + putData(msg, options.data); return new Promise(function (resolve, reject){ msg.post().then(function (result){ let _result = { data: result.data }; @@ -229,44 +244,49 @@ export class GpgME { /** * Sign a Message - * @param {String|Object} data text/data to be signed. Accepts Strings - * and Objects with a getText method. - * @param {inputKeys} keys The key/keys to use for signing - * @param {String} mode The signing mode. Currently supported: + * @param {Object} options Signing options + * @param {String|Object} options.data text/data to be signed. Accepts + * Strings and Objects with a getText method. + * @param {inputKeys} options.keys The key/keys to use for signing + * @param {String} options.mode The signing mode. Currently supported: * 'clearsign':The Message is embedded into the signature; * 'detached': The signature is stored separately - * @param {Boolean} base64 input is considered base64 + * @param {Boolean} options.base64 input is considered base64 * @returns {Promise<signResult>} * @async */ - sign (data, keys, mode='clearsign', base64=false) { - if (data === undefined){ + sign (options){ + if ( + !options || (typeof options !== 'object')){ + return Promise.reject(gpgme_error('PARAM_WRONG')); + } + if (!options.data){ return Promise.reject(gpgme_error('MSG_EMPTY')); } - let key_arr = toKeyIdArray(keys); + if (!options.mode) { + options.mode = 'clearsign'; + } + let key_arr = toKeyIdArray(options.keys); if (key_arr.length === 0){ return Promise.reject(gpgme_error('MSG_NO_KEYS')); } let msg = createMessage('sign'); msg.setParameter('keys', key_arr); - if (base64 === true){ + if (options.base64 === true){ msg.setParameter('base64', true); } - msg.setParameter('mode', mode); - putData(msg, data); + msg.setParameter('mode', options.mode); + putData(msg, options.data); return new Promise(function (resolve,reject) { - if (mode ==='detached'){ - msg.expected ='binary'; - } msg.post().then( function (message) { - if (mode === 'clearsign'){ + if (options.mode === 'clearsign'){ resolve({ data: message.data } ); - } else if (mode === 'detached') { + } else if (options.mode === 'detached') { resolve({ - data: data, + data: options.data, signature: message.data }); } @@ -278,28 +298,33 @@ export class GpgME { /** * Verifies data. - * @param {String|Object} data text/data to be verified. Accepts Strings - * and Objects with a getText method - * @param {String} (optional) A detached signature. If not present, + * @param {Object} options + * @param {String|Object} options.data text/data to be verified. Accepts + * Strings and Objects with a getText method + * @param {String} options.signature A detached signature. If not present, * opaque mode is assumed - * @param {Boolean} (optional) Data and signature are base64 encoded + * @param {Boolean} options.base64 Indicating that data and signature are + * base64 encoded * @returns {Promise<verifyResult>} *@async */ - verify (data, signature, base64 = false){ + verify (options){ + if (!options || (typeof options !== 'object') || !options.data){ + return Promise.reject(gpgme_error('PARAM_WRONG')); + } let msg = createMessage('verify'); - let dt = putData(msg, data); + let dt = putData(msg, options.data); if (dt instanceof Error){ return Promise.reject(dt); } - if (signature){ - if (typeof (signature)!== 'string'){ + if (options.signature){ + if (typeof signature !== 'string'){ return Promise.reject(gpgme_error('PARAM_WRONG')); } else { msg.setParameter('signature', signature); } } - if (base64 === true){ + if (options.base64 === true){ msg.setParameter('base64', true); } return new Promise(function (resolve, reject){ @@ -342,14 +367,14 @@ function putData (message, data){ } if (!data){ return gpgme_error('PARAM_WRONG'); - } else if (typeof (data) === 'string') { + } else if (typeof data === 'string') { message.setParameter('data', data); } else if ( - typeof (data) === 'object' && - typeof (data.getText) === 'function' + (typeof data === 'object') && + (typeof data.getText === 'function') ){ let txt = data.getText(); - if (typeof (txt) === 'string'){ + if (typeof txt === 'string'){ message.setParameter('data', txt); } else { return gpgme_error('PARAM_WRONG'); |