PIN unblocking sampleΒΆ

This sample demonstrates how to unblock a PIN on the smart card, using the SCWS API.

<!doctype html>
<html>

    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Titre de la page</title>
        <script src="libs/promise-polyfill.js"></script>
        <script src="../src/scwsapi.js"></script>
        <script>

window.webappcert = "4UpBWnegUBAvq^XEy?Wx*D2NhkB7x6dA4AIQx>7Q$Aa9*ezeS)1egU^Ww]@ckyH}xmwQ5ePgb7Esf!$KtegUjDx>qg7nN%>oenfcEA5]T9f!+jnh7*Qre?]dqiX-xCg+rrZBz%n]fekdkgbQWsf!Lgsh99DEgC*T@BAzE.fe24nh.D^pfFk7vf!$KwenfcEA5]T9f/.0wf/z0zf/yZrf!>QFf!$Kw.!(V$+<#r}}9:O$/0QjO^8yyJbhkXX(B/&-^t7*%2^A#v/l:0E3#I@b^6Wt[N]8$%)VZ%2UIeHQRQ-Y^?rAX.s$Y0:Dp5-PKK1Vb.Fq&fp^lS@0TS0sd(Cm?7PAbn)8T5Ffb/s!twJklugBFI<7ZR6Ao#59@R=V:}J^S!Emm7J>e%/Q(tGRG::Hb5YW.&8y0GoGQdJN3&qd0V-&)-WQ]olzy+p!1jdScZW%MO]hVV&&!yNS5OpcJMotfZRiOANs!KNSaM/+%O/Z4+!<}Pz(W6vGn.4[eZBk{IY8@<)zMk6BaB+<[D6%f6ek&+Juow]5P[$Nxtq$nWD-I!S1[5mFZaMEYnQ>K)EE]Zs!o%9Jgb]1a:xf]qL@V}1(7FZwm|MIIE5DCCAsygAwIBAgIBADANBgkqhkiG9w0BAQsFADA9MQswCQYDVQQGEwJGUjEdMBsGA1UEAwwUSWRvcHRlIFdlYkFwcENlcnQgQ0ExDzANBgNVBAoMBklkb3B0ZTAeFw0yMjA5MjExNDExMzNaFw0yMjEyMzEyMjU5MDBaMDExEjAQBgNVBAMTCU1heGltZURldjEPMA0GA1UEChMGSWRvcHRlMQowCAYDVQQLEwEwMIIBIjANBgkqhki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UDaw3WX6mjjCvLvVmdRP4XWOFOexcvMc7Ljsm/g/BeSN+hI9SZXPdCfTHVdsQq2JWOamRsPxl+MtN9CAl47UZHOGN7G2CEupdaaXgovMMACxZdO8ZXfa9uRbAYEoW3uR794rqZkjvkUzdzjimkiucrrV4YJpsPpioDQUv9NKGRP4y9IqP6Sr04GMyo/GWLxfyBJN8fDliQSqPHnQq+VuixqX4SYNAKfkdESmNXczaID2AwPQboX1CJcoD+l5dHFDa2hUeVmTe/emTKspX8yPVQNVkL7MqkbLgEckOfffy/rIN6uB8gQsh0rthkqQ1a9nG6dlNKl37tQTLsmzL1T1eaOJ60dAYBUIX4AsXk=";

function log(msg) {
    window.infodiv.insertAdjacentHTML('beforeend', msg + '<br>');
    window.infodiv.scrollTop += 1000;
}

window.token = null;

window.onload = function() {
    window.infodiv = document.getElementById("infodiv");
    window.certlist = document.getElementById("certificatelist");
    /* connecting to SCWS */
    log("Connecting to SCWS...");
    SCWS.findService(window.webappcert).then(function(challenge) {
        log("Connection to SCWS succeeded");
        return new Promise(function(resolve, reject) {
            var req = new XMLHttpRequest();
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    if (this.status === 200)
                        resolve(req.responseText);
                    else
                        reject(new Error("Challenge signature failed"));
                }
            };
            req.open("GET", "signchallenge.php?rnd=" + challenge, true);
            req.send();
        }).then(function(signature) {
            return SCWS.createEnvironment(signature);
        });
    }).then(function() {
        log("SCWS environment created successfully");
        /* check all readers already connected */
        var readers = SCWS.readers;
        if (readers.length == 0) {
            log("No readers found");
        }
        else {
            for (var i = 0; i < readers.length; i++)
                checkReader(readers[i]);
        }
        /* register event callbacks for checking readers changes */
        SCWS.onreaderadded = function(reader) {
            log("Reader inserted: '" + reader.name + "'");
            checkReader(reader);
        }
        SCWS.onreaderremoved = function(reader) {
            log("Reader removed: '" + reader.name + "'");
            checkReader(reader);
        }
        SCWS.onreaderstatechanged = function(reader) {
            log("Reader state changed: '" + reader.name + "'");
            checkReader(reader);
        }
    }).catch(function(err) {
        log("ERROR: " + err.message);
    });
}

/**
 * Connects to the reader.
 */
function checkReader(reader) {
    if (reader.status == "ok" && reader.cardPresent) {
        /* disconnecting previous card */
        if (window.token) {
            if (window.token.reader == reader)
                return;
            window.token.disconnect();
            window.token = null;
        }
        /* connecting to reader */
        log("Connecting to reader '" + reader.name + "'...");
        reader.connect().then(function(token) {
            log("Connected. Operation will be made on token '" + token.label + "'...");
            for (var i = 0; i < token.pins.length; i++) {
                log("&nbsp;&nbsp;Pin " + i + ": " + (token.pins[i].label || "Default PIN"));
            }
            window.token = token;
        }).catch(function(err) {
            log("ERROR: " + err.message);
        });
    }
    else {
        /* disconnecting */
        if (window.token && window.token.reader == reader) {
            window.token.disconnect();
            window.token = null;
        }
    }
}

function proceed()
{
    if (!window.token)
        return;
    var pinNum = parseInt(document.getElementById("pinnum").value);
    var pin = window.token.pins[pinNum];
    if (!pin)
        return;
    var pukVal = document.getElementById("puk").value;
    var pinVal = document.getElementById("pin1").value;
    if (pinVal != document.getElementById("pin2").value) {
        alert("New PIN and confirmation value inconsistent");
        return;
    };
    log("Login with PUK...");
    pin.login(pukVal, true).then(function() {
        log("Login successful, reinitializing PIN...");
        return pin.init(pinVal);
    }).then(function() {
        log ("PIN unblocked successfully");
    }).catch(function(err) {
        log("ERROR: " + err.message);
    });
}

        </script>
    </head>

    <body>
        
        <p>
            PIN number: <input id="pinnum" type="text" value="0"/><br>
            PUK: <input id="puk" type="password" /><br>
            New PIN: <input id="pin1" type="password" /><br>
            Confirm new PIN: <input id="pin2" type="password" /><br>
            <input type="button" onclick="proceed()" value="Unblock"/>
        </p>
        
        <div id="infodiv" style="display:inline-block;vertical-align:top;width:100%;height:10rem;overflow:auto;border:1px solid gray;"></div>

    </body>
</html>