From 493a6e4d298cd767ffa48789891d97cb4f80323c Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 10 Jun 2024 16:25:49 +0200 Subject: [PATCH] feat(backend): add skeleton discord auth controller --- .../Authentication/DiscordAuthController.cs | 14 +++++++++ Foxnouns.Backend/Database/DatabaseContext.cs | 2 ++ .../Database/DatabaseQueryExtensions.cs | 31 +++++++++++++++++++ .../Models/{Cache.cs => TemporaryKey.cs} | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Foxnouns.Backend/Controllers/Authentication/DiscordAuthController.cs rename Foxnouns.Backend/Database/Models/{Cache.cs => TemporaryKey.cs} (89%) diff --git a/Foxnouns.Backend/Controllers/Authentication/DiscordAuthController.cs b/Foxnouns.Backend/Controllers/Authentication/DiscordAuthController.cs new file mode 100644 index 0000000..4934ff5 --- /dev/null +++ b/Foxnouns.Backend/Controllers/Authentication/DiscordAuthController.cs @@ -0,0 +1,14 @@ +using Foxnouns.Backend.Database; +using Microsoft.AspNetCore.Mvc; + +namespace Foxnouns.Backend.Controllers.Authentication; + +[Route("/api/v2/auth/discord")] +public class DiscordAuthController(Config config, DatabaseContext db) : ApiControllerBase +{ + [HttpPost("url")] + public async Task AuthenticationUrl() + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Foxnouns.Backend/Database/DatabaseContext.cs b/Foxnouns.Backend/Database/DatabaseContext.cs index f9ec686..03e2c42 100644 --- a/Foxnouns.Backend/Database/DatabaseContext.cs +++ b/Foxnouns.Backend/Database/DatabaseContext.cs @@ -17,6 +17,7 @@ public class DatabaseContext : DbContext public DbSet FediverseApplications { get; set; } public DbSet Tokens { get; set; } public DbSet Applications { get; set; } + public DbSet TemporaryKeys { get; set; } public DatabaseContext(Config config) { @@ -47,6 +48,7 @@ public class DatabaseContext : DbContext { modelBuilder.Entity().HasIndex(u => u.Username).IsUnique(); modelBuilder.Entity().HasIndex(m => new { m.UserId, m.Name }).IsUnique(); + modelBuilder.Entity().HasIndex(k => k.Key).IsUnique(); modelBuilder.Entity() .OwnsOne(u => u.Fields, f => f.ToJson()) diff --git a/Foxnouns.Backend/Database/DatabaseQueryExtensions.cs b/Foxnouns.Backend/Database/DatabaseQueryExtensions.cs index 40e333c..0b77ddb 100644 --- a/Foxnouns.Backend/Database/DatabaseQueryExtensions.cs +++ b/Foxnouns.Backend/Database/DatabaseQueryExtensions.cs @@ -1,7 +1,9 @@ using System.Security.Cryptography; using Foxnouns.Backend.Database.Models; using Foxnouns.Backend.Utils; +using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.EntityFrameworkCore; +using NodaTime; namespace Foxnouns.Backend.Database; @@ -84,4 +86,33 @@ public static class DatabaseQueryExtensions await context.SaveChangesAsync(); return app; } + + public static Task SetKeyAsync(this DatabaseContext context, string key, string value, Duration expireAfter) => + context.SetKeyAsync(key, value, SystemClock.Instance.GetCurrentInstant() + expireAfter); + + public static async Task SetKeyAsync(this DatabaseContext context, string key, string value, Instant expires) + { + context.TemporaryKeys.Add(new TemporaryKey + { + Expires = expires, + Key = key, + Value = value, + }); + await context.SaveChangesAsync(); + } + + public static async Task GetKeyAsync(this DatabaseContext context, string key, + bool delete = false) + { + var value = await context.TemporaryKeys.FirstOrDefaultAsync(k => k.Key == key); + if (value == null) return null; + + if (delete) + { + await context.TemporaryKeys.Where(k => k.Key == key).ExecuteDeleteAsync(); + await context.SaveChangesAsync(); + } + + return value.Value; + } } \ No newline at end of file diff --git a/Foxnouns.Backend/Database/Models/Cache.cs b/Foxnouns.Backend/Database/Models/TemporaryKey.cs similarity index 89% rename from Foxnouns.Backend/Database/Models/Cache.cs rename to Foxnouns.Backend/Database/Models/TemporaryKey.cs index 81d4b2b..d3dbfc8 100644 --- a/Foxnouns.Backend/Database/Models/Cache.cs +++ b/Foxnouns.Backend/Database/Models/TemporaryKey.cs @@ -2,7 +2,7 @@ using NodaTime; namespace Foxnouns.Backend.Database.Models; -public class Cache +public class TemporaryKey { public long Id { get; init; } public required string Key { get; init; }