2024-05-29 05:50:47 +03:00
|
|
|
|
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;
|
2024-05-29 05:32:22 +03:00
|
|
|
|
|
|
|
|
|
namespace Mirea.Api.Security.Services;
|
|
|
|
|
|
2024-05-29 05:50:47 +03:00
|
|
|
|
public class AuthService(ICacheService cache)
|
2024-05-29 05:32:22 +03:00
|
|
|
|
{
|
|
|
|
|
public TimeSpan Lifetime { private get; init; }
|
2024-05-29 05:35:44 +03:00
|
|
|
|
|
|
|
|
|
private static string GenerateRefreshToken() => Guid.NewGuid().ToString().Replace("-", "") +
|
|
|
|
|
GeneratorKey.GenerateString(32);
|
2024-05-29 05:36:26 +03:00
|
|
|
|
|
|
|
|
|
private static string GetAuthCacheKey(string fingerprint) => $"{fingerprint}_auth_token";
|
2024-05-29 05:50:47 +03:00
|
|
|
|
|
|
|
|
|
private Task SetAuthTokenDataToCache(string fingerprint, AuthToken data, CancellationToken cancellation) =>
|
|
|
|
|
cache.SetAsync(
|
|
|
|
|
GetAuthCacheKey(fingerprint),
|
|
|
|
|
JsonSerializer.SerializeToUtf8Bytes(data),
|
|
|
|
|
slidingExpiration: Lifetime,
|
|
|
|
|
cancellationToken: cancellation);
|
2024-05-29 05:32:22 +03:00
|
|
|
|
}
|