From b5dbd0cc7a7fc99c33779e84f138427d6dc3993e Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Sun, 26 May 2024 12:53:35 +0200 Subject: [PATCH] OpenPGP.js automatically import backup keys from server --- dev/Settings/User/Security.js | 6 +----- dev/Stores/User/OpenPGP.js | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/dev/Settings/User/Security.js b/dev/Settings/User/Security.js index bbb8e9dd5..5ee4a8f62 100644 --- a/dev/Settings/User/Security.js +++ b/dev/Settings/User/Security.js @@ -18,8 +18,6 @@ import { OpenPgpGeneratePopupView } from 'View/Popup/OpenPgpGenerate'; import { SMimeUserStore } from 'Stores/User/SMime'; import { SMimeImportPopupView } from 'View/Popup/SMimeImport'; -import Remote from 'Remote/User/Fetch'; - export class UserSettingsSecurity extends AbstractViewSettings { constructor() { super(); @@ -62,9 +60,7 @@ export class UserSettingsSecurity extends AbstractViewSettings { } importToOpenPGP() { - OpenPGPUserStore.isSupported() && Remote.request('GetPGPKeys', - (iError, oData) => !iError && oData.Result && OpenPGPUserStore.importKeys(oData.Result) - ); + OpenPGPUserStore.loadBackupKeys(); } importToSMime() { diff --git a/dev/Stores/User/OpenPGP.js b/dev/Stores/User/OpenPGP.js index b3914e9c6..55acd484d 100644 --- a/dev/Stores/User/OpenPGP.js +++ b/dev/Stores/User/OpenPGP.js @@ -16,6 +16,8 @@ import { Passphrases } from 'Storage/Passphrases'; import { baseCollator } from 'Common/Translator'; const + loaded = () => !!window.openpgp, + findOpenPGPKey = (keys, query/*, sign*/) => keys.find(key => key.for(query) || query == key.id || query == key.fingerprint @@ -134,23 +136,36 @@ export const OpenPGPUserStore = new class { } loadKeyrings() { - if (window.openpgp) { - loadOpenPgpKeys(publicKeysItem).then(keys => { + if (loaded()) { + loadOpenPgpKeys(publicKeysItem) + .then(keys => { this.publicKeys(dedup(keys)); console.log('openpgp.js public keys loaded'); - }); - loadOpenPgpKeys(privateKeysItem).then(keys => { - this.privateKeys(dedup(keys)); - console.log('openpgp.js private keys loaded'); + }) + .finally(() => { + loadOpenPgpKeys(privateKeysItem) + .then(keys => { + this.privateKeys(dedup(keys)); + console.log('openpgp.js private keys loaded'); + }) + .finally(() => { + /*SettingsGet('loadBackupKeys') && */this.loadBackupKeys(); + }); }); } } + loadBackupKeys() { + Remote.request('GetPGPKeys', + (iError, oData) => !iError && oData.Result && this.importKeys(oData.Result) + ); + } + /** * @returns {boolean} */ isSupported() { - return !!window.openpgp; + return loaded(); } importKey(armoredKey) { @@ -158,7 +173,7 @@ export const OpenPGPUserStore = new class { } async importKeys(keys) { - if (window.openpgp) { + if (loaded()) { const privateKeys = this.privateKeys(), publicKeys = this.publicKeys(); for (const armoredKey of keys) try { @@ -185,7 +200,7 @@ export const OpenPGPUserStore = new class { keyPair.revocationCertificate */ storeKeyPair(keyPair) { - if (window.openpgp) { + if (loaded()) { openpgp.readKey({armoredKey:keyPair.publicKey}).then(key => { this.publicKeys.push(new OpenPgpKeyModel(keyPair.publicKey, key)); storeOpenPgpKeys(this.publicKeys, publicKeysItem);