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

    File:           login_email.asp
    Description:    Pre-login scherm
    Parameters:
    Context:
    Note:           Vraagt e-mail adres/inlogcode
                    Bepaalt aan de hand daarvan (aut_idp.aut_idp_emailwhitelist)
                    welke sso voor jou geschikt is.

*/
ANONYMOUS_Allowed = 1;
var JSON_Result = true;
%>
<!--#include file="../Shared/common.inc"-->
<% settings.overrule_setting("multi_language_option", 0); %>
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/send_mail.inc"-->
<!-- #include file="../aut/login.inc" -->
<%

var email      = getFParam("prs_email");
email = email.replace(/^\s+|\s+$/g,""); // trim spaces

// Uberhaupt syntactisch geldige e-mail?
if (!new RegExp(S("email_regexp_single"), "ig").test(email))
    abort_with_warning(L("lcl_invalid_email"));

// En valt het ook binnen onze eventuele white-list?
var sql = "SELECT aut_idp_saml_metaurl aut_idp_email_whitelist"
        + "     , aut_idp_type"
        + "     , aut_idp_code"
        + "  FROM aut_idp"
        + " WHERE aut_idp_saml_metaurl IS NOT NULL";
var oRs = Oracle.Execute(sql);
var idp_code = null;
while (!oRs.EOF)
{
    var email_whitelist = oRs("aut_idp_email_whitelist").Value;
    if (new RegExp(email_whitelist, "i").test(email))
    {
        idp_code = oRs("aut_idp_code").Value;
        idp_type = oRs("aut_idp_type").Value;
    }
    oRs.MoveNext();
}
if (!idp_code)
   abort_with_warning(L("lcl_self_register_email_wrong"));

oRs.Close()

if (idp_type == 4) // JWT, dit zal zelfregistratie zijn geweest
{
    var sql = "SELECT prs_perslid_key"
            + "  FROM prs_v_aanwezigperslid"
            + " WHERE upper(prs_perslid_email) = " + safe.quoted_sql_upper(email);
    var oRs = Oracle.Execute(sql)
    if (oRs.Eof)
        url = rooturl + "/appl/aut/idp_self_register.asp?prs_email=" + safe.url(email);
    else
        url = rooturl + "/appl/aut/login.asp?prs_email=" + safe.url(email);
    oRs.Close()
    var result = { success: true, redirect_uri: protectQS.create(url) };
}
else if (idp_type == 5) // SAML, doe SSO, er volgt misschien wel provisioning
{
    url = rooturl + "?sso=" + idp_code;
    var result = { success: true, redirect_uri: url };
}
else
   abort_with_warning(L("Internal IdP error " + idp_code + ":" + idp_type)); // Dit snap ik niet.

Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
