OpenPGP fix handling of importing keys

This commit is contained in:
the-djmaze 2024-03-04 12:42:23 +01:00
parent 5aa71a3ede
commit b5062624fa
2 changed files with 32 additions and 20 deletions

View file

@ -63,11 +63,7 @@ export class UserSettingsSecurity extends AbstractViewSettings {
importToOpenPGP() {
OpenPGPUserStore.isSupported() && Remote.request('GetPGPKeys',
(iError, oData) => {
if (!iError && oData.Result) {
oData.Result.forEach(key => OpenPGPUserStore.importKey(key));
}
}
(iError, oData) => !iError && oData.Result && OpenPGPUserStore.importKeys(oData.Result)
);
}

View file

@ -77,15 +77,11 @@ const
class OpenPgpKeyModel {
constructor(armor, key) {
this.key = key;
const aEmails = [];
if (key.users) {
key.users.forEach(user => user.userID.email && aEmails.push(user.userID.email));
}
this.id = key.getKeyID().toHex().toUpperCase();
this.fingerprint = key.getFingerprint();
this.can_encrypt = !!key.getEncryptionKey();
this.can_sign = !!key.getSigningKey();
this.emails = aEmails;
this.emails = key.users.map(user => user.userID.email).filter(email => email);
this.armor = armor;
this.askDelete = ko.observable(false);
this.openForDeletion = ko.observable(null).askDeleteHelper();
@ -93,6 +89,15 @@ class OpenPgpKeyModel {
// key.getPrimaryUser()
}
/*
get id() { return this.key.getKeyID().toHex().toUpperCase(); }
get fingerprint() { return this.key.getFingerprint(); }
get can_encrypt() { return !!this.key.getEncryptionKey(); }
get can_sign() { return !!this.key.getSigningKey(); }
get emails() { return this.key.users.map(user => user.userID.email).filter(email => email); }
get armor() { return this.key.armor(); }
*/
view() {
showScreenPopup(OpenPgpKeyPopupView, [this]);
}
@ -142,18 +147,29 @@ export const OpenPGPUserStore = new class {
}
importKey(armoredKey) {
window.openpgp && openpgp.readKey({armoredKey:armoredKey}).then(key => {
if (!key.err) {
key = new OpenPgpKeyModel(armoredKey, key);
const isPrivate = key.key.isPrivate(),
keys = isPrivate ? this.privateKeys : this.publicKeys;
if (!keys.find(entry => entry.fingerprint == key.fingerprint)) {
keys.push(key);
keys(sort(keys));
storeOpenPgpKeys(keys, isPrivate ? privateKeysItem : publicKeysItem);
this.importKeys([armoredKey]);
}
async importKeys(keys) {
if (window.openpgp) {
const privateKeys = this.privateKeys(),
publicKeys = this.publicKeys();
for (const armoredKey of keys) try {
let key = await openpgp.readKey({armoredKey:armoredKey});
if (!key.err) {
key = new OpenPgpKeyModel(armoredKey, key);
const keys = key.key.isPrivate() ? privateKeys : publicKeys;
keys.find(entry => entry.fingerprint == key.fingerprint)
|| keys.push(key);
}
} catch (e) {
console.error(e, armoredKey);
}
});
this.privateKeys(sort(privateKeys));
this.publicKeys(sort(publicKeys));
storeOpenPgpKeys(privateKeys, privateKeysItem);
storeOpenPgpKeys(publicKeys, publicKeysItem);
}
}
/**