Improved mailvelope handling

This commit is contained in:
the-djmaze 2023-03-07 16:06:09 +01:00
parent 4a9bc53558
commit d484392518
2 changed files with 54 additions and 52 deletions

View file

@ -9,17 +9,15 @@ import { SettingsCapa, SettingsGet } from 'Common/Globals';
import { GnuPGUserStore } from 'Stores/User/GnuPG'; import { GnuPGUserStore } from 'Stores/User/GnuPG';
import { OpenPGPUserStore } from 'Stores/User/OpenPGP'; import { OpenPGPUserStore } from 'Stores/User/OpenPGP';
// https://mailvelope.github.io/mailvelope/Keyring.html
let mailvelopeKeyring = null;
export const export const
BEGIN_PGP_MESSAGE = '-----BEGIN PGP MESSAGE-----', BEGIN_PGP_MESSAGE = '-----BEGIN PGP MESSAGE-----',
// BEGIN_PGP_SIGNATURE = '-----BEGIN PGP SIGNATURE-----', // BEGIN_PGP_SIGNATURE = '-----BEGIN PGP SIGNATURE-----',
// BEGIN_PGP_SIGNED = '-----BEGIN PGP SIGNED MESSAGE-----', // BEGIN_PGP_SIGNED = '-----BEGIN PGP SIGNED MESSAGE-----',
PgpUserStore = new class { PgpUserStore = new class {
constructor() {
// https://mailvelope.github.io/mailvelope/Keyring.html
this.mailvelopeKeyring = null;
}
init() { init() {
if (SettingsCapa('OpenPGP') && window.crypto && crypto.getRandomValues) { if (SettingsCapa('OpenPGP') && window.crypto && crypto.getRandomValues) {
rl.loadScript(SettingsGet('StaticLibsJs').replace('/libs.', '/openpgp.')) rl.loadScript(SettingsGet('StaticLibsJs').replace('/libs.', '/openpgp.'))
@ -38,7 +36,7 @@ export const
identifier = identifier || SettingsGet('Email'); identifier = identifier || SettingsGet('Email');
if (window.mailvelope) { if (window.mailvelope) {
const fn = keyring => { const fn = keyring => {
this.mailvelopeKeyring = keyring; mailvelopeKeyring = keyring;
console.log('mailvelope ready'); console.log('mailvelope ready');
}; };
mailvelope.getKeyring().then(fn, err => { mailvelope.getKeyring().then(fn, err => {
@ -79,8 +77,7 @@ export const
async mailvelopeHasPublicKeyForEmails(recipients) { async mailvelopeHasPublicKeyForEmails(recipients) {
const const
keyring = this.mailvelopeKeyring, mailvelope = mailvelopeKeyring && await mailvelopeKeyring.validKeyForAddress(recipients)
mailvelope = keyring && await keyring.validKeyForAddress(recipients)
/*.then(LookupResult => Object.entries(LookupResult))*/, /*.then(LookupResult => Object.entries(LookupResult))*/,
entries = mailvelope && Object.entries(mailvelope); entries = mailvelope && Object.entries(mailvelope);
return entries && entries.filter(value => value[1]).length === recipients.length; return entries && entries.filter(value => value[1]).length === recipients.length;
@ -104,8 +101,7 @@ export const
} }
async getMailvelopePrivateKeyFor(email/*, sign*/) { async getMailvelopePrivateKeyFor(email/*, sign*/) {
let keyring = this.mailvelopeKeyring; if (mailvelopeKeyring && await mailvelopeKeyring.hasPrivateKey({email:email})) {
if (keyring && await keyring.hasPrivateKey({email:email})) {
return ['mailvelope', email]; return ['mailvelope', email];
} }
return false; return false;
@ -145,46 +141,48 @@ export const
} }
// Try Mailvelope (does not support inline images) // Try Mailvelope (does not support inline images)
try { if (mailvelopeKeyring) {
let emails = [...message.from,...message.to,...message.cc].validUnique(), try {
i = emails.length; let emails = [...message.from,...message.to,...message.cc].validUnique(),
while (i--) { i = emails.length;
if (await this.getMailvelopePrivateKeyFor(emails[i].email)) { 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;
let result = await mailvelope.createDisplayContainer( body.textContent = '';
'#'+body.id, let result = await mailvelope.createDisplayContainer(
armoredText, '#'+body.id,
this.mailvelopeKeyring, armoredText,
{ mailvelopeKeyring,
senderAddress: sender {
// emails[i].email senderAddress: sender
} // emails[i].email
); }
if (result) { );
if (result.error?.message) { if (result) {
if ('PWD_DIALOG_CANCEL' !== result.error.code) { if (result.error?.message) {
alert(result.error.code + ': ' + 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;
} }
break;
} }
} catch (err) {
console.error(err);
} }
} catch (err) {
console.error(err);
} }
// Now try GnuPG // Now try GnuPG
@ -217,19 +215,19 @@ export const
* So far this is only the autocrypt header. * So far this is only the autocrypt header.
*/ */
/* /*
this.mailvelopeKeyring.additionalHeadersForOutgoingEmail(headers) mailvelopeKeyring.additionalHeadersForOutgoingEmail(headers)
this.mailvelopeKeyring.addSyncHandler(syncHandlerObj) mailvelopeKeyring.addSyncHandler(syncHandlerObj)
this.mailvelopeKeyring.createKeyBackupContainer(selector, options) mailvelopeKeyring.createKeyBackupContainer(selector, options)
this.mailvelopeKeyring.createKeyGenContainer(selector, { mailvelopeKeyring.createKeyGenContainer(selector, {
// userIds: [], // userIds: [],
keySize: 4096 keySize: 4096
}) })
this.mailvelopeKeyring.exportOwnPublicKey(emailAddr).then(<AsciiArmored, Error>) mailvelopeKeyring.exportOwnPublicKey(emailAddr).then(<AsciiArmored, Error>)
this.mailvelopeKeyring.importPublicKey(armored) mailvelopeKeyring.importPublicKey(armored)
// https://mailvelope.github.io/mailvelope/global.html#SyncHandlerObject // https://mailvelope.github.io/mailvelope/global.html#SyncHandlerObject
this.mailvelopeKeyring.addSyncHandler({ mailvelopeKeyring.addSyncHandler({
uploadSync uploadSync
downloadSync downloadSync
backup backup

View file

@ -455,6 +455,10 @@
} }
} }
.mailvelope iframe {
min-height: 400px;
}
html:not(.sm-msgView-bottom):not(.sm-msgView-side) { html:not(.sm-msgView-bottom):not(.sm-msgView-side) {
#rl-right:not(.message-selected) #V-MailMessageView { #rl-right:not(.message-selected) #V-MailMessageView {
display: none; display: none;