refactor: change token to instance token

This commit is contained in:
Polianin Nikita 2024-11-02 00:51:27 +03:00
parent 23f74b3bdf
commit 6c9af942f4

View File

@ -82,7 +82,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
}; };
await SetAuthTokenDataToCache(authToken, cancellation); await SetAuthTokenDataToCache(authToken, cancellation);
cookieOptions.SetCookie(context, CookieNames.AccessToken, token, expireIn); cookieOptions.SetCookie(context, CookieNames.AccessToken, authToken.AccessToken, expireIn);
cookieOptions.SetCookie(context, CookieNames.RefreshToken, authToken.RefreshToken, DateTime.UtcNow.Add(Lifetime)); cookieOptions.SetCookie(context, CookieNames.RefreshToken, authToken.RefreshToken, DateTime.UtcNow.Add(Lifetime));
logger.LogInformation( logger.LogInformation(
@ -100,9 +100,9 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
var firstTokenAuth = await cache.GetAsync<FirstAuthToken?>(GetFirstAuthCacheKey(requestContext.Fingerprint), cancellationToken: cancellation) var firstTokenAuth = await cache.GetAsync<FirstAuthToken?>(GetFirstAuthCacheKey(requestContext.Fingerprint), cancellationToken: cancellation)
?? throw new SecurityException("The session time has expired"); ?? throw new SecurityException("The session time has expired");
switch (firstTokenAuth.SecondFactor) switch (firstTokenAuth.TwoFactorAuthenticator)
{ {
case SecondFactor.Totp: case TwoFactorAuthenticator.Totp:
{ {
if (string.IsNullOrEmpty(firstTokenAuth.Secret)) if (string.IsNullOrEmpty(firstTokenAuth.Secret))
throw new InvalidOperationException("The user's secrets for data processing were not transferred."); throw new InvalidOperationException("The user's secrets for data processing were not transferred.");
@ -127,7 +127,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
await VerifyUserOrThrowError(requestContext, user, password, cancellation); await VerifyUserOrThrowError(requestContext, user, password, cancellation);
if (user.SecondFactor == SecondFactor.None) if (user.TwoFactorAuthenticator == TwoFactorAuthenticator.None)
{ {
await GenerateAuthTokensAsync(cookieOptions, context, requestContext, user.Id.ToString(), cancellation); await GenerateAuthTokensAsync(cookieOptions, context, requestContext, user.Id.ToString(), cancellation);
return true; return true;
@ -137,7 +137,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
{ {
UserId = user.Id.ToString(), UserId = user.Id.ToString(),
Secret = user.SecondFactorToken, Secret = user.SecondFactorToken,
SecondFactor = user.SecondFactor TwoFactorAuthenticator = user.TwoFactorAuthenticator
}; };
await cache.SetAsync(GetFirstAuthCacheKey(requestContext.Fingerprint), firstAuthToken, absoluteExpirationRelativeToNow: LifetimeFirstAuth, cancellationToken: cancellation); await cache.SetAsync(GetFirstAuthCacheKey(requestContext.Fingerprint), firstAuthToken, absoluteExpirationRelativeToNow: LifetimeFirstAuth, cancellationToken: cancellation);
@ -181,7 +181,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
authToken.RefreshToken = newRefreshToken; authToken.RefreshToken = newRefreshToken;
await SetAuthTokenDataToCache(authToken, cancellation); await SetAuthTokenDataToCache(authToken, cancellation);
cookieOptions.SetCookie(context, CookieNames.AccessToken, token, expireIn); cookieOptions.SetCookie(context, CookieNames.AccessToken, authToken.AccessToken, expireIn);
cookieOptions.SetCookie(context, CookieNames.RefreshToken, authToken.RefreshToken, DateTime.UtcNow.Add(Lifetime)); cookieOptions.SetCookie(context, CookieNames.RefreshToken, authToken.RefreshToken, DateTime.UtcNow.Add(Lifetime));
} }