mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-01-16 17:10:21 -05:00
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com> Co-authored-by: Weblate (bot) <hosted@weblate.org> Co-authored-by: Adam Havránek <adamhavra@seznam.cz> Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com> Co-authored-by: Dark77 <Dark77@pobox.sk> Co-authored-by: Frozehunter <frozehunter@me.com> Co-authored-by: Havokdan <havokdan@yahoo.com.br> Co-authored-by: Igor Dobrača <igor.dobraca@gmail.com> Co-authored-by: Karl B <karl.owl@proton.me> Co-authored-by: Morhain Olivier <sesram@users.noreply.hosted.weblate.org> Co-authored-by: daydreamrabbit <devrabbit90@gmail.com> Co-authored-by: karigane <169052233+karigane-cha@users.noreply.github.com> Co-authored-by: oxygen44k <iiccpp@outlook.com> Co-authored-by: Максим Горпиніч <gorpinicmaksim0@gmail.com> Co-authored-by: 無情天 <kofzhanganguo@126.com> Co-authored-by: 안세훈 <on9686@gmail.com>
69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading.Tasks;
|
|
using API.Services;
|
|
using API.Services.Reading;
|
|
using API.Services.Store;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace API.Middleware;
|
|
#nullable enable
|
|
|
|
/// <summary>
|
|
/// Middleware that identifies and tracks device activity for authenticated requests.
|
|
/// Runs after authentication middleware and ClientInfoMiddleware.
|
|
/// Can be skipped on specific endpoints using [SkipDeviceTracking] attribute.
|
|
/// </summary>
|
|
public class DeviceTrackingMiddleware(RequestDelegate next, ILogger<DeviceTrackingMiddleware> logger)
|
|
{
|
|
public async Task InvokeAsync(
|
|
HttpContext context,
|
|
IClientDeviceService clientDeviceService,
|
|
IDeviceTrackingService deviceTrackingService,
|
|
IClientInfoAccessor clientInfoAccessor,
|
|
IUserContext userContext)
|
|
{
|
|
var endpoint = context.GetEndpoint();
|
|
var skipTracking = endpoint?.Metadata.GetMetadata<SkipDeviceTrackingAttribute>() != null;
|
|
|
|
if (skipTracking || context.Request.Path.Equals("/"))
|
|
{
|
|
await next(context);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var userId = userContext.GetUserId();
|
|
var clientInfo = clientInfoAccessor.Current;
|
|
var clientUiFingerprint = clientInfoAccessor.CurrentUiFingerprint; // string from webapp header
|
|
|
|
if (userId.HasValue && clientInfo != null)
|
|
{
|
|
var deviceId = await deviceTrackingService.TrackDeviceAsync(
|
|
userId.Value,
|
|
clientInfo,
|
|
clientUiFingerprint,
|
|
context.RequestAborted);
|
|
|
|
ClientInfoAccessor.SetDeviceId(deviceId);
|
|
logger.LogTrace("Device {DeviceId} tracked for user {UserId}", deviceId, userId);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Failed to track device activity");
|
|
}
|
|
|
|
await next(context);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attribute to skip device tracking on specific endpoints.
|
|
/// Use for high-frequency endpoints where device tracking adds unnecessary overhead.
|
|
/// </summary>
|
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
|
public class SkipDeviceTrackingAttribute : Attribute;
|