-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProcessNotificationData.cs
163 lines (126 loc) · 5.21 KB
/
ProcessNotificationData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
////*********************************************************
// <copyright company="Intuit">
// Author:Nimisha
//
////*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using Webhooks.Models.DTO;
using System.Web.Script.Serialization;
using System.Collections;
using Webhooks.Models.Service;
using System.Collections.Concurrent;
using System.Configuration;
namespace Webhooks.Models.Utility
{
public class ProcessNotificationData
{
private static string payloadLoaded = null;
/// <summary>
/// HASH the notification payload with HMAC_SHA256_ALGORITHM using <verifier> as the key
/// Compare the value from above step to the intuit-signature header from the notification. The values should be identical.
/// </summary>
//public static bool Validate(string payload, string verifier)
public static bool Validate(string payload, object hmacHeaderSignature)
{
payloadLoaded = payload;
//Get Webhooks verifier
string verifier = ConfigurationManager.AppSettings["WebHooksVerifier"].ToString();
if (hmacHeaderSignature == null)
{
return false;
}
try
{
var keyBytes = Encoding.UTF8.GetBytes(verifier);
var dataBytes = Encoding.UTF8.GetBytes(payloadLoaded);
//use the SHA256Managed Class to compute the hash
var hmac = new HMACSHA256(keyBytes);
var hmacBytes = hmac.ComputeHash(dataBytes);
//Get payload signature value.
//var hmacHeaderSignature = HttpContext.Current.Request.Headers[SIGNATURE];//Header value
var createPayloadSignature = Convert.ToBase64String(hmacBytes);//Payload value
//spawn new thread
//Compare webhooks response payload's signature with the signature passed in the header of the post webhooks request from Intuit. If they match, the call is verified.
if ((string)hmacHeaderSignature == createPayloadSignature)
{
//Add response payload to queue in a separate thread for processing if signature matches
Thread thread1 = new Thread(new ThreadStart(AddToQueue));
thread1.Start();
thread1.Join(60000);
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// Add webhooks notifications to queue
/// </summary>
private static void AddToQueue()
{
//Deserealiaze webhooks response payload
WebhooksNotificationdto.WebhooksData webhooksData = JsonConvert.DeserializeObject<WebhooksNotificationdto.WebhooksData>(payloadLoaded);
//Create a blocking queue/collection
BlockingCollection<WebhooksNotificationdto.WebhooksData> dataItems = new BlockingCollection<WebhooksNotificationdto.WebhooksData>(1);
Task.Run(() =>
{
// Add items to blocking collection(queue)
dataItems.Add(webhooksData);
// Let consumer know we are done.
dataItems.CompleteAdding();
});
Task.Run(() =>
{
while (!dataItems.IsCompleted)
{
//Create WebhooksData reference
WebhooksNotificationdto.WebhooksData webhooksData1 = null;
try
{
//Take our Items from blocking collection(dequeue)
webhooksData1 = dataItems.Take();
}
catch (InvalidOperationException) { }
if (webhooksData1 != null)
{
//Start processing queue items
ProcessQueueItem(webhooksData1);
}
}
});
}
/// <summary>
/// Process each item of queue
/// </summary>
private static bool ProcessQueueItem(object queueItem1)
{
WebhooksNotificationdto.WebhooksData we = (WebhooksNotificationdto.WebhooksData)queueItem1;
//Get realm from deserialized WebHooksData
foreach (WebhooksNotificationdto.EventNotifications eventNotifications in we.EventNotifications)
{
//Update last updated time for each realm in DB
DBUtility.UpdateLastUpdatedDateDB(eventNotifications.RealmId);
}
return true;
}
}
}