Add Application configuration #11
42
Security/Services/PreAuthService.cs
Normal file
42
Security/Services/PreAuthService.cs
Normal file
@ -0,0 +1,42 @@
|
||||
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 GenerateFirstAuthToken() => Guid.NewGuid().ToString().Replace("-", "");
|
||||
|
||||
public async Task<PreAuthTokenResponse> CreateLoginTokenAsync(TokenRequest request, string userId, CancellationToken cancellation = default)
|
||||
{
|
||||
var firstAuthToken = GenerateFirstAuthToken();
|
||||
|
||||
var loginStructure = new PreAuthToken
|
||||
{
|
||||
Fingerprint = request.Fingerprint,
|
||||
UserId = userId,
|
||||
UserAgent = request.UserAgent,
|
||||
Token = firstAuthToken
|
||||
};
|
||||
|
||||
await cache.SetAsync(
|
||||
request.Fingerprint,
|
||||
JsonSerializer.SerializeToUtf8Bytes(loginStructure),
|
||||
Lifetime,
|
||||
cancellation);
|
||||
|
||||
return new PreAuthTokenResponse
|
||||
{
|
||||
Token = firstAuthToken,
|
||||
ExpiresIn = DateTime.UtcNow.Add(Lifetime)
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user