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

    File:           shared/qrcodeotp.asp
    Description:    Toon een QR-code van de Google Authenticator URI
    Parameters:     Geen
    Note:           (Alleen) met FACFAC mag je dat ook bij iemand anders opvragen

*/ %>

<%
var NO_OTP_OK = true;
DOCTYPE_Disable = true; // We doen een image/png
%>
<!-- #include file="../Shared/common.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 otpsecret = user_info.otpsecret();
var oslogin = user_info.oslogin();

if (getQParamInt("temp", 0))
{
    var otpsecret = Session("otp_secret_temp");
    user.auth_required_or_abort(otpsecret);
}
var otpresult = otpcodes(otpsecret);

if (otpresult.otpstep != 30)
{
    var oWhip2png = Server.CreateObject("SLNKDWF.Whip2PNG"); // Hopen dat dit wel lukt
    Response.clear;
    Response.contenttype = "image/png";
    Response.BinaryWrite(oWhip2png.TextAsPNG("Sorry, Google Authenticator cannot handle {0} second stepsize".format(otpresult.otpstep)));
    Response.End();
}

var binary = hex_to_binary(otpresult.otpseed);
var secret32 = binary_to_base32(binary); // 3132333435363738393031323334353637383930 ==> GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ

var issuer = L("lcl_otp_issuer").format(HTTP.urlzelf());
var friendly = L("lcl_otp_friendly").format(customerId, oslogin, user_info.naam());
var txt = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}".format(issuer, friendly, secret32)
__Log("QR Code: " + txt);

var xc = Server.CreateObject("SLNKDWF.QRCode");
xc.Text       = txt;
xc.Version    = getQParamInt("version", -1);
xc.Autoextend = getQParamInt("autoextend", 1)==1;
xc.Level      = getQParamInt("level", 1);
xc.Masking    = getQParamInt("masking", -1);
xc.Size       = getQParamInt("size", 8);
xc.Color      = getQParamInt("color", S("qr_color"));

if (__Logging > 0)
{
    Response.AddHeader("QRCode", xc.Text);
}

Response.clear;
Response.contenttype = "image/png";
Response.BinaryWrite(xc.GetAsPNG());
%><% ASPPAGE_END(); %>
