js: get default key fixes

--

* src/Keyring.js: The answer was not parsed correctly, so a config was
  being ignored.

* If no config is set, we return the first non-invalid key with a
  secret, instead of the first key (which may be e.g. an expired one)
This commit is contained in:
Maximilian Krambach 2018-08-16 12:03:30 +02:00
parent aeb065acc9
commit 715cdc0d7d

View File

@ -176,12 +176,13 @@ export class GPGME_Keyring {
let msg = createMessage('config_opt');
msg.setParameter('component', 'gpg');
msg.setParameter('option', 'default-key');
msg.post().then(function(response){
if (response.value !== undefined
&& response.value.hasOwnProperty('string')
&& typeof(response.value.string) === 'string'
){
me.getKeys(response.value.string,true).then(
msg.post().then(function(resp){
if (resp.option !== undefined
&& resp.option.hasOwnProperty('value')
&& resp.option.value.length === 1
&& resp.option.value[0].hasOwnProperty('string')
&& typeof(resp.option.value[0].string) === 'string'){
me.getKeys(resp.option.value[0].string, true).then(
function(keys){
if(keys.length === 1){
resolve(keys[0]);
@ -198,7 +199,14 @@ export class GPGME_Keyring {
if (result.keys.length === 0){
reject(gpgme_error('KEY_NO_DEFAULT'));
} else {
resolve(result.keys[0]);
for (let i=0; i< result.keys.length; i++ ) {
if (result.keys[i].get('invalid') === false) {
resolve(result.keys[i]);
break;
} else if (i === result.keys.length - 1){
reject(gpgme_error('KEY_NO_DEFAULT'));
}
}
}
}, function(error){
reject(error);