Files
Facilitor/APPL/API/api_msgraphnotification.asp
Koen Reefman 1be764b6b5 Merge 2023.3 Gold B changes
svn path=/Website/trunk/; revision=63306
2024-01-23 12:34:53 +00:00

150 lines
6.1 KiB
Plaintext

<%@ language = "JavaScript" %>
<% /*
$Revision$
$Id$
File: api_msgraphnotification.asp
Description: Opvangscript voor msgraph notifications
Parameters:
Status:
Context:
Notes:
*/
DOCTYPE_Disable = 1; // Anders problemen met het terugsturen van het validationtoken
THIS_FILE = "appl/api/api_msgraphnotification.asp";
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../api/api.inc" -->
<%
var API = new API_func();
// Bij aanmaken van de subscription doet Graph direct aan validatie-aanroep
// De token parameter moet je teruggeven.
var validationToken = getQParam("validationToken", "");
if (validationToken)
{
__Log("Subscribtion validated: " + validationToken);
Response.ContentType = "text/plain";
Response.Write(validationToken);
Response.End;
}
else
{ // We hebben een notificatie ontvangen, stuur direct een 202 conform specificatie
Response.Status = "202 - Accepted";
Response.Write("");
Response.Flush;
}
function _cleanIncomingOutlookQueue() {
Application.Lock();
var incomingOutlookQueue = JSON.parse(Application(customerId + "_incomingOutlookQueue") || "{}");
for (var x in incomingOutlookQueue) {
if (incomingOutlookQueue[x] < new Date().getTime()) { // Verwijder verlopen changes die hier nog in staan
delete incomingOutlookQueue[x];
}
}
Application(customerId + "_incomingOutlookQueue") = JSON.stringify(incomingOutlookQueue);
Application.UnLock();
}
function _updateIncomingOutlookQueue(etag, CRUD) { // etag = 'The HTTP entity tag that represents the version of the object'
Application.Lock();
var incomingOutlookQueue = JSON.parse(Application(customerId + "_incomingOutlookQueue") || "{}");
if (CRUD == "C") {
if (etag in incomingOutlookQueue) { // We hebben deze al ontvangen en zijn begonnen met verwerken
__Log("MS Graph, duplicate notification ignored", "#FFFF44");
Application.UnLock();
Response.End;
}
incomingOutlookQueue[etag] = new Date().getTime() + 4 * 60 * 60 * 1000; // Expired in 4 uur
} else if (CRUD == "D") {
// delete incomingOutlookQueue[etag];
// ipv deleten laten we deze expiren over 3s zodat we reeds verwerkte notificaties niet nog eens gaan verwerken als het HTTP verkeer elkaar kruist
if (etag in incomingOutlookQueue) {
incomingOutlookQueue[etag] = new Date().getTime() + 3 * 1000;
}
}
Application(customerId + "_incomingOutlookQueue") = JSON.stringify(incomingOutlookQueue);
Application.UnLock();
}
// Als we hier komen hebben we een echte notificatie binnengekregen
var notidata = RequestJSON(API.apidata.loglevel).value;
if (notidata.length > 1) {
__DoLog("INTERNAL ERROR; We hebben meerdere notificaties in 1x ontvangen, dat kunnen we (nog) niet aan", "#FF0000");
}
// We behandelen alleen de eerste, het is overigens nog niet opgevallen dat notificaties gebundeld worden
notidata = notidata[0];
try {
var etag = notidata.resourceData["@odata.etag"];
} catch (e) {
__DoLog("Malformed MS Graph notification received:", "#FF0000");
__DoLog(notidata, "#FF0000");
Response.End;
}
_cleanIncomingOutlookQueue();
_updateIncomingOutlookQueue(etag, "C");
// {
// "value": [
// {
// "subscriptionId": "7b8d9ffc-1d50-41b7-b0c9-c85be52836fd",
// "subscriptionExpirationDateTime": "2021-02-19T04:51:01-08:00",
// "changeType": "updated",
// "resource": "Users/5381c9d7-d7ef-4b6f-930d-f7893adf0b14/Events/AAMkADgwY2U2OTNlLTUzNjgtNDI4Zi1hZTdmLWU0NzRlODA0MTQ3YQBGAAAAAAAiY_3E-_MuTaCgYlN5eIOoBwAsO-qkNQq_Sqb0Bq5U7JV3AAAAAAENAAAsO-qkNQq_Sqb0Bq5U7JV3AAC550IPAAA=",
// "resourceData": {
// "@odata.type": "#Microsoft.Graph.Event",
// "@odata.id": "Users/5381c9d7-d7ef-4b6f-930d-f7893adf0b14/Events/AAMkADgwY2U2OTNlLTUzNjgtNDI4Zi1hZTdmLWU0NzRlODA0MTQ3YQBGAAAAAAAiY_3E-_MuTaCgYlN5eIOoBwAsO-qkNQq_Sqb0Bq5U7JV3AAAAAAENAAAsO-qkNQq_Sqb0Bq5U7JV3AAC550IPAAA=",
// "@odata.etag": "W/\"DwAAABYAAAAsO/qkNQq+Sqb0Bq5U7JV3AAC50Ux3\"",
// "id": "AAMkADgwY2U2OTNlLTUzNjgtNDI4Zi1hZTdmLWU0NzRlODA0MTQ3YQBGAAAAAAAiY_3E-_MuTaCgYlN5eIOoBwAsO-qkNQq_Sqb0Bq5U7JV3AAAAAAENAAAsO-qkNQq_Sqb0Bq5U7JV3AAC550IPAAA="
// },
// "clientState": "b70f833c968537eb8f6e86a8c50de7e3221c8485",
// "tenantId": "bab7c51b-2329-47f1-b6ff-1a5270efc193"
// }
// ]
// }
// We hebben res_ruimte_key meegegeven in de hookurl. Er zijn meer manieren
// om de ruimte te achterhalen maar deze is het snelst/ simpelst
var res_ruimte_key = getQParamInt("res_ruimte", -1);
if (res_ruimte_key == -1)
Response.End; // even geen errors
var sql = "SELECT res_ruimte_extern_id,"
+ " res_ruimte_graphhooksecret"
+ " FROM res_ruimte"
+ " WHERE res_ruimte_key = " + res_ruimte_key;
var oRs = Oracle.Execute(sql);
var zaalemail = oRs("res_ruimte_extern_id").Value;
__Log("Incoming hook notification for {0}".format(zaalemail));
if (zaalemail == null || oRs("res_ruimte_graphhooksecret").Value != notidata.clientState)
{
if (zaalemail == null) {
__Log("Extern ID (and with that, the subscription) has been removed");
} else {
__Log(notidata);
__Log("clientState does not match (should {0} is {1}.\nOld subscription? It will expire {2}".format(oRs("res_ruimte_graphhooksecret").Value,
notidata.clientState,
notidata.subscriptionExpirationDateTime));
}
oRs.Close();
Response.End;
}
oRs.Close();
var DEZE = this;
var hook = hookfunc.gethook("utils/exchange/process_webhook.wsc");
hook.initialize({
DEZE: DEZE,
custabspath: Server.MapPath(custpath)
});
hook.process_webhook(res_ruimte_key, zaalemail, notidata);
hook = null;
_updateIncomingOutlookQueue(etag, "D"); // En verwijder hem maar weer
Response.Write("");
ASPPAGE_END();
%>