Add OpenPGP key management foundation (encryption phase 1) and encryption plan, v0.2.4
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
// Encryption key management (desktop). Hidden where the platform has no key store yet.
|
||||
function pgpAvailable() { return Boolean(window.daybreakMail?.pgp); }
|
||||
|
||||
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>
|
||||
<div id="pgp-key-list" class="attachment-list">Loading keys…</div>
|
||||
<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>
|
||||
<label class="compose-line"><span>Passphrase</span><input id="pgp-passphrase" type="password" autocomplete="new-password" placeholder="At least 8 characters"></label>
|
||||
<div class="pgp-actions"><button class="send-button" id="pgp-generate">Create key</button><button class="discard" id="pgp-import">Import private key…</button></div>
|
||||
<p class="account-note" id="pgp-status" role="status"></p>
|
||||
</div></div></div>`;
|
||||
const close = () => { root.innerHTML = ''; };
|
||||
root.querySelector('.modal-close').onclick = close;
|
||||
root.querySelector('.modal-overlay').onclick = event => { if (event.target.classList.contains('modal-overlay')) close(); };
|
||||
const status = message => { $('#pgp-status').textContent = message; };
|
||||
const passphraseField = () => $('#pgp-passphrase');
|
||||
const needPassphrase = () => {
|
||||
const value = passphraseField().value;
|
||||
if (!value) { status('Enter the key passphrase in the Passphrase field first.'); passphraseField().focus(); }
|
||||
return value;
|
||||
};
|
||||
|
||||
async function render() {
|
||||
const list = $('#pgp-key-list');
|
||||
let keys = [];
|
||||
try { keys = await window.daybreakMail.pgp.list(); } catch (error) { list.textContent = error.message || 'Could not load keys.'; return; }
|
||||
list.replaceChildren();
|
||||
if (!keys.length) { list.textContent = 'No keys yet.'; return; }
|
||||
keys.forEach(key => {
|
||||
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 = key.email || 'OpenPGP key';
|
||||
const meta = document.createElement('small');
|
||||
meta.textContent = `${key.name ? key.name + ' · ' : ''}${key.algorithm} · ${new Date(key.createdAt).toLocaleDateString()}`;
|
||||
const fingerprint = document.createElement('small');
|
||||
fingerprint.textContent = `Fingerprint: ${key.fingerprint.match(/.{1,4}/g).join(' ')}`;
|
||||
info.append(title, meta, fingerprint);
|
||||
row.append(info);
|
||||
const actions = [
|
||||
['Export public', () => window.daybreakMail.pgp.exportPublic(key.fingerprint), false],
|
||||
['Export private', () => window.daybreakMail.pgp.exportPrivate(key.fingerprint, needPassphrase()), true]
|
||||
];
|
||||
if (key.hasRevocationCertificate) actions.push(['Revocation cert.', () => window.daybreakMail.pgp.exportRevocation(key.fingerprint, needPassphrase()), true]);
|
||||
actions.push(['Delete', async () => {
|
||||
if (!confirm('Delete this key? Encrypted mail that needs it can no longer be read unless you have a backup.')) return { canceled: true };
|
||||
await window.daybreakMail.pgp.remove(key.fingerprint, needPassphrase());
|
||||
await render();
|
||||
return { deleted: true };
|
||||
}, true]);
|
||||
actions.forEach(([label, run, secret]) => {
|
||||
const button = document.createElement('button');
|
||||
button.className = 'discard';
|
||||
button.textContent = label;
|
||||
button.onclick = async () => {
|
||||
if (secret && !passphraseField().value) { needPassphrase(); return; }
|
||||
try {
|
||||
const result = await run();
|
||||
status(result?.saved ? 'File saved.' : result?.deleted ? 'Key deleted.' : '');
|
||||
} catch (error) { status(error.message || 'That action failed.'); }
|
||||
};
|
||||
row.append(button);
|
||||
});
|
||||
list.append(row);
|
||||
});
|
||||
}
|
||||
|
||||
$('#pgp-generate').onclick = async () => {
|
||||
const button = $('#pgp-generate');
|
||||
button.disabled = true;
|
||||
status('Creating key…');
|
||||
try {
|
||||
await window.daybreakMail.pgp.generate({
|
||||
name: $('#pgp-name').value, email: $('#pgp-email').value, passphrase: passphraseField().value, accountId: account.id || null
|
||||
});
|
||||
status('Key created. Export your private key and revocation certificate now and keep them safe.');
|
||||
await render();
|
||||
} catch (error) { status(error.message || 'Could not create the key.'); }
|
||||
button.disabled = false;
|
||||
};
|
||||
$('#pgp-import').onclick = async () => {
|
||||
const passphrase = needPassphrase();
|
||||
if (!passphrase) return;
|
||||
try {
|
||||
const result = await window.daybreakMail.pgp.importKey({ passphrase, accountId: account.id || null });
|
||||
status(result.canceled ? '' : 'Key imported.');
|
||||
await render();
|
||||
} catch (error) { status(error.message || 'Could not import the key.'); }
|
||||
};
|
||||
void render();
|
||||
}
|
||||
|
||||
if (typeof openPreferences === 'function') {
|
||||
const baseOpenPreferences = openPreferences;
|
||||
openPreferences = function () {
|
||||
baseOpenPreferences();
|
||||
const fields = $('#modal-root .compose-fields');
|
||||
if (!fields || !pgpAvailable()) return;
|
||||
const button = document.createElement('button');
|
||||
button.type = 'button';
|
||||
button.className = 'discard';
|
||||
button.id = 'open-encryption-keys';
|
||||
button.textContent = 'Encryption keys…';
|
||||
button.onclick = openEncryptionKeys;
|
||||
fields.append(button);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user