diff --git a/dev/Stores/User/GnuPG.js b/dev/Stores/User/GnuPG.js index 0376ac379..33961d3d6 100644 --- a/dev/Stores/User/GnuPG.js +++ b/dev/Stores/User/GnuPG.js @@ -11,8 +11,12 @@ import Remote from 'Remote/User/Fetch'; import { showScreenPopup } from 'Knoin/Knoin'; import { OpenPgpKeyPopupView } from 'View/Popup/OpenPgpKey'; +import { AskPopupView } from 'View/Popup/Ask'; const + askPassphrase = async privateKey => + await AskPopupView.password('GnuPG key
' + privateKey.id + ' ' + privateKey.emails[0]), + findGnuPGKey = (keys, query, sign) => keys.find(key => key[sign ? 'can_sign' : 'can_decrypt'] @@ -169,7 +173,7 @@ export const GnuPGUserStore = new class { Uid: message.uid, PartId: pgpInfo.PartId, KeyId: key.id, - Passphrase: prompt('GnuPG Passphrase for ' + key.id + ' ' + key.uids[0].uid), + Passphrase: await askPassphrase(key), Data: '' // message.plain() optional } if (null !== params.Passphrase) { @@ -199,4 +203,12 @@ export const GnuPGUserStore = new class { } } + async sign(/*text, privateKey, detached*/) { + throw 'Sign failed'; + } + + async encrypt(/*text, recipients, signPrivateKey*/) { + throw 'Encrypt failed'; + } + }; diff --git a/dev/Stores/User/OpenPGP.js b/dev/Stores/User/OpenPGP.js index bc67b7dc5..71aa673c0 100644 --- a/dev/Stores/User/OpenPGP.js +++ b/dev/Stores/User/OpenPGP.js @@ -11,6 +11,7 @@ import Remote from 'Remote/User/Fetch'; import { showScreenPopup } from 'Knoin/Knoin'; import { OpenPgpKeyPopupView } from 'View/Popup/OpenPgpKey'; +import { AskPopupView } from 'View/Popup/Ask'; const findOpenPGPKey = (keys, query/*, sign*/) => @@ -18,6 +19,9 @@ const key.emails.includes(query) || query == key.id || query == key.fingerprint ), + askPassphrase = async privateKey => + await AskPopupView.password('OpenPGP.js key
' + privateKey.id + ' ' + privateKey.emails[0]), + /** * OpenPGP.js v5 removed the localStorage (keyring) * This should be compatible with the old OpenPGP.js v2 @@ -180,8 +184,8 @@ export const OpenPGPUserStore = new class { } } if (privateKey) try { - // Ask passphrase of private key - const passphrase = prompt('OpenPGP.js Passphrase for ' + privateKey.id + ' ' + privateKey.emails[0]); + const passphrase = await askPassphrase(privateKey); + if (null !== passphrase) { const publicKey = findOpenPGPKey(this.publicKeys, sender/*, sign*/), @@ -243,7 +247,7 @@ export const OpenPGPUserStore = new class { * https://docs.openpgpjs.org/global.html#sign */ async sign(text, privateKey, detached) { - const passphrase = prompt('OpenPGP.js Passphrase for ' + privateKey.id + ' ' + privateKey.emails[0]); + const passphrase = await askPassphrase(privateKey); if (null !== passphrase) { privateKey = await openpgp.decryptKey({ privateKey: privateKey.key, @@ -258,7 +262,7 @@ export const OpenPGPUserStore = new class { detached: !!detached }); } - return false; + throw 'Sign cancelled'; } /** @@ -269,7 +273,7 @@ export const OpenPGPUserStore = new class { recipients = recipients.map(email => this.publicKeys().find(key => key.emails.includes(email))).filter(key => key); if (count === recipients.length) { if (signPrivateKey) { - const passphrase = prompt('OpenPGP.js Passphrase for ' + signPrivateKey.id + ' ' + signPrivateKey.emails[0]); + const passphrase = await askPassphrase(signPrivateKey); if (null === passphrase) { return; } @@ -285,6 +289,7 @@ export const OpenPGPUserStore = new class { // signature }); } + throw 'Encrypt failed'; } }; diff --git a/dev/View/Popup/Ask.js b/dev/View/Popup/Ask.js index 87b7e64e5..dadf6a9a2 100644 --- a/dev/View/Popup/Ask.js +++ b/dev/View/Popup/Ask.js @@ -42,8 +42,8 @@ class AskPopupView extends AbstractViewPopup { * @param {boolean=} bFocusYesOnShow = true * @returns {void} */ - onShow(askDesc, fYesFunc = null, fNoFunc = null, yesButton = '', noButton = '', isFocusYesOnShow = true) { - this.askDesc(askDesc || ''); + onShow(sAskDesc, fYesFunc = null, fNoFunc = null, yesButton = '', noButton = '', isFocusYesOnShow = true) { + this.askDesc(sAskDesc || ''); this.yesButton(yesButton || i18n('POPUPS_ASK/BUTTON_YES')); this.noButton(noButton || i18n('POPUPS_ASK/BUTTON_NO')); this.fYesAction = fYesFunc; @@ -75,4 +75,17 @@ class AskPopupView extends AbstractViewPopup { } } +AskPopupView.password = function(sAskDesc) { + return new Promise(resolve => { + this.showModal([ + sAskDesc + `

${i18n('GLOBAL/PASSWORD')}:

`, + () => resolve(this.__dom.querySelector('input[type="password"]').value), + () => resolve(null), + '', + i18n('GLOBAL/CANCEL'), + false + ]); + }); +} + export { AskPopupView, AskPopupView as default };