using Mirea.Api.Security.Common.Domain; using Mirea.Api.Security.Common.Dto.Requests; using Mirea.Api.Security.Common.Dto.Responses; using Mirea.Api.Security.Common.Interfaces; using System; using System.Text.Json; using System.Threading; using System.Threading.Tasks; namespace Mirea.Api.Security.Services; public class PreAuthService(ICacheService cache) { public TimeSpan Lifetime { private get; init; } private static string GeneratePreAuthToken() => Guid.NewGuid().ToString().Replace("-", "") + GeneratorKey.GenerateString(16); private static string GetPreAuthCacheKey(string fingerprint) => $"{fingerprint}_pre_auth_token"; { var firstAuthToken = GenerateFirstAuthToken(); var loginStructure = new PreAuthToken { Fingerprint = request.Fingerprint, UserId = userId, UserAgent = request.UserAgent, Token = firstAuthToken }; await cache.SetAsync( GetPreAuthCacheKey(request.Fingerprint), JsonSerializer.SerializeToUtf8Bytes(loginStructure), Lifetime, cancellation); return new PreAuthTokenResponse { Token = firstAuthToken, ExpiresIn = DateTime.UtcNow.Add(Lifetime) }; } }