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

    File:           login_save.asp
    Description:    Username/wachtwoord verificatie van interactieve inlogger
    Parameters:
    Context:
    Note:           Als de gebruiker ook een otp-code moet opgeven komen we
                    hier *twee* keer langs, de tweede keer met een code

                    Bij success geven we een jwt-claim terug, de gebruiker is
                    nog niet vanzelf ingelogd!
*/ %>
<%
var JSON_Result = true;
var ANONYMOUS_Allowed = 1;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./login.inc" -->
<%
protectRequest.validateToken();

var nm = getFParam("vis_name");
nm = nm.replace(/^\s+|\s+$/g,""); // trim spaces
var ps = getFParam("vis_pswd");
ps = ps.replace(/^\s+|\s+$/g,""); // trim spaces
var otpcode = getFParam("otpcode", "");

var fac_lang = getFParamSafe("fac_lang", "");

var remember = getFParam("remember", "off")=="on";
var ismobile = getFParam("mobile");

var ident = getIdentity(nm, ps, { mobile: ismobile, otpcode: otpcode });

var result = { success: ident.user_key > 0 };

if (ident.user_key > 0)
{
    doLogin(ident.user_key, {});
    if (S("login_remember_days") > 0 && remember)
        makeSessionCookie("Remember Login");

    if (S("prs_password_otp_mode") & 4 && user.haspw() &&
         !user.otpsecret() &&
         !user.isSystemUser() &&
         !user.isGroupedUser())
    {
        if ((S("prs_password_otp_mode") & 1 && user.has("WEB_PRSSYS")) ||
            (S("prs_password_otp_mode") & 2))
       {
            Session("must_set_otp") = 1;
            // Dit triggert in common.inc een redirect naar /appl/prs/prs_perslid_otp_new.asp
       }
    }


    result.success = true;
}
else
{
    if (ident.otp_user_key > 0)
    {
        var otp_user = new Perslid(ident.otp_user_key);
        var issuer = L("lcl_otp_issuer").format(HTTP.urlzelf());
        var friendly = L("lcl_otp_friendly").format(customerId, otp_user.oslogin(), otp_user.naam());

        result.otp_prompt = L("lcl_otp_enter").format(friendly, issuer);
        result.success = true;
    }
    else
    {
        result.message = ident.fail_reason;
        shared.auditfail(L("lcl_autfai_loginfailed").format(nm, result.message));
    }
}

if (result.success && fac_lang && fac_lang in lcl.languages && user_key > 0)
{
    sql = "UPDATE prs_perslid SET prs_perslid_lang = "+ safe.quoted_sql(fac_lang)
        + " WHERE prs_perslid_key = " + user_key;

    Oracle.Execute(sql);
    Session("user_lang") = "" + fac_lang;
}

Response.Write(JSON.stringify(result));

ASPPAGE_END(); %>
