diff --git a/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs b/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs index ad013e7..0df8831 100644 --- a/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs +++ b/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs @@ -65,7 +65,10 @@ public static class LoggerConfiguration return app.Use(async (context, next) => { var traceId = Activity.Current?.Id ?? context.TraceIdentifier; + using (LogContext.PushProperty("TraceId", traceId)) + using (LogContext.PushProperty("UserAgent", context.Request.Headers.UserAgent.ToString())) + using (LogContext.PushProperty("RemoteIPAddress", context.Connection.RemoteIpAddress?.ToString())) { await next(); } diff --git a/Security/Services/AuthService.cs b/Security/Services/AuthService.cs index 33c5d27..a505c09 100644 --- a/Security/Services/AuthService.cs +++ b/Security/Services/AuthService.cs @@ -63,19 +63,16 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I if (countFailedLogin > 5) { logger.LogWarning( - "Multiple unsuccessful login attempts for user ID {UserId} from IP {UserIp}. Attempt count: {AttemptNumber}.", + "Multiple unsuccessful login attempts for user ID {UserId}. Attempt count: {AttemptNumber}.", user.Id, - requestContext.Ip, countFailedLogin); throw new SecurityException("Too many unsuccessful login attempts. Please try again later."); } logger.LogInformation( - "Login attempt failed for user ID {UserId}. IP: {UserIp}, User-Agent: {UserAgent}, Fingerprint: {Fingerprint}. Attempt count: {AttemptNumber}.", + "Login attempt failed for user ID {UserId}. Fingerprint: {Fingerprint}. Attempt count: {AttemptNumber}.", user.Id, - requestContext.Ip, - requestContext.UserAgent, requestContext.Fingerprint, countFailedLogin); @@ -100,10 +97,8 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I cookieOptions.SetCookie(context, CookieNames.RefreshToken, authToken.RefreshToken, DateTime.UtcNow.Add(Lifetime)); logger.LogInformation( - "Login successful for user ID {UserId}. IP: {UserIp}, User-Agent: {UserAgent}, Fingerprint: {Fingerprint}.", + "Login successful for user ID {UserId}. Fingerprint: {Fingerprint}.", authToken.UserId, - authToken.Ip, - authToken.UserAgent, authToken.Fingerprint); } @@ -184,10 +179,8 @@ 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}. IP: {UserIp}, User-Agent: {UserAgent}, Fingerprint: {Fingerprint}. Reason: {Reason}.", + logger.LogWarning("Token validation failed for user ID {UserId}. Fingerprint: {Fingerprint}. Reason: {Reason}.", authToken.UserId, - authToken.Ip, - authToken.UserAgent, authToken.Fingerprint, authToken.RefreshToken != requestContext.RefreshToken ? $"Cached refresh token '{authToken.RefreshToken}' does not match the provided refresh token '{requestContext.RefreshToken}'" : diff --git a/Security/Services/OAuthService.cs b/Security/Services/OAuthService.cs index 0f99548..d10f568 100644 --- a/Security/Services/OAuthService.cs +++ b/Security/Services/OAuthService.cs @@ -110,21 +110,25 @@ public class OAuthService(ILogger logger, Dictionary (x.Key, new Uri(redirectUri.TrimEnd('/') + "/?provider=" + (int)x.Key)))]; - } + public (OAuthProvider Provider, Uri Redirect)[] GetAvailableProviders(string redirectUri) => + [.. providers.Select(x => (x.Key, new Uri(redirectUri.TrimEnd('/') + "/?provider=" + (int)x.Key)))]; public async Task<(OAuthProvider provider, OAuthUser User)> LoginOAuth(HttpContext context, CookieOptionsParameters cookieOptions, string redirectUrl, string code, string state, CancellationToken cancellation = default) { @@ -139,11 +143,17 @@ public class OAuthService(ILogger logger, Dictionary logger, Dictionary