LacunaWebPKI.listCertificates example (AngularJS)

by Lacuna Software

HTML

<script src="https://webpki.lacunasoftware.com/Scripts/LacunaWebPKI/lacuna-web-pki-1.3.3.js"></script>
<div ng-app>
    <div ng-controller="ExampleController">
        <form>
            <button type="button" ng-click="start()">Start</button><br/>
            <select ng-model="selectedCertificate" ng-options="getCertificateDisplayName(cert) for cert in certificates">
                <option value="">Choose a certificate ...</option>
            </select>
            <button type="button" ng-click="getSelectedCert()">Get Selected Cert</button>
        </form>
        <p ng-repeat="log in logs">{{log.message}}</p>
    </div>
</div>

JavaScript

// ------------------------------------------------------------------------------------------
// LacunaWebPKI.listCertificates example (AngularJS)

function ExampleController($scope) {
    
    var onWebPkiDetected = function () {
        log('Component detected, listing certificates ...');
        pki.listCertificates().success(function (certs) {
            log('Certificates listed!');
            // We will assign to the $scope.certificates variable the actual array yielded
            // by listCertificates and the view will call the function below to get the
            // visual representation for each item.
            $scope.certificates = certs;
        });
    };
    
    $scope.getCertificateDisplayName = function (cert) {
        // The view calls this function to get the visual representation of each
        // certificate. Here, the cert variable is one of the items in the actual array
        // yielded by listCertificates, so it has the properties subjectName, issuerName
        // thumbprint.
        return cert.subjectName + ' (issued by ' + cert.issuerName + ')';
    };
    
    $scope.getSelectedCert = function () {
        if ($scope.selectedCertificate == null) {
            alert('Select a certificate');
        }
        // The $scope.selectedCertificate contains the actual item in the array yielded
        // by listCertificates. So, all we have to do is read its thumbprint property.
        var selectedCertThumb = $scope.selectedCertificate.thumbprint;
        alert("The selected certificate's thumbprint is " + selectedCertThumb);
    };
    
    // ------------------------------------------------------------------------------------------
    // The code from this point on is necessary for the example to run, but is not the focus of
    // this specific example

    $scope.logs = [];
    $scope.certificates = [];
    $scope.selectedCertificate = null;
    
    var myLicense = {
        "format": 1,
        "allowedDomains": [
           ...