diff --git a/Security/Services/AuthService.cs b/Security/Services/AuthService.cs index 732c031..1c7b80e 100644 --- a/Security/Services/AuthService.cs +++ b/Security/Services/AuthService.cs @@ -191,9 +191,10 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I 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 authToken = await cache.GetAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellation) - ?? throw new SecurityException("The session time has expired"); + var authToken = await cache.GetAsync(GetAuthCacheKey(requestContext.Fingerprint), cancellation) ?? + throw new SecurityException(defaultMessageError); if (authToken.RefreshToken != requestContext.RefreshToken || 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.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.Fingerprint, - authToken.RefreshToken != requestContext.RefreshToken ? - $"Cached refresh token '{authToken.RefreshToken}' does not match the provided refresh token '{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}'"); + authToken.RefreshToken, + requestContext.RefreshToken); + 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);