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() { importToOpenPGP() {
OpenPGPUserStore.isSupported() && Remote.request('GetPGPKeys', OpenPGPUserStore.isSupported() && Remote.request('GetPGPKeys',
(iError, oData) => { (iError, oData) => !iError && oData.Result && OpenPGPUserStore.importKeys(oData.Result)
if (!iError && oData.Result) {
oData.Result.forEach(key => OpenPGPUserStore.importKey(key));
}
}
); );
} }

View file

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