<script src="https://demo.yubico.com/js/u2f-api.js"></script>
<script>
    {% if debug %}
        if (location.protocol !== 'https:') {
            alert("You are using HTTP. Change the protocol to HTTPS. HTTPS is required by U2F.");
        }
    {% endif %}
    document.addEventListener('DOMContentLoaded', function () {
        if (!(conditionizr.chromium || conditionizr.chrome)) {
            $('.unsupported-modal').modal({});
        }
        Array.from(document.querySelectorAll('.u2f-enroll')).forEach(function (form) {
            form.addEventListener("submit", function (ev) {
                ev.preventDefault();
                var form = ev.target;
                var u2f_enroll_field = form.querySelector('input[name="u2f_enroll"]');
                var request = JSON.parse(u2f_enroll_field.value);
                console.log("Request", request);
                /* u2f.register(<Application id>,
                     [<RegisterRequest instance>, ...],
                     [<RegisteredKey for known token 1>, ...],
                     registerResponseHandler); */
                $('.u2f-authentication-modal').modal({});
                u2f.register(request.appId,
                    request.registerRequests,
                    request.registeredKeys,
                    function (deviceResponse) {
                        var u2f_bind_field = form.querySelector('input[name="u2f_bind"]');
                        if (u2f_bind_field === null) {
                            alert("Not found U2F bind field!");
                        }
                        u2f_bind_field.value = JSON.stringify(deviceResponse);
                        form.submit();
                    }
                );
            }, false);
        });
        Array.from(document.querySelectorAll('.u2f-challenge')).forEach(function (form) {
            form.addEventListener("submit", function (ev) {

                ev.preventDefault();
                var form = ev.target;
                var u2f_challenge_field = form.querySelector('input[name="u2f_challenge"]');
                var request = JSON.parse(u2f_challenge_field.value);
                console.log("Challenge", request);
                /*     void sign (DOMString appId,
                DOMString challenge,
                sequence<RegisteredKey> registeredKeys,
                function(SignResponse or Error) callback,
                optional unsigned long? opt_timeoutSeconds);
                */
                $('.u2f-authentication-modal').modal({});

                u2f.sign(request.appId,
                    request.challenge,
                    request.registeredKeys,
                    function (deviceResponse) {
                        var u2f_verify_field = form.querySelector('input[name="u2f_verify"]');
                        if (u2f_verify_field === null) {
                            alert("Not found U2F verify field!");
                        }
                        console.log(deviceResponse.errorMessage);
                        u2f_verify_field.value = JSON.stringify(deviceResponse);
                        form.submit();
                    }
                );
            }, false);
        });
    });
</script>
