feat(backend): add skeleton discord auth controller
This commit is contained in:
parent
50257d61f8
commit
493a6e4d29
4 changed files with 48 additions and 1 deletions
|
@ -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<IActionResult> AuthenticationUrl()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ public class DatabaseContext : DbContext
|
|||
public DbSet<FediverseApplication> FediverseApplications { get; set; }
|
||||
public DbSet<Token> Tokens { get; set; }
|
||||
public DbSet<Application> Applications { get; set; }
|
||||
public DbSet<TemporaryKey> TemporaryKeys { get; set; }
|
||||
|
||||
public DatabaseContext(Config config)
|
||||
{
|
||||
|
@ -47,6 +48,7 @@ public class DatabaseContext : DbContext
|
|||
{
|
||||
modelBuilder.Entity<User>().HasIndex(u => u.Username).IsUnique();
|
||||
modelBuilder.Entity<Member>().HasIndex(m => new { m.UserId, m.Name }).IsUnique();
|
||||
modelBuilder.Entity<TemporaryKey>().HasIndex(k => k.Key).IsUnique();
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.OwnsOne(u => u.Fields, f => f.ToJson())
|
||||
|
|
|
@ -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<string?> 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;
|
||||
}
|
||||
}
|
|
@ -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; }
|
Loading…
Reference in a new issue