fix: add refresh expire date

This commit is contained in:
Polianin Nikita 2024-06-21 21:36:11 +03:00
parent a5f9e67647
commit 039d323643
2 changed files with 14 additions and 11 deletions

View File

@ -5,6 +5,7 @@ namespace Mirea.Api.Security.Common.Dto.Responses;
public class AuthTokenResponse
{
public required string AccessToken { get; set; }
public DateTime AccessExpiresIn { get; set; }
public required string RefreshToken { get; set; }
public DateTime ExpiresIn { get; set; }
public DateTime RefreshExpiresIn { get; set; }
}

View File

@ -34,7 +34,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
public async Task<AuthTokenResponse> GenerateAuthTokensAsync(TokenRequest request, string userId, CancellationToken cancellation = default)
{
var refreshToken = GenerateRefreshToken();
var accessToken = GenerateAccessToken(userId);
var (token, expireIn) = GenerateAccessToken(userId);
var authTokenStruct = new AuthToken
{
@ -43,16 +43,17 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
RefreshToken = refreshToken,
UserAgent = request.UserAgent,
UserId = userId,
AccessToken = accessToken.Token
AccessToken = token
};
await SetAuthTokenDataToCache(request.Fingerprint, authTokenStruct, cancellation);
return new AuthTokenResponse
{
AccessToken = accessToken.Token,
ExpiresIn = accessToken.ExpireIn,
RefreshToken = authTokenStruct.RefreshToken
AccessToken = token,
AccessExpiresIn = expireIn,
RefreshToken = authTokenStruct.RefreshToken,
RefreshExpiresIn = DateTime.UtcNow.Add(Lifetime),
};
}
@ -77,17 +78,18 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
throw new SecurityException(request.Fingerprint);
}
var accessToken = GenerateAccessToken(authToken.UserId);
var (token, expireIn) = GenerateAccessToken(authToken.UserId);
await RevokeAccessToken(authToken.AccessToken);
authToken.AccessToken = accessToken.Token;
authToken.AccessToken = token;
await SetAuthTokenDataToCache(request.Fingerprint, authToken, cancellation);
return new AuthTokenResponse
{
AccessToken = accessToken.Token,
ExpiresIn = accessToken.ExpireIn,
RefreshToken = GenerateRefreshToken()
AccessToken = token,
AccessExpiresIn = expireIn,
RefreshToken = GenerateRefreshToken(),
RefreshExpiresIn = DateTime.UtcNow.Add(Lifetime)
};
}