345ae0a4b5
CI/CD / CI · Admin API (dotnet build) (push) Successful in 41s
CI/CD / CI · Admin Web (tsc) (push) Failing after 5s
CI/CD / CI · Website (tsc) (push) Failing after 4s
CI/CD / CI · Koja (tsc) (push) Failing after 5s
CI/CD / CI · API (dotnet build + test) (push) Successful in 1m13s
CI/CD / CI · Dashboard (tsc) (push) Failing after 2m32s
CI/CD / Deploy · all services (push) Has been skipped
56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using System.Text.Json;
|
|
using StackExchange.Redis;
|
|
|
|
namespace Meezi.API.Services;
|
|
|
|
public record RefreshTokenPayload(
|
|
string UserId,
|
|
string CafeId,
|
|
string Role,
|
|
string PlanTier,
|
|
string Language,
|
|
string Actor = Meezi.Core.Constants.MeeziActorKinds.Merchant,
|
|
string? ActiveBranchId = null);
|
|
|
|
public interface IRefreshTokenStore
|
|
{
|
|
Task StoreAsync(string refreshToken, RefreshTokenPayload payload, TimeSpan ttl, CancellationToken cancellationToken = default);
|
|
Task<RefreshTokenPayload?> GetAsync(string refreshToken, CancellationToken cancellationToken = default);
|
|
Task RevokeAsync(string refreshToken, CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
public class RedisRefreshTokenStore : IRefreshTokenStore
|
|
{
|
|
private readonly IConnectionMultiplexer _redis;
|
|
|
|
public RedisRefreshTokenStore(IConnectionMultiplexer redis)
|
|
{
|
|
_redis = redis;
|
|
}
|
|
|
|
private static string Key(string token) => $"refresh:{token}";
|
|
|
|
public async Task StoreAsync(string refreshToken, RefreshTokenPayload payload, TimeSpan ttl, CancellationToken cancellationToken = default)
|
|
{
|
|
var db = _redis.GetDatabase();
|
|
var json = JsonSerializer.Serialize(payload);
|
|
await db.StringSetAsync(Key(refreshToken), json, ttl);
|
|
}
|
|
|
|
public async Task<RefreshTokenPayload?> GetAsync(string refreshToken, CancellationToken cancellationToken = default)
|
|
{
|
|
var db = _redis.GetDatabase();
|
|
var value = await db.StringGetAsync(Key(refreshToken));
|
|
if (value.IsNullOrEmpty)
|
|
return null;
|
|
|
|
return JsonSerializer.Deserialize<RefreshTokenPayload>(value.ToString());
|
|
}
|
|
|
|
public async Task RevokeAsync(string refreshToken, CancellationToken cancellationToken = default)
|
|
{
|
|
var db = _redis.GetDatabase();
|
|
await db.KeyDeleteAsync(Key(refreshToken));
|
|
}
|
|
}
|