Resolve all high-severity architecture review findings
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Bounded, process-local activity shared by an HTTP cookie and its existing Blazor
|
||||
/// circuit. Only authenticated, validated interaction requests advance this clock.
|
||||
/// The protected cookie remains the durable anchor on restart or node change.
|
||||
/// </summary>
|
||||
public sealed class SessionActivityTracker
|
||||
{
|
||||
/// <summary>Opaque per-login identifier carried in the protected cookie.</summary>
|
||||
public const string SessionIdClaim = "zb:sessionid";
|
||||
private const int Capacity = 10_000;
|
||||
private readonly object _sync = new();
|
||||
private readonly Dictionary<string, DateTimeOffset> _activity = new(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>Returns the newer of the protected cookie anchor and this node's observed activity.</summary>
|
||||
public DateTimeOffset? LastActivity(ClaimsPrincipal principal)
|
||||
{
|
||||
if (!DateTimeOffset.TryParse(principal.FindFirst(JwtTokenService.LastActivityClaimType)?.Value, out var stamp))
|
||||
return null;
|
||||
var id = principal.FindFirst(SessionIdClaim)?.Value;
|
||||
lock (_sync)
|
||||
return id is not null && _activity.TryGetValue(id, out var seen) && seen > stamp ? seen : stamp;
|
||||
}
|
||||
|
||||
/// <summary>Records a validated interaction without changing another login's activity.</summary>
|
||||
public void Record(ClaimsPrincipal principal, DateTimeOffset now)
|
||||
{
|
||||
var id = principal.FindFirst(SessionIdClaim)?.Value;
|
||||
if (id is null || principal.Identity?.IsAuthenticated != true) return;
|
||||
lock (_sync)
|
||||
{
|
||||
if (!_activity.ContainsKey(id) && _activity.Count >= Capacity)
|
||||
_activity.Remove(_activity.MinBy(p => p.Value).Key);
|
||||
if (!_activity.TryGetValue(id, out var previous) || now > previous)
|
||||
_activity[id] = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user