-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
35 lines (29 loc) · 1.06 KB
/
Copy pathProgram.cs
File metadata and controls
35 lines (29 loc) · 1.06 KB
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
using System.Net;
using System.Text;
using Mailtrap.Webhooks;
var signingSecret = Environment.GetEnvironmentVariable("MAILTRAP_WEBHOOK_SIGNING_SECRET") ?? string.Empty;
using var listener = new HttpListener();
listener.Prefixes.Add("http://localhost:9292/webhooks/mailtrap/");
listener.Start();
while (true)
{
var context = await listener.GetContextAsync();
var request = context.Request;
var response = context.Response;
// Use the raw request body — parsing and re-serializing the JSON may
// reorder keys or alter whitespace and invalidate the signature.
string payload;
using (var reader = new StreamReader(request.InputStream, Encoding.UTF8))
{
payload = await reader.ReadToEndAsync();
}
var signature = request.Headers["Mailtrap-Signature"] ?? string.Empty;
if (!WebhookSignature.Verify(payload, signature, signingSecret))
{
response.StatusCode = (int)HttpStatusCode.Unauthorized;
response.Close();
continue;
}
response.StatusCode = (int)HttpStatusCode.OK;
response.Close();
}