Replaced prompt() into a AskPopupView.password() Promise

This commit is contained in:
the-djmaze 2022-02-07 17:30:41 +01:00
parent 1f0af5c0ac
commit d9f50fa829
3 changed files with 38 additions and 8 deletions

View file

@ -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<br>' + 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';
}
};

View file

@ -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<br>' + 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';
}
};

View file

@ -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 + `<p>${i18n('GLOBAL/PASSWORD')}: <input type="password" autofocus=""></p>`,
() => resolve(this.__dom.querySelector('input[type="password"]').value),
() => resolve(null),
'',
i18n('GLOBAL/CANCEL'),
false
]);
});
}
export { AskPopupView, AskPopupView as default };