diff options
| author | Tim <contact@bytim.eu> | 2025-12-25 19:59:26 +0100 |
|---|---|---|
| committer | Tim <contact@bytim.eu> | 2025-12-25 19:59:26 +0100 |
| commit | 771b949618bb4e07c09c2fb94a7f92e13f471b9e (patch) | |
| tree | 88c2e69fc14bef91e414c605712420437cc92ccf /ShareGuard.Web/Api.cs | |
Diffstat (limited to 'ShareGuard.Web/Api.cs')
| -rw-r--r-- | ShareGuard.Web/Api.cs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/ShareGuard.Web/Api.cs b/ShareGuard.Web/Api.cs new file mode 100644 index 0000000..990abc9 --- /dev/null +++ b/ShareGuard.Web/Api.cs @@ -0,0 +1,40 @@ +namespace ShareGuard.Web; + +public static class Api +{ + private const string TemplateFilePath = "./wg-quick-template.txt"; + + public static async Task Redeem(HttpContext context) + { + await using var db = new Database(); + var link = db.Links.FirstOrDefault(link => link.Token == context.Request.RouteValues["token"]!.ToString() + && link.PeerPublicKey == null); + if (link == null) + { + context.Response.StatusCode = StatusCodes.Status403Forbidden; + await context.Response.WriteAsync("Invalid token."); + return; + } + + var form = await context.Request.ReadFormAsync(); + var peerPublicKey = form["PublicKey"]; + if (peerPublicKey.Count < 1) + { + context.Response.StatusCode = StatusCodes.Status400BadRequest; + await context.Response.WriteAsync("Bad request."); + return; + } + + link.PeerPublicKey = peerPublicKey[0]; + + if (!File.Exists(TemplateFilePath)) + { + context.Response.StatusCode = StatusCodes.Status204NoContent; + await context.Response.WriteAsync("Template is not set yet."); + return; + } + + db.SaveChanges(); + await context.Response.WriteAsync(File.ReadAllText(TemplateFilePath).Replace("{ip}", link.IpAddress)); + } +}
\ No newline at end of file |
