From 2ab5dea8ba13696b342d91f55a58cfaa2d921ea5 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Wed, 25 Dec 2024 05:52:39 +0300 Subject: [PATCH] feat: add a change to the User Agent and Ip address in case of a mismatch --- Security/Services/AuthService.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Security/Services/AuthService.cs b/Security/Services/AuthService.cs index 1c7b80e..e78ce75 100644 --- a/Security/Services/AuthService.cs +++ b/Security/Services/AuthService.cs @@ -227,7 +227,30 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I requestContext.Ip); throw new SecurityException(defaultMessageError); + } + if (authToken.UserAgent != requestContext.UserAgent) + { + logger.LogInformation("The resulting User-Agent {ProvidedUserAgent} does not match the cached " + + "{ExpectedUserAgent} of the user {UserId} with the fingerprint {Fingerprint}.", + requestContext.UserAgent, + authToken.UserAgent, + authToken.UserId, + requestContext.Fingerprint); + + authToken.UserAgent = requestContext.UserAgent; + } + + if (authToken.Ip != requestContext.Ip) + { + logger.LogInformation("The resulting Ip {ProvidedIp} does not match the cached " + + "{ExpectedIp} of the user {UserId} with the fingerprint {Fingerprint}.", + requestContext.Ip, + authToken.Ip, + authToken.UserId, + requestContext.Fingerprint); + + authToken.Ip = requestContext.Ip; } var (token, expireIn) = GenerateAccessToken(authToken.UserId);