2024-09-09 14:37:59 +02:00
|
|
|
using Foxnouns.Backend.Database;
|
2024-11-03 13:53:16 +01:00
|
|
|
using Foxnouns.Backend.Database.Models;
|
2024-06-13 02:23:55 +02:00
|
|
|
using Foxnouns.Backend.Services;
|
|
|
|
using Foxnouns.Backend.Utils;
|
2024-09-10 02:39:07 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-06-13 02:23:55 +02:00
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Extensions;
|
|
|
|
|
|
|
|
public static class KeyCacheExtensions
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
public static async Task<string> GenerateAuthStateAsync(
|
|
|
|
this KeyCacheService keyCacheService,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
2024-06-13 02:23:55 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
string state = AuthUtils.RandomToken().Replace('+', '-').Replace('/', '_');
|
2024-09-09 14:50:00 +02:00
|
|
|
await keyCacheService.SetKeyAsync($"oauth_state:{state}", "", Duration.FromMinutes(10), ct);
|
2024-06-13 02:23:55 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public static async Task ValidateAuthStateAsync(
|
|
|
|
this KeyCacheService keyCacheService,
|
|
|
|
string state,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
2024-06-13 02:23:55 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
string? val = await keyCacheService.GetKeyAsync($"oauth_state:{state}", ct: ct);
|
2024-10-02 00:28:07 +02:00
|
|
|
if (val == null)
|
|
|
|
throw new ApiError.BadRequest("Invalid OAuth state");
|
2024-06-13 02:23:55 +02:00
|
|
|
}
|
2024-09-09 14:37:59 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public static async Task<string> GenerateRegisterEmailStateAsync(
|
|
|
|
this KeyCacheService keyCacheService,
|
|
|
|
string email,
|
|
|
|
Snowflake? userId = null,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
2024-09-09 14:37:59 +02:00
|
|
|
{
|
2024-09-10 02:39:07 +02:00
|
|
|
// This state is used in links, not just as JSON values, so make it URL-safe
|
2024-12-08 15:07:25 +01:00
|
|
|
string state = AuthUtils.RandomToken().Replace('+', '-').Replace('/', '_');
|
2024-10-02 00:28:07 +02:00
|
|
|
await keyCacheService.SetKeyAsync(
|
|
|
|
$"email_state:{state}",
|
|
|
|
new RegisterEmailState(email, userId),
|
|
|
|
Duration.FromDays(1),
|
|
|
|
ct
|
|
|
|
);
|
2024-09-09 14:37:59 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public static async Task<RegisterEmailState?> GetRegisterEmailStateAsync(
|
|
|
|
this KeyCacheService keyCacheService,
|
|
|
|
string state,
|
|
|
|
CancellationToken ct = default
|
2024-12-04 17:43:02 +01:00
|
|
|
) => await keyCacheService.GetKeyAsync<RegisterEmailState>($"email_state:{state}", ct: ct);
|
2024-11-03 13:53:16 +01:00
|
|
|
|
|
|
|
public static async Task<string> GenerateAddExtraAccountStateAsync(
|
|
|
|
this KeyCacheService keyCacheService,
|
|
|
|
AuthType authType,
|
|
|
|
Snowflake userId,
|
2024-12-04 01:48:52 +01:00
|
|
|
string? instance = null,
|
2024-11-03 13:53:16 +01:00
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
string state = AuthUtils.RandomToken();
|
2024-11-03 13:53:16 +01:00
|
|
|
await keyCacheService.SetKeyAsync(
|
|
|
|
$"add_account:{state}",
|
2024-12-04 01:48:52 +01:00
|
|
|
new AddExtraAccountState(authType, userId, instance),
|
2024-11-03 13:53:16 +01:00
|
|
|
Duration.FromDays(1),
|
|
|
|
ct
|
|
|
|
);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<AddExtraAccountState?> GetAddExtraAccountStateAsync(
|
|
|
|
this KeyCacheService keyCacheService,
|
|
|
|
string state,
|
|
|
|
CancellationToken ct = default
|
2024-12-08 15:07:25 +01:00
|
|
|
) => await keyCacheService.GetKeyAsync<AddExtraAccountState>($"add_account:{state}", true, ct);
|
2024-09-09 14:37:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-10 02:39:07 +02:00
|
|
|
public record RegisterEmailState(
|
|
|
|
string Email,
|
2024-10-02 00:28:07 +02:00
|
|
|
[property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] Snowflake? ExistingUserId
|
|
|
|
);
|
2024-11-03 13:53:16 +01:00
|
|
|
|
2024-12-04 01:48:52 +01:00
|
|
|
public record AddExtraAccountState(AuthType AuthType, Snowflake UserId, string? Instance = null);
|