-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
77 lines (57 loc) · 2.63 KB
/
Program.cs
File metadata and controls
77 lines (57 loc) · 2.63 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
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
using System.Net;
using Microsoft.AspNetCore.Mvc;
using WebhookReceiver;
var builder = WebApplication.CreateBuilder(args);
var settings = builder.Configuration.GetSection("Settings").Get<Settings>() ?? new Settings();
builder.Services.AddSingleton(settings);
builder.Services.AddHostedService<DeleteOldFiles>();
var app = builder.Build();
if (!Directory.Exists(settings.GetUserTempPath()))
Directory.CreateDirectory(settings.GetUserTempPath());
Console.WriteLine($"Temp file root path: {settings.GetUserTempPath()}");
IResult? ValidateRequest(HttpRequest request, string? code)
{
if (!request.Headers.ContainsKey("Authorization"))
{
if (!string.Equals(code, settings.AuthCode))
return Results.Unauthorized();
return null;
}
var authHeader = request.Headers["Authorization"][0];
if (authHeader?.StartsWith("Bearer ") == true)
{
var token = authHeader.Substring("Bearer ".Length);
if (!string.Equals(token, settings.AuthCode))
return Results.Unauthorized();
return null;
}
return Results.Unauthorized();
}
async Task<IResult> StoreRequestAsync(HttpRequest request, string id, int? returnStatus, string? body = null)
{
var prefix = string.IsNullOrWhiteSpace(id) ? string.Empty : $"{Utils.SanitizeFileName(id)}-";
var localFilePath = Path.Combine(settings.GetUserTempPath(), $"{prefix}{Guid.NewGuid()}.txt");
var headers = string.Join(Environment.NewLine, request.Headers.Select(header => $"{header.Key}: {header.Value}"));
var requestInfo = body is null
? $"{id}{request.QueryString}{Environment.NewLine}{Environment.NewLine}{headers}"
: $"{id}{request.QueryString}{Environment.NewLine}{Environment.NewLine}{headers}{Environment.NewLine}{Environment.NewLine}{body}";
await File.WriteAllTextAsync(localFilePath, requestInfo);
if (returnStatus.HasValue && Enum.IsDefined(typeof(HttpStatusCode), returnStatus.Value))
return Results.StatusCode(returnStatus.Value);
return Results.Ok();
}
app.MapGet("/{*id}", async (HttpRequest request, string id, [FromQuery]int? returnStatus, [FromQuery]string? code = "") =>
{
var unauthorizedResult = ValidateRequest(request, code);
if (unauthorizedResult is not null)
return unauthorizedResult;
return await StoreRequestAsync(request, id, returnStatus);
});
app.MapPost("/{*id}", async (HttpRequest request, string id, [FromBody]object body, [FromQuery]int? returnStatus, [FromQuery]string? code = "") =>
{
var unauthorizedResult = ValidateRequest(request, code);
if (unauthorizedResult is not null)
return unauthorizedResult;
return await StoreRequestAsync(request, id, returnStatus, body.ToString());
});
app.Run();