LacunaWebPKI: complete example (with jQuery and extra UI components)

by Lacuna Software

HTML

<script src="https://rawgit.com/malsup/blockui/316f6e5d76a33266970778e80507149d9ef6a02d/jquery.blockUI.js"></script>
<script src="https://get.webpkiplugin.com/Scripts/LacunaWebPKI/lacuna-web-pki-2.5.0.js"></script>
<div id="signatureControlsPanel">
    <select id="certificateSelect"></select>
    <button id="refreshButton" type="button">Refresh</button><br/><br/>
    <button id="signFileButton" type="button">Sign Dummy File</button>
</div>
<div id="dialog">
    <p id="dialogMessage"></p>
</div>
<div id="blockPanel">
</div>

CSS

#signatureControlsPanel {
    margin-top: 1em;
    display: none;
}
#blockPanel {
    display: none;
    padding: 0.5em;
}

JavaScript

// ------------------------------------------------------------------------------------------
// LacunaWebPKI: skip certificate selection if only 1 suitable certificate is found

// CPF of the "suitable" certificate
var userInfo = {
	cpf: '32649517115'
};
var certThumb = null;
var signatureProcessId = null;

var pki = new LacunaWebPKI({
	"format": 1,
	"allowedDomains": [
		"jsfiddle.net",
		"webpki.lacunasoftware.com"
	],
	"expiration": null,
	"signature": "ClKvO1J22vAD+YmfANiKQLbcLE1lNraPKCel6tRM+ZxR+h6M/crtJYRRVGGz7hrdbM0Y0mfTu15RMYGqQMi1QNZS6GrT4vNzIayv552Fl0EFWQA7jWlctUwfYoHRHVEnCNx9YGXDiA9+yDoGlVwgTR7fjzNeS3Fen1MVIyKBF464gN0JvdiCRJMI47JGVDkPmKjcrYIvJs6y5Lg25RW4ZnBKVruS+HR2s3k8ZrV4y4RCQE4UYMKbukF9vsF+JqAEifRlPq2xLcrNdxBveVDSXS/LRHAcrZrMM+Iw4A79jl0ngWPcy+CwinAhT+3dxVo5ZWMRQFpmTkylEMDvTjV9wQ=="
});

function start() {
	log('Checking component installation ...');
	blockUI.start('Initializing Web PKI ...');
	pki.init({
		ready: onWebPkiReady,
		notInstalled: onWebPkiNotInstalled,
		defaultError: onWebPkiError
	});
}

function onWebPkiError(message, error, origin) {
	blockUI.stop();
	log('Web PKI error originated at ' + origin + ': ' + error);
	showModal('Error', 'Web PKI error: ' + message);
}

function onWebPkiNotInstalled (status, message) {
	log('Installation NOT ok: ' + message);
	blockUI.stop();
	showModal('Message', message + '<br/><br/>Click OK to go to installation page', function() {
		pki.redirectToInstallPage();
	});
}

function onWebPkiReady () {
	log('Component ready.');
	blockUI.message('Loading certificates ...');
	loadCertificates();
}

function refresh() {
	blockUI.start('Loading certificates ...');
	loadCertificates();
}

function loadCertificates() {
	log('Listing certificates ...');
	pki.listCertificates().success(function (certificates) {
		log('Certificates listed.');
    var suitableCerts = [];
    var now = new Date();
    for (var i = 0; i < certificates.length; i++) {
    	var c = certificates[i];
      console.log(c.pkiBrazil.cpf);
    ...