feat: add generate auth token
This commit is contained in:
parent
f3063c5322
commit
81f2f995b0
@ -3,6 +3,10 @@ using Mirea.Api.Security.Common.Dto.Requests;
|
|||||||
using Mirea.Api.Security.Common.Dto.Responses;
|
using Mirea.Api.Security.Common.Dto.Responses;
|
||||||
using Mirea.Api.Security.Common.Interfaces;
|
using Mirea.Api.Security.Common.Interfaces;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Security;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Mirea.Api.Security.Services;
|
namespace Mirea.Api.Security.Services;
|
||||||
|
|
||||||
@ -23,4 +27,31 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService)
|
|||||||
JsonSerializer.SerializeToUtf8Bytes(data),
|
JsonSerializer.SerializeToUtf8Bytes(data),
|
||||||
slidingExpiration: Lifetime,
|
slidingExpiration: Lifetime,
|
||||||
cancellationToken: cancellation);
|
cancellationToken: cancellation);
|
||||||
|
|
||||||
|
public async Task<AuthTokenResponse> GenerateAuthTokensAsync(TokenRequest request, string preAuthToken, CancellationToken cancellation = default)
|
||||||
|
{
|
||||||
|
string userId = await new PreAuthService(cache).MatchToken(request, preAuthToken, cancellation);
|
||||||
|
|
||||||
|
var refreshToken = GenerateRefreshToken();
|
||||||
|
var accessToken = GenerateAccessToken(userId);
|
||||||
|
|
||||||
|
var authTokenStruct = new AuthToken
|
||||||
|
{
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
Ip = request.Ip,
|
||||||
|
RefreshToken = refreshToken,
|
||||||
|
UserAgent = request.UserAgent,
|
||||||
|
UserId = userId,
|
||||||
|
AccessToken = accessToken.Token
|
||||||
|
};
|
||||||
|
|
||||||
|
await SetAuthTokenDataToCache(request.Fingerprint, authTokenStruct, cancellation);
|
||||||
|
|
||||||
|
return new AuthTokenResponse
|
||||||
|
{
|
||||||
|
AccessToken = accessToken.Token,
|
||||||
|
ExpiresIn = accessToken.ExpireIn,
|
||||||
|
RefreshToken = authTokenStruct.RefreshToken
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user