Skip to content

Commit

Permalink
feat: Add checkbox for showing certificates without private key
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed May 30, 2024
1 parent d807662 commit 4e8998e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
21 changes: 18 additions & 3 deletions example6.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ <h2>Certificate chain building</h2>
<label for="certificates">Certificates:</label>
<select id="certificates"></select>
</div>
<div class="group">
<label for="withoutKey">Show certificates without private key</label>
<input type="checkbox" id="withoutKey">
</div>
<div class="group">
<button id="build">Build</button>
</div>
Expand Down Expand Up @@ -113,18 +117,29 @@ <h2>Certificate chain building</h2>
const providerID = $("providers").value;
if (providerID) {
const crypto = await ws.getCrypto(providerID);
fillCertificateSelect(crypto, $("certificates"))
fillCertificateSelect(crypto, $certificates, !$withoutKey.checked);
}
}

$("providers").onchange = async () => {
const providerID = $("providers").value;
const provider = await ws.getCrypto(providerID)
fillCertificateSelect(provider, $("certificates"))
const provider = await ws.getCrypto(providerID);
fillCertificateSelect(provider, $certificates, !$withoutKey.checked);
}

const $providers = $("providers");
const $build = $("build");
const $chain = $("chain");
const $certificates = $("certificates");
const $withoutKey = $("withoutKey");

$withoutKey.onchange = () => {
(async () => {
const providerID = $providers.value;
const provider = await ws.getCrypto(providerID)
fillCertificateSelect(provider, $certificates, !$withoutKey.checked);
})();
}

$build.onclick = async () => {
$build.disabled = true;
Expand Down
41 changes: 24 additions & 17 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function FillProviderSelect(domSelect) {
* @param {*} crypto
* @param {*} domSelect
*/
async function fillCertificateSelect(provider, domSelect) {
async function fillCertificateSelect(provider, domSelect, hasKey = true) {
if (! await provider.isLoggedIn()) {
await provider.login();
}
Expand All @@ -80,19 +80,19 @@ async function fillCertificateSelect(provider, domSelect) {

const certs = [];
for (const certID of certIDs) {
for (const keyID of keyIDs) {
if (keyID.split("-")[2] === certID.split("-")[2]) {
try {
const cert = await provider.certStorage.getItem(certID);

certs.push({
id: certID,
item: cert,
});
} catch (e) {
console.error(`Cannot get certificate ${certID} from CertificateStorage. ${e.message}`);
}
}
const [, , certId] = certID.split("-");
if (hasKey && !keyIDs.some((keyID) => keyID.split("-")[2] === certId)) {
continue;
}
try {
const cert = await provider.certStorage.getItem(certID);

certs.push({
id: certID,
item: cert,
});
} catch (e) {
console.error(`Cannot get certificate ${certID} from CertificateStorage. ${e.message}`);
}
}

Expand Down Expand Up @@ -171,20 +171,27 @@ function GetCommonName(name) {
return res ? res[1] : "Unknown";
}

async function GetCertificateKey(type, provider, certID) {
async function GetCertificateKey(type, provider, certID, algorithm, extractable, usages) {
const keyIDs = await provider.keyStorage.keys()
for (const keyID of keyIDs) {
const parts = keyID.split("-");

if (parts[0] === type && parts[2] === certID.split("-")[2]) {
const key = await provider.keyStorage.getItem(keyID);
let key;
if (algorithm && extractable !== undefined && usages) {
key = await provider.keyStorage.getItem(keyID, algorithm, extractable, usages);
} else {
key = await provider.keyStorage.getItem(keyID);
}
if (key) {
return key;
}
}
}
if (type === "public") {
const cert = await provider.certStorage.getItem(certID);
const cert = (algorithm && extractable !== undefined && usages)
? await provider.certStorage.getItem(certID, algorithm, extractable, usages)
: await provider.certStorage.getItem(certID);
if (cert) {
return cert.publicKey;
}
Expand Down

0 comments on commit 4e8998e

Please sign in to comment.