<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$

    File:           prs_perslid_otp_new.asp
    Description:    Genereert en toont de persoonlijke QR voor de Google Authenticator
    Parameters:
    Context:
    Note:           De nieuwe code wordt pas definitief als de gebruiker hem
                    heeft gescanned en bevestigd via prs_perslid_otp_new_save.asp
                    (Alleen) met FACFAC mag je dat ook bij iemand anders doen
*/ %>
<%
var NO_OTP_OK = true;
%>
<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/iface.inc"-->
<!--#include file="../aut/login.inc"-->
<!--#include file="../Shared/encoding.inc" -->
<%
var theUser_key = getQParamInt("prs_key", user_key);
if (theUser_key != user_key && !user.has("WEB_FACFAC"))
  abort_with_warning(L("lcl_no_auth"));

var user_info = new Perslid(theUser_key);

var raw = shared.random(20);
var newsecret = '';
for (var i = 0; i < raw.length; i++)
{
    var chr = raw.charCodeAt(i);
    if (chr < 16)
        newsecret += "0";
    newsecret += chr.toString(16);
}
Session("otp_secret_temp") = '1$30$6$0$' + newsecret;
//__Log("newsecret: " + newsecret);
%>
<html>
  <head>
<%  FCLTHeader.Generate();
 %>
    <script type='text/javascript'>
        function otp_accept()
        {
            var otp_code = $("#otpcode").val();
            if (otp_code)
            {
                var data = { otp_code: otp_code,
                             prs_key: <%=theUser_key%> };
                <% protectRequest.dataToken("data"); %>
                $.post("prs_perslid_otp_new_save.asp", data, FcltCallbackAndThenAlways(otp_accept_callback), "json");
            }
        }

        function otp_accept_callback(data)
        {
            if (data.success) {
                FcltMgr.closeDetail(window, data);
                if (FcltMgr === FcltMgr.topmanager()) {
                    window.top.location.href = "<%=safe.jsstring(rooturl + "/")%>";
                }
            }
        }

        function otp_cancel() {
            FcltMgr.confirm(L("lcl_otp_close_confirm"), gen_cancel);
        }

        $(() => {
            iface.button.disable("btn_accept_submit");

            $("#otpcode").on("input", event => {
                if (event.currentTarget.value === "") {
                    iface.button.disable("btn_accept_submit");
                } else {
                    iface.button.enable("btn_accept_submit");
                }
            });
            $("form[name=u2]").on("submit", event => {
                event.preventDefault();
                $("#btn_accept_submit").trigger("click");
            });
        });
    </script>
  </head>

  <body class="fclt-modal" id="mod_authQR">
<%
    MODAL_START();
%>  <form name="u2"><%
    MODAL_BLOCK_START("", L("lcl_otp_authenticatie_activate"), {icon: "fa-shield-check"});

    var secret32 = binary_to_base32(hex_to_binary(newsecret));
%>
    <div id="myQRheader"><%=L("lcl_otp_auth_header").format(secret32)%></div>
        <center><img alt="<%=L("lcl_fac_qrcode")%>" class="QRC" src='./qrcodeotp.asp?size=4&temp=1&prs_key=<%=theUser_key%>'></center>
    <div id="myQRfooter"><%=L("lcl_otp_auth_footer")%></div>
    <br>
    <label for='otpcode'><%=L("lcl_otp_auth_verification")%></label>
    <br><input id='otpcode' name='otpcode' type='text' class='fldshort'>
<%

    MODAL_BLOCK_END();
    var buttons =  [ {title: L("lcl_submit"), icon: "fa-fclt-save", action: "otp_accept()", singlepress: true, id: "btn_accept_submit", importance: 1  },
                     {title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "otp_cancel()", importance: 3 } ];
    SIMPLE_BLOCK_START();
        CreateButtons(buttons, { entersubmit: true });
    SIMPLE_BLOCK_END();

    IFACE.FORM_END();
%>  </form><%
    MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
