refactor: instead of Reason, add explicit arguments

This commit is contained in:
Polianin Nikita 2024-12-25 05:51:54 +03:00
parent dfac9ddca8
commit 5e65aded79

View File

@ -191,9 +191,10 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
public async Task RefreshTokenAsync(CookieOptionsParameters cookieOptions, HttpContext context, CancellationToken cancellation = default) public async Task RefreshTokenAsync(CookieOptionsParameters cookieOptions, HttpContext context, CancellationToken cancellation = default)
{ {
const string defaultMessageError = "The session time has expired";
var requestContext = new RequestContextInfo(context, cookieOptions); var requestContext = new RequestContextInfo(context, cookieOptions);
var authToken = await cache.GetAsync<AuthToken>(GetAuthCacheKey(requestContext.Fingerprint), cancellation) var authToken = await cache.GetAsync<AuthToken>(GetAuthCacheKey(requestContext.Fingerprint), cancellation) ??
?? throw new SecurityException("The session time has expired"); throw new SecurityException(defaultMessageError);
if (authToken.RefreshToken != requestContext.RefreshToken || if (authToken.RefreshToken != requestContext.RefreshToken ||
authToken.UserAgent != requestContext.UserAgent && authToken.UserAgent != requestContext.UserAgent &&
@ -204,14 +205,29 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
cookieOptions.DropCookie(context, CookieNames.AccessToken); cookieOptions.DropCookie(context, CookieNames.AccessToken);
cookieOptions.DropCookie(context, CookieNames.RefreshToken); cookieOptions.DropCookie(context, CookieNames.RefreshToken);
logger.LogWarning("Token validation failed for user ID {UserId}. Fingerprint: {Fingerprint}. Reason: {Reason}.", const string error = "Token validation failed for user ID {UserId}. Fingerprint: {Fingerprint}. ";
if (authToken.RefreshToken != requestContext.RefreshToken)
logger.LogWarning(
error +
"Cached refresh token {ExpectedRefreshToken} does not match the provided refresh token {RefreshToken}",
authToken.UserId, authToken.UserId,
authToken.Fingerprint, authToken.Fingerprint,
authToken.RefreshToken != requestContext.RefreshToken ? authToken.RefreshToken,
$"Cached refresh token '{authToken.RefreshToken}' does not match the provided refresh token '{requestContext.RefreshToken}'" : requestContext.RefreshToken);
$"User-Agent '{authToken.UserAgent}' and IP '{authToken.Ip}' in cache do not match the provided User-Agent '{requestContext.UserAgent}' and IP '{requestContext.Ip}'"); else
logger.LogWarning(
error +
"User-Agent {ExpectedUserAgent} and IP {ExpectedUserIp} in cache do not match the provided " +
"User-Agent {ProvidedUserAgent} and IP {ProvidedIp}",
authToken.UserId,
authToken.Fingerprint,
authToken.UserAgent,
authToken.Ip,
requestContext.UserAgent,
requestContext.Ip);
throw new SecurityException(defaultMessageError);
throw new SecurityException("The session time has expired");
} }
var (token, expireIn) = GenerateAccessToken(authToken.UserId); var (token, expireIn) = GenerateAccessToken(authToken.UserId);