mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-05 15:37:03 +03:00
Improvements for PGP decryption #600
This commit is contained in:
parent
bef1a378ca
commit
2cdadf3a6c
2 changed files with 61 additions and 46 deletions
|
|
@ -132,51 +132,64 @@ export const
|
||||||
}
|
}
|
||||||
|
|
||||||
async decrypt(message) {
|
async decrypt(message) {
|
||||||
const sender = message.from[0].email,
|
const armoredText = message.plain(),
|
||||||
armoredText = message.plain();
|
emails = [...message.from,...message.to,...message.cc].validUnique();
|
||||||
|
|
||||||
if (!this.isEncrypted(armoredText)) {
|
if (!this.isEncrypted(armoredText)) {
|
||||||
return;
|
throw Error('Not armored text');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try OpenPGP.js
|
// Try OpenPGP.js
|
||||||
let result = await OpenPGPUserStore.decrypt(armoredText, sender);
|
let email = emails.find(email => {
|
||||||
if (result) {
|
let result = OpenPGPUserStore.getPrivateKeyFor(email.email);
|
||||||
|
if (result) {
|
||||||
|
console.log('Trying decrypt with '+result.id+' of '+email.email);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
|
});
|
||||||
|
if (email) {
|
||||||
|
let result = await OpenPGPUserStore.decrypt(armoredText, email.email);
|
||||||
|
if (result?.data) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
console.error('OpenPGP decrypt failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try Mailvelope (does not support inline images)
|
// Try Mailvelope (does not support inline images)
|
||||||
try {
|
try {
|
||||||
let key = await this.getMailvelopePrivateKeyFor(message.to[0].email);
|
let i = emails.length;
|
||||||
if (key) {
|
while (i--) {
|
||||||
/**
|
if (await this.getMailvelopePrivateKeyFor(emails[i].email)) {
|
||||||
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createEncryptedFormContainer
|
/**
|
||||||
* Creates an iframe to display an encrypted form
|
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createEncryptedFormContainer
|
||||||
*/
|
* Creates an iframe to display an encrypted form
|
||||||
// mailvelope.createEncryptedFormContainer('#mailvelope-form');
|
*/
|
||||||
/**
|
// mailvelope.createEncryptedFormContainer('#mailvelope-form');
|
||||||
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createDisplayContainer
|
/**
|
||||||
* Creates an iframe to display the decrypted content of the encrypted mail.
|
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createDisplayContainer
|
||||||
*/
|
* Creates an iframe to display the decrypted content of the encrypted mail.
|
||||||
const body = message.body;
|
*/
|
||||||
body.textContent = '';
|
const body = message.body;
|
||||||
result = await mailvelope.createDisplayContainer(
|
body.textContent = '';
|
||||||
'#'+body.id,
|
let result = await mailvelope.createDisplayContainer(
|
||||||
armoredText,
|
'#'+body.id,
|
||||||
this.mailvelopeKeyring,
|
armoredText,
|
||||||
{
|
this.mailvelopeKeyring,
|
||||||
senderAddress: sender
|
{
|
||||||
}
|
senderAddress: message.from[0].email
|
||||||
);
|
// emails[i].email
|
||||||
if (result) {
|
}
|
||||||
if (result.error?.message) {
|
);
|
||||||
if ('PWD_DIALOG_CANCEL' !== result.error.code) {
|
if (result) {
|
||||||
alert(result.error.code + ': ' + result.error.message);
|
if (result.error?.message) {
|
||||||
|
if ('PWD_DIALOG_CANCEL' !== result.error.code) {
|
||||||
|
alert(result.error.code + ': ' + result.error.message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
body.classList.add('mailvelope');
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
body.classList.add('mailvelope');
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
||||||
|
|
@ -529,21 +529,23 @@ export class MailMessageView extends AbstractViewRight {
|
||||||
pgpDecrypt() {
|
pgpDecrypt() {
|
||||||
const oMessage = currentMessage();
|
const oMessage = currentMessage();
|
||||||
PgpUserStore.decrypt(oMessage).then(result => {
|
PgpUserStore.decrypt(oMessage).then(result => {
|
||||||
if (result) {
|
if (result?.data) {
|
||||||
oMessage.pgpDecrypted(true);
|
oMessage.pgpDecrypted(true);
|
||||||
if (result.data) {
|
MimeToMessage(result.data, oMessage);
|
||||||
MimeToMessage(result.data, oMessage);
|
oMessage.html() ? oMessage.viewHtml() : oMessage.viewPlain();
|
||||||
oMessage.html() ? oMessage.viewHtml() : oMessage.viewPlain();
|
if (result.signatures?.length) {
|
||||||
if (result.signatures?.length) {
|
oMessage.pgpSigned(true);
|
||||||
oMessage.pgpSigned(true);
|
oMessage.pgpVerified({
|
||||||
oMessage.pgpVerified({
|
signatures: result.signatures,
|
||||||
signatures: result.signatures,
|
success: !!result.signatures.length
|
||||||
success: !!result.signatures.length
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// TODO: translate
|
||||||
|
alert('Decryption failed, canceled or not possible');
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
.catch(e => console.error(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
pgpVerify(/*self, event*/) {
|
pgpVerify(/*self, event*/) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue