[git] GPGME - branch, master, updated. gpgme-1.11.1-233-g3fd6837

by Maximilian Krambach cvs at cvs.gnupg.org
Thu Aug 23 12:17:14 CEST 2018


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GnuPG Made Easy".

The branch, master has been updated
       via  3fd6837fce9b339b8b09a800a969e0950e78250c (commit)
       via  60dc499abd89f7e62a7b9cad943a96faa65187d5 (commit)
      from  24a00058652233775cbe51446cba337b70cefdf1 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 3fd6837fce9b339b8b09a800a969e0950e78250c
Author: Maximilian Krambach <maximilian.krambach at intevation.de>
Date:   Thu Aug 23 12:15:59 2018 +0200

    js: use destructured option parameters
    
    --
    * Adds to f0409bbdafcbd4f8b0be099a6b3ce0d5352c9bcd and makes use of
      destructuring, allowing for defaults, and cleaning up the
      validation.

diff --git a/lang/js/src/Keyring.js b/lang/js/src/Keyring.js
index d8fd8c8..5902708 100644
--- a/lang/js/src/Keyring.js
+++ b/lang/js/src/Keyring.js
@@ -35,7 +35,7 @@ export class GPGME_Keyring {
     /**
      * Queries Keys (all Keys or a subset) from gnupg.
      *
-     * @param {Object} options
+     * @param {Object} options:
      * @param {String | Array<String>} options.pattern (optional) A pattern to
      * search for in userIds or KeyIds.
      * @param {Boolean} options.prepare_sync (optional) if set to true, most
@@ -49,24 +49,14 @@ export class GPGME_Keyring {
      * @static
      * @async
      */
-    getKeys (options){
-        if (options && typeof options !== 'object'){
-            return Promise.reject(gpgme_error('PARAM_WRONG'));
-        }
+    getKeys ({ pattern, prepare_sync = false, search = false }){
         return new Promise(function (resolve, reject) {
             let msg = createMessage('keylist');
-            if (options && options.pattern) {
-                if (
-                    typeof options.pattern === 'string'
-                    || Array.isArray(options.pattern)
-                ){
-                    msg.setParameter('keys', options.pattern);
-                } else {
-                    reject(gpgme_error('PARAM_WRONG'));
-                }
+            if (pattern) {
+                msg.setParameter('keys', pattern);
             }
             msg.setParameter('sigs', true);
-            if (options && options.search === true){
+            if (search === true){
                 msg.setParameter('locate', true);
             }
             msg.post().then(function (result){
@@ -75,11 +65,11 @@ export class GPGME_Keyring {
                     resolve([]);
                 } else {
                     let secondrequest;
-                    if (options && options.prepare_sync === true) {
+                    if (prepare_sync === true) {
                         secondrequest = function () {
                             let msg2 = createMessage('keylist');
-                            if (options.pattern){
-                                msg2.setParameter('keys', options.pattern);
+                            if (pattern){
+                                msg2.setParameter('keys', pattern);
                             }
                             msg2.setParameter('secret', true);
                             return msg2.post();
@@ -91,7 +81,7 @@ export class GPGME_Keyring {
                     }
                     secondrequest().then(function (answer) {
                         for (let i=0; i < result.keys.length; i++){
-                            if (options.prepare_sync === true){
+                            if (prepare_sync === true){
                                 if (answer && answer.keys) {
                                     for (let j=0;
                                         j < answer.keys.length; j++ ){
@@ -111,7 +101,7 @@ export class GPGME_Keyring {
                                 }
                             }
                             let k = createKey(result.keys[i].fingerprint,
-                                !options.prepare_sync, result.keys[i]);
+                                !prepare_sync, result.keys[i]);
                             resultset.push(k);
                         }
                         resolve(resultset);
@@ -147,27 +137,19 @@ export class GPGME_Keyring {
      * @static
      * @async
      */
-    getKeysArmored (options) {
-        if (options && typeof options !== 'object'){
-            return Promise.reject(gpgme_error('PARAM_WRONG'));
-        }
+    getKeysArmored ({ pattern, with_secret_fpr }) {
         return new Promise(function (resolve, reject) {
             let msg = createMessage('export');
             msg.setParameter('armor', true);
-            if (options.with_secret_fpr === true) {
+            if (with_secret_fpr === true) {
                 msg.setParameter('with-sec-fprs', true);
             }
-            if (options.pattern){
-                if (
-                    typeof options.pattern === 'string'
-                    || Array.isArray(options.pattern)
-                ){
-                    msg.setParameter('keys', options.pattern);
-                }
+            if (pattern){
+                msg.setParameter('keys', pattern);
             }
             msg.post().then(function (answer){
                 const result = { armored: answer.data };
-                if (options.with_secret_fpr === true){
+                if (with_secret_fpr === true){
                     if (answer.hasOwnProperty('sec-fprs')){
                         result.secret_fprs = answer['sec-fprs'];
                     } else {
@@ -404,39 +386,27 @@ export class GPGME_Keyring {
      * @return {Promise<Key|GPGME_Error>}
      * @async
      */
-    generateKey (options){
-        if (!options
-            || typeof options !== 'object'
-            || typeof options.userId !== 'string'
+    generateKey ({ userId, algo = 'default', expires= 0, subkey_algo }){
+        if (typeof userId !== 'string'
             // eslint-disable-next-line no-use-before-define
-            || ( options.algo && supportedKeyAlgos.indexOf(options.algo) < 0 )
-            || ( options.expires && !(
-                Number.isInteger(options.expires) || options.expires < 0 ) )
+            || (algo && supportedKeyAlgos.indexOf(algo) < 0 )
+            || (!Number.isInteger(expires) || expires < 0 )
         ){
             return Promise.reject(gpgme_error('PARAM_WRONG'));
         }
         // eslint-disable-next-line no-use-before-define
-        if (options.subkey_algo && supportedKeyAlgos.indexOf(
-            options.subkey_algo) < 0
-        ){
+        if (subkey_algo && supportedKeyAlgos.indexOf(subkey_algo) < 0){
             return Promise.reject(gpgme_error('PARAM_WRONG'));
         }
         let me = this;
         return new Promise(function (resolve, reject){
             let msg = createMessage('createkey');
-            msg.setParameter('userid', options.userId);
-            if (!options.algo){
-                options.algo === 'default';
-            }
-            msg.setParameter('algo', options.algo);
-            if (options.subkey_algo) {
-                msg.setParameter('subkey-algo', options.subkey_algo );
-            }
-            if (options.expires){
-                msg.setParameter('expires', options.expires);
-            } else {
-                msg.setParameter('expires', 0);
+            msg.setParameter('userid', userId);
+            msg.setParameter('algo', algo);
+            if (subkey_algo) {
+                msg.setParameter('subkey-algo',subkey_algo );
             }
+            msg.setParameter('expires', expires);
             msg.post().then(function (response){
                 me.getKeys(response.fingerprint, true).then(
                     // TODO prepare_sync?
diff --git a/lang/js/src/gpgmejs.js b/lang/js/src/gpgmejs.js
index ac64030..ffee719 100644
--- a/lang/js/src/gpgmejs.js
+++ b/lang/js/src/gpgmejs.js
@@ -134,43 +134,39 @@ export class GpgME {
      * message and additional info.
      * @async
      */
-    encrypt (options){
-        if (!options || (typeof options !== 'object')){
-            return Promise.reject(gpgme_error('PARAM_WRONG'));
-        }
-        if (!options.hasOwnProperty('data')
-            || !options.hasOwnProperty('publicKeys')
-        ){
+    encrypt ({ data, publicKeys, secretKeys, base64 = false, armor = true,
+        wildcard, additional = {} }){
+        if (!data || !publicKeys){
             return Promise.reject(gpgme_error('MSG_INCOMPLETE'));
         }
         let msg = createMessage('encrypt');
         if (msg instanceof Error){
             return Promise.reject(msg);
         }
-        if (!options.hasOwnProperty('armor')){
-            options.armor = true;
-        }
-        msg.setParameter('armor', options.armor);
+        msg.setParameter('armor', armor);
 
-        if (options.base64 === true) {
+        if (base64 === true) {
             msg.setParameter('base64', true);
         }
-        let pubkeys = toKeyIdArray(options.publicKeys);
+        let pubkeys = toKeyIdArray(publicKeys);
+        if (!pubkeys.length) {
+            return Promise.reject(gpgme_error('MSG_NO_KEYS'));
+        }
         msg.setParameter('keys', pubkeys);
-        let sigkeys = toKeyIdArray(options.secretKeys);
+        let sigkeys = toKeyIdArray(secretKeys);
         if (sigkeys.length > 0) {
             msg.setParameter('signing_keys', sigkeys);
         }
-        putData(msg, options.data);
-        if (options.wildcard === true){
+        putData(msg, data);
+        if (wildcard === true){
             msg.setParameter('throw-keyids', true);
         }
-        if (options.additional){
-            let additional_Keys = Object.keys(options.additional);
+        if (additional){
+            let additional_Keys = Object.keys(additional);
             for (let k = 0; k < additional_Keys.length; k++) {
                 try {
                     msg.setParameter(additional_Keys[k],
-                        options.additional[additional_Keys[k]]);
+                        additional[additional_Keys[k]]);
                 }
                 catch (error){
                     return Promise.reject(error);
@@ -197,11 +193,8 @@ export class GpgME {
     * @returns {Promise<decrypt_result>} Decrypted Message and information
     * @async
     */
-    decrypt (options){
-        if (!options || (typeof options !== 'object')){
-            return Promise.reject('PARAM_WRONG');
-        }
-        if (!options.data){
+    decrypt ({ data, base64, expect }){
+        if (!data){
             return Promise.reject(gpgme_error('MSG_EMPTY'));
         }
         let msg = createMessage('decrypt');
@@ -209,13 +202,13 @@ export class GpgME {
         if (msg instanceof Error){
             return Promise.reject(msg);
         }
-        if (options.base64 === true){
+        if (base64 === true){
             msg.setParameter('base64', true);
         }
-        if (options.expect === 'base64' || options.expect === 'uint8'){
-            msg.expected = options.expect;
+        if (expect === 'base64' || expect === 'uint8'){
+            msg.expected = expect;
         }
-        putData(msg, options.data);
+        putData(msg, data);
         return new Promise(function (resolve, reject){
             msg.post().then(function (result){
                 let _result = { data: result.data };
@@ -260,38 +253,32 @@ export class GpgME {
      * @returns {Promise<signResult>}
      * @async
      */
-    sign (options){
-        if (
-            !options || (typeof options !== 'object')){
-            return Promise.reject(gpgme_error('PARAM_WRONG'));
-        }
-        if (!options.data){
+    sign ({ data, keys, mode = 'clearsign', base64 }){
+        if (!data){
             return Promise.reject(gpgme_error('MSG_EMPTY'));
         }
-        if (!options.mode) {
-            options.mode = 'clearsign';
-        }
-        let key_arr = toKeyIdArray(options.keys);
+        let key_arr = toKeyIdArray(keys);
         if (key_arr.length === 0){
             return Promise.reject(gpgme_error('MSG_NO_KEYS'));
         }
-        let msg = createMessage('sign');
 
+        let msg = createMessage('sign');
         msg.setParameter('keys', key_arr);
-        if (options.base64 === true){
+        if (base64 === true){
             msg.setParameter('base64', true);
         }
-        msg.setParameter('mode', options.mode);
-        putData(msg, options.data);
+        msg.setParameter('mode', mode);
+        putData(msg, data);
+
         return new Promise(function (resolve,reject) {
             msg.post().then( function (message) {
-                if (options.mode === 'clearsign'){
+                if (mode === 'clearsign'){
                     resolve({
                         data: message.data }
                     );
-                } else if (options.mode === 'detached') {
+                } else if (mode === 'detached') {
                     resolve({
-                        data: options.data,
+                        data: data,
                         signature: message.data
                     });
                 }
@@ -313,23 +300,23 @@ export class GpgME {
      * @returns {Promise<verifyResult>}
      *@async
     */
-    verify (options){
-        if (!options || (typeof options !== 'object') || !options.data){
+    verify ({ data, signature, base64 }){
+        if (!data){
             return Promise.reject(gpgme_error('PARAM_WRONG'));
         }
         let msg = createMessage('verify');
-        let dt = putData(msg, options.data);
+        let dt = putData(msg, data);
         if (dt instanceof Error){
             return Promise.reject(dt);
         }
-        if (options.signature){
+        if (signature){
             if (typeof signature !== 'string'){
                 return Promise.reject(gpgme_error('PARAM_WRONG'));
             } else {
                 msg.setParameter('signature', signature);
             }
         }
-        if (options.base64 === true){
+        if (base64 === true){
             msg.setParameter('base64', true);
         }
         return new Promise(function (resolve, reject){
diff --git a/lang/js/unittests.js b/lang/js/unittests.js
index 212effd..8f1ffb6 100644
--- a/lang/js/unittests.js
+++ b/lang/js/unittests.js
@@ -262,7 +262,7 @@ function unittests (){
         it('Loading Keys from Keyring, to be used synchronously',
             function (done){
                 let keyring = new GPGME_Keyring;
-                keyring.getKeys(null, true).then(function (result){
+                keyring.getKeys({ prepare_sync: true }).then(function (result){
                     expect(result).to.be.an('array');
                     expect(result[0].get('hasSecret')).to.be.a('boolean');
                     done();
@@ -273,7 +273,9 @@ function unittests (){
         it('Loading specific Key from Keyring, to be used synchronously',
             function (done){
                 let keyring = new GPGME_Keyring;
-                keyring.getKeys(kp.validKeyFingerprint, true).then(
+                keyring.getKeys({
+                    pattern: kp.validKeyFingerprint,
+                    prepare_sync: true }).then(
                     function (result){
                         expect(result).to.be.an('array');
                         expect(result[0].get('hasSecret')).to.be.a('boolean');

commit 60dc499abd89f7e62a7b9cad943a96faa65187d5
Author: Maximilian Krambach <maximilian.krambach at intevation.de>
Date:   Thu Aug 23 11:28:18 2018 +0200

    js: update getDefaultKey to more precise logic
    
    --
    
    * src/Keyring.js: Adapted Keyring.getDefaultKey() to my current
      understanding  of a default signing key: either the default key set
      in the gpg config, or 'the first usable private key' - usability
      meaning  'not invalid, expired, revoked, and can be used for
      signing'. It should be the same key used as in command line when
      doing a --sign operation.
      In case the user has a smartcard plugged in, we currently
      won't know of this here, so our choice may differ. But as we do all
      javascript-binding sign operations with the key  fingerprint
      explicitly set, this should not be a real problem. This method is
      seen more as a convenience to tell using librarys which key
      represents the main user.

diff --git a/lang/js/src/Keyring.js b/lang/js/src/Keyring.js
index d6ba1d6..d8fd8c8 100644
--- a/lang/js/src/Keyring.js
+++ b/lang/js/src/Keyring.js
@@ -221,7 +221,12 @@ export class GPGME_Keyring {
                             reject(gpgme_error('KEY_NO_DEFAULT'));
                         } else {
                             for (let i=0; i< result.keys.length; i++ ) {
-                                if (result.keys[i].invalid === false) {
+                                if (
+                                    result.keys[i].invalid === false &&
+                                    result.keys[i].expired === false &&
+                                    result.keys[i].revoked === false &&
+                                    result.keys[i].can_sign === true
+                                ) {
                                     let k = createKey(
                                         result.keys[i].fingerprint,
                                         !prepare_sync,

-----------------------------------------------------------------------

Summary of changes:
 lang/js/src/Keyring.js | 87 ++++++++++++++++++--------------------------------
 lang/js/src/gpgmejs.js | 87 +++++++++++++++++++++-----------------------------
 lang/js/unittests.js   |  6 ++--
 3 files changed, 72 insertions(+), 108 deletions(-)


hooks/post-receive
-- 
GnuPG Made Easy
http://git.gnupg.org




More information about the Gnupg-commits mailing list