start (actual) email auth, add CancellationToken to most async methods
This commit is contained in:
parent
acc54a55bc
commit
344a0071e5
22 changed files with 325 additions and 152 deletions
|
@ -1,3 +1,4 @@
|
|||
using Foxnouns.Backend.Database;
|
||||
using Foxnouns.Backend.Services;
|
||||
using Foxnouns.Backend.Utils;
|
||||
using NodaTime;
|
||||
|
@ -6,16 +7,31 @@ namespace Foxnouns.Backend.Extensions;
|
|||
|
||||
public static class KeyCacheExtensions
|
||||
{
|
||||
public static async Task<string> GenerateAuthStateAsync(this KeyCacheService keyCacheSvc)
|
||||
public static async Task<string> GenerateAuthStateAsync(this KeyCacheService keyCacheSvc, CancellationToken ct = default)
|
||||
{
|
||||
var state = AuthUtils.RandomToken();
|
||||
await keyCacheSvc.SetKeyAsync($"oauth_state:{state}", "", Duration.FromMinutes(10));
|
||||
await keyCacheSvc.SetKeyAsync($"oauth_state:{state}", "", Duration.FromMinutes(10), ct);
|
||||
return state;
|
||||
}
|
||||
|
||||
public static async Task ValidateAuthStateAsync(this KeyCacheService keyCacheSvc, string state)
|
||||
public static async Task ValidateAuthStateAsync(this KeyCacheService keyCacheSvc, string state, CancellationToken ct = default)
|
||||
{
|
||||
var val = await keyCacheSvc.GetKeyAsync($"oauth_state:{state}", delete: true);
|
||||
var val = await keyCacheSvc.GetKeyAsync($"oauth_state:{state}", delete: true, ct);
|
||||
if (val == null) throw new ApiError.BadRequest("Invalid OAuth state");
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> GenerateRegisterEmailStateAsync(this KeyCacheService keyCacheSvc, string email,
|
||||
Snowflake? userId = null, CancellationToken ct = default)
|
||||
{
|
||||
var state = AuthUtils.RandomToken();
|
||||
await keyCacheSvc.SetKeyAsync($"email_state:{state}", new RegisterEmailState(email, userId),
|
||||
Duration.FromDays(1), ct);
|
||||
return state;
|
||||
}
|
||||
|
||||
public static async Task<RegisterEmailState?> GetRegisterEmailStateAsync(this KeyCacheService keyCacheSvc,
|
||||
string state, CancellationToken ct = default) =>
|
||||
await keyCacheSvc.GetKeyAsync<RegisterEmailState>($"email_state:{state}", delete: true, ct);
|
||||
}
|
||||
|
||||
public record RegisterEmailState(string Email, Snowflake? ExistingUserId);
|
Loading…
Add table
Add a link
Reference in a new issue