LacunaWebPKI: List and read certificate Example

by Lacuna Software

HTML

<script src="https://cdn.lacunasoftware.com/libs/web-pki/lacuna-web-pki-2.16.1.min.js"></script>
<h2>Web PKI - List and read certificate Example</h2>
<select id="certificateSelect"></select>
<button id="refreshCertificates" type="button">Refresh Certificates</button>
<button id="readCertificate" type="button">Read Certificate</button>
<div id="logPanel"></div>

JavaScript

// ------------------------------------------------------------------------------------------
// LacunaWebPKI: List Certificates Sample

var pki = new LacunaWebPKI('AiEAKi5sYWN1bmFzb2Z0d2FyZS5jb20sanNmaWRkbGUubmV0AAADAFBybwgAANjqpHQf3QgAAWdZXb1mfX0OvXPptndkKubdjvH2Bp2TAY3IW+i5PpTMtBmnQwYw4YMtouSaod41haOgzNYbyveZ6VFpjW1IVAQRlVh1yO66+W2jkNm/ZJLqn3bQ/KgSPnxRN8/6yCykZGw+kGe0mhaahRYXYAakzU8+MaRrTShwmJpr1ko1nPt9o62/9L32Jpaq2TZLQc++C/JzUT0FUX7ycsPD8FfucX/LN4NVh9rFlWN+iPRrZ5jHcYFA1OdG2wFMzBQyF311KNdx3An5xMj1BrHum4ocvNxM7RABh9YClezDlmdZTG7MLe8I/goKoVy/KQ/dN4g06ze+GW5hhU9Hj6CKgUaSriM=');

function start() {
    log('Initializing component ...');
    pki.init({ ready: onWebPkiReady });
}

function onWebPkiReady () {
    log('Component ready, listing certificates ...');
    refreshCertificates();
}

function refreshCertificates() {
    $('#certificateSelect').empty();
    
    pki.listCertificates()
    .success(function (certs) {
        log(certs.length + ' certificates found.');
        certs.forEach(c => insertCertificate(c));
    });
}

function insertCertificate(cert) {
    var text = cert.subjectName + ' (Issuer: ' + cert.issuerName + ')';
    $('#certificateSelect').append($('<option />').val(cert.thumbprint).text(text));
}

function readCertificate() {
    pki.readCertificate({
        thumbprint: $('#certificateSelect').val()
    }).success(function (content) {
        log('Certificate content: ' + content);
    });
}

function log(message) {
    $('#logPanel').append('<p>' + message + '</p>');
    console.log(message);
}

$(function() {
    $('#refreshCertificates').click(refreshCertificates);
    $('#readCertificate').click(readCertificate);
    start();
});