Skip to content

Commit

Permalink
fix: public key being seen as private key + clearer generate button text
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuavanderpoll committed Jul 27, 2024
1 parent 81a473c commit d2c9522
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions public/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ async function generateKeys() {
generateBtn.disabled = true;
generateBtn.innerHTML = 'Generating...';

await new Promise(resolve => setTimeout(resolve, 10));

try {
let privateKey, publicKey, fingerprint, expirationDate;

if (keyType.startsWith('RSA')) {
let rsaBits = parseInt(keyType.split('-')[1], 10);

if (rsaBits === 4096) {
setTimeout(() => alert("Generating RSA-4096 key, this may take a while..."), 10);
alert("Generating RSA-4096 key, this may take a while...");
}

const keyPair = await openpgp.generateKey({
Expand Down Expand Up @@ -72,17 +74,37 @@ async function storeKeys() {
const key = document.getElementById('key').value;

let fingerprint, expirationDate, email, comment;
let privateKey = null;
let publicKey = null;

try {
const publicKeyObj = await openpgp.readKey({ armoredKey: key });
fingerprint = publicKeyObj.getFingerprint();
expirationDate = getExpirationTime(await publicKeyObj.getExpirationTime());

const userIDs = publicKeyObj.getUserIDs();
if (userIDs.length > 0) {
const [userID] = userIDs[0].split('<');
comment = userID.trim();
email = userIDs[0].match(/<([^>]+)>/)[1];
// Try to read the key as a private key
try {
const privateKeyObj = await openpgp.readPrivateKey({ armoredKey: key });
privateKey = key;
publicKey = (await privateKeyObj.toPublic()).armor();
fingerprint = privateKeyObj.getFingerprint();
expirationDate = getExpirationTime(await privateKeyObj.getExpirationTime());

const userIDs = privateKeyObj.getUserIDs();
if (userIDs.length > 0) {
const [userID] = userIDs[0].split('<');
comment = userID.trim();
email = userIDs[0].match(/<([^>]+)>/)[1];
}
} catch (error) {
// If it fails, try to read it as a public key
const publicKeyObj = await openpgp.readKey({ armoredKey: key });
publicKey = key;
fingerprint = publicKeyObj.getFingerprint();
expirationDate = getExpirationTime(await publicKeyObj.getExpirationTime());

const userIDs = publicKeyObj.getUserIDs();
if (userIDs.length > 0) {
const [userID] = userIDs[0].split('<');
comment = userID.trim();
email = userIDs[0].match(/<([^>]+)>/)[1];
}
}

const existingKey = keys.find(k => k.fingerprint === fingerprint);
Expand All @@ -91,7 +113,7 @@ async function storeKeys() {
return;
}

keys.push({ privateKey: key, publicKey: key, email, comment, expirationDate, fingerprint });
keys.push({ privateKey, publicKey, email, comment, expirationDate, fingerprint });
saveKeysToLocalStorage();
refreshKeyList();

Expand All @@ -108,7 +130,10 @@ function refreshKeyList() {
keysList.innerHTML = '';

keys.forEach((key, index) => {
const copyPrivateKey = key.privateKey ? `<button class="copyPrivateKeyButton bg-red-500 text-white px-2 py-1 rounded" data-index="${index}">Copy Private</button>` : '';
let copyPrivateKey = '';
if (key.privateKey) {
copyPrivateKey = `<button class="copyPrivateKeyButton bg-red-500 text-white px-2 py-1 rounded" data-index="${index}">Copy Private</button>`;
}

const row = `<tr>
<td class="border px-4 py-2">${formatFingerprint(key.fingerprint)}</td>
Expand Down

0 comments on commit d2c9522

Please sign in to comment.