Improve documentation

This commit is contained in:
Yarmo Mackenbach 2020-06-26 13:06:32 +02:00
parent 1880554ae8
commit 51110408b1

View file

@ -1,21 +1,21 @@
async function verifySignature(opts) { async function verifySignature(opts) {
// Init
const elRes = document.body.querySelector("#result"); const elRes = document.body.querySelector("#result");
const elResContent = document.body.querySelector("#resultContent"); const elResContent = document.body.querySelector("#resultContent");
let keyData, feedback, signature, verified; let keyData, feedback, signature, verified, valid;
// Reset feedback
elRes.innerHTML = ""; elRes.innerHTML = "";
elResContent.innerHTML = ""; elResContent.innerHTML = "";
try { try {
// Get key data
keyData = await fetchKeys(opts); keyData = await fetchKeys(opts);
if (opts.signature == null) { // Handle missing signature
elRes.innerHTML = "No signature was provided."; if (opts.signature == null) { throw("No signature was provided."); }
elRes.classList.remove('green');
elRes.classList.add('red');
return;
}
// Try two different methods of signature reading
let readError = null; let readError = null;
try { try {
signature = await openpgp.message.readArmored(opts.signature); signature = await openpgp.message.readArmored(opts.signature);
@ -27,12 +27,14 @@ async function verifySignature(opts) {
} catch(e) { } catch(e) {
readError = e; readError = e;
} }
if (signature == null) {throw(readError)}; if (signature == null) { throw(readError) };
// Verify the signature
verified = await openpgp.verify({ verified = await openpgp.verify({
message: signature, message: signature,
publicKeys: keyData.publicKey publicKeys: keyData.publicKey
}); });
{ valid } = verified.signatures[0];
} catch (e) { } catch (e) {
console.error(e); console.error(e);
elRes.innerHTML = e; elRes.innerHTML = e;
@ -41,13 +43,15 @@ async function verifySignature(opts) {
return; return;
} }
// Init feedback to empty string
feedback = ''; feedback = '';
const { valid } = verified.signatures[0];
// If content was extracted from signature
if (keyData.sigContent) { if (keyData.sigContent) {
elResContent.innerHTML = "<strong>Signature content:</strong><br><span style=\"white-space: pre-line\">"+sigContent+"</span>"; elResContent.innerHTML = "<strong>Signature content:</strong><br><span style=\"white-space: pre-line\">"+sigContent+"</span>";
} }
// Provide different feedback depending on key input mode
if (opts.mode == "signature" && keyData.sigUserId) { if (opts.mode == "signature" && keyData.sigUserId) {
if (valid) { if (valid) {
feedback += "The message was signed by the userId extracted from the signature.<br>"; feedback += "The message was signed by the userId extracted from the signature.<br>";
@ -88,27 +92,30 @@ async function verifySignature(opts) {
} }
} }
// Display feedback
elRes.innerHTML = feedback; elRes.innerHTML = feedback;
}; };
async function encryptMessage(opts) { async function encryptMessage(opts) {
// Init
const elEnc = document.body.querySelector("#messageEncrypted"); const elEnc = document.body.querySelector("#messageEncrypted");
const elRes = document.body.querySelector("#result"); const elRes = document.body.querySelector("#result");
let keyData, feedback, message, encrypted; let keyData, feedback, message, encrypted;
// Reset feedback
elRes.innerHTML = ""; elRes.innerHTML = "";
elEnc.value = ""; elEnc.value = "";
try { try {
// Get key data
keyData = await fetchKeys(opts); keyData = await fetchKeys(opts);
// Handle missing message
if (opts.message == null) { if (opts.message == null) {
elRes.innerHTML = "No message was provided."; throw("No message was provided.");
elRes.classList.remove('green');
elRes.classList.add('red');
return;
} }
// Encrypt the message
encrypted = await openpgp.encrypt({ encrypted = await openpgp.encrypt({
message: openpgp.message.fromText(opts.message), message: openpgp.message.fromText(opts.message),
publicKeys: keyData.publicKey publicKeys: keyData.publicKey
@ -121,6 +128,7 @@ async function encryptMessage(opts) {
return; return;
} }
// Display encrypted data
elEnc.value = encrypted.data; elEnc.value = encrypted.data;
}; };