Add OpenPGP mail encryption: decrypt, verify, encrypt, sign and contact keyring, v0.3.0
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -5,8 +5,15 @@ function openEncryptionKeys() {
|
||||
const root = $('#modal-root');
|
||||
const account = connectedMailAccount || {};
|
||||
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal attachments-modal"><div class="modal-head"><span>Encryption keys</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields">
|
||||
<p class="account-note">OpenPGP keys are protected by a passphrase and stored encrypted by your operating system. Back up your private key and revocation certificate: without them, encrypted mail cannot be recovered. Sending and reading encrypted mail is not available yet.</p>
|
||||
<p class="account-note">OpenPGP keys are protected by a passphrase and stored encrypted by your operating system. Back up your private key and revocation certificate: without them, encrypted mail cannot be recovered. Contact keys below are used to encrypt mail to your correspondents and to check signatures.</p>
|
||||
<div id="pgp-key-list" class="attachment-list">Loading keys…</div>
|
||||
<h3 class="pgp-heading">Contact public keys</h3>
|
||||
<div id="pgp-contact-list" class="attachment-list">Loading contacts…</div>
|
||||
<div class="pgp-actions"><button class="discard" id="pgp-add-contact">Import public key…</button></div>
|
||||
<h3 class="pgp-heading">Passphrase memory</h3>
|
||||
<label class="compose-line"><span>Minutes</span><input id="pgp-cache-minutes" type="number" min="0" max="1440" step="1"></label>
|
||||
<div class="pgp-actions"><button class="discard" id="pgp-save-cache">Save</button><button class="discard" id="pgp-forget">Forget passphrases now</button></div>
|
||||
<p class="account-note">Unlocked passphrases are kept in memory only, for this many minutes (0 asks every time). Opening encrypted attachments needs the passphrase to be remembered.</p>
|
||||
<h3 class="pgp-heading">Create a key</h3>
|
||||
<label class="compose-line"><span>Name</span><input id="pgp-name" maxlength="100" value="${esc(account.displayName || '')}"></label>
|
||||
<label class="compose-line"><span>Email</span><input id="pgp-email" type="email" value="${esc(account.email || '')}"></label>
|
||||
@@ -94,6 +101,78 @@ function openEncryptionKeys() {
|
||||
await render();
|
||||
} catch (error) { status(error.message || 'Could not import the key.'); }
|
||||
};
|
||||
async function renderContacts() {
|
||||
const list = $('#pgp-contact-list');
|
||||
let contacts = [];
|
||||
try { contacts = await window.daybreakMail.pgp.contacts(); } catch (error) { list.textContent = error.message || 'Could not load contacts.'; return; }
|
||||
list.replaceChildren();
|
||||
if (!contacts.length) { list.textContent = 'No contact keys yet. Import a public key, or add one from a received message.'; return; }
|
||||
contacts.forEach(contact => {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'attachment-row';
|
||||
const info = document.createElement('div');
|
||||
info.className = 'attachment-row-info';
|
||||
const title = document.createElement('strong');
|
||||
title.textContent = contact.emails.join(', ') || contact.name || 'Public key';
|
||||
const meta = document.createElement('small');
|
||||
meta.textContent = `${contact.name ? contact.name + ' · ' : ''}added ${new Date(contact.addedAt).toLocaleDateString()} · from ${contact.source}`;
|
||||
const fingerprint = document.createElement('small');
|
||||
fingerprint.textContent = `Fingerprint: ${contact.fingerprint.match(/.{1,4}/g).join(' ')}`;
|
||||
info.append(title, meta, fingerprint);
|
||||
const trust = document.createElement('select');
|
||||
trust.className = 'pgp-trust';
|
||||
trust.setAttribute('aria-label', 'Key trust');
|
||||
[['unknown', 'Not verified'], ['trusted', 'Trusted'], ['verified', 'Verified (fingerprint checked)']].forEach(([value, label]) => {
|
||||
const option = document.createElement('option');
|
||||
option.value = value; option.textContent = label; option.selected = contact.trust === value;
|
||||
trust.append(option);
|
||||
});
|
||||
trust.onchange = async () => {
|
||||
if (trust.value === 'verified' && !confirm(`Mark this key as verified only if you compared this fingerprint with its owner through another channel:\n\n${contact.fingerprint.match(/.{1,4}/g).join(' ')}`)) {
|
||||
trust.value = contact.trust;
|
||||
return;
|
||||
}
|
||||
try { await window.daybreakMail.pgp.setTrust(contact.fingerprint, trust.value); contact.trust = trust.value; status('Trust updated.'); }
|
||||
catch (error) { trust.value = contact.trust; status(error.message || 'Could not update trust.'); }
|
||||
};
|
||||
const actions = [
|
||||
['Export', () => window.daybreakMail.pgp.exportContact(contact.fingerprint)],
|
||||
['Remove', async () => {
|
||||
if (!confirm('Remove this contact key? You will no longer be able to encrypt to it.')) return { canceled: true };
|
||||
await window.daybreakMail.pgp.removeContact(contact.fingerprint);
|
||||
await renderContacts();
|
||||
return { deleted: true };
|
||||
}]
|
||||
];
|
||||
row.append(info, trust);
|
||||
actions.forEach(([label, run]) => {
|
||||
const button = document.createElement('button');
|
||||
button.className = 'discard';
|
||||
button.textContent = label;
|
||||
button.onclick = async () => {
|
||||
try { const result = await run(); status(result?.saved ? 'File saved.' : result?.deleted ? 'Contact key removed.' : ''); }
|
||||
catch (error) { status(error.message || 'That action failed.'); }
|
||||
};
|
||||
row.append(button);
|
||||
});
|
||||
list.append(row);
|
||||
});
|
||||
}
|
||||
|
||||
$('#pgp-add-contact').onclick = async () => {
|
||||
try {
|
||||
const result = await window.daybreakMail.pgp.addContact({});
|
||||
if (!result.canceled) status(result.keyChangedFrom?.length ? 'Key added. Warning: you already had a different key for this address. Verify the new fingerprint.' : 'Public key added. Verify its fingerprint with the owner, then mark it as verified.');
|
||||
await renderContacts();
|
||||
} catch (error) { status(error.message || 'Could not import the key.'); }
|
||||
};
|
||||
window.daybreakMail.pgp.getSettings().then(settings => { $('#pgp-cache-minutes').value = settings.cacheMinutes; }).catch(() => {});
|
||||
$('#pgp-save-cache').onclick = async () => {
|
||||
try { const settings = await window.daybreakMail.pgp.setSettings({ cacheMinutes: Number($('#pgp-cache-minutes').value) }); status(`Passphrases are remembered for ${settings.cacheMinutes} minute(s).`); }
|
||||
catch (error) { status(error.message || 'Could not save the setting.'); }
|
||||
};
|
||||
$('#pgp-forget').onclick = async () => { await window.daybreakMail.pgp.forgetPassphrases(); status('Remembered passphrases were cleared.'); };
|
||||
void renderContacts();
|
||||
void render();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user