using System.Security.Claims;
namespace ZB.MOM.WW.ScadaBridge.Security;
///
/// 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.
///
public sealed class SessionActivityTracker
{
/// Opaque per-login identifier carried in the protected cookie.
public const string SessionIdClaim = "zb:sessionid";
private const int Capacity = 10_000;
private readonly object _sync = new();
private readonly Dictionary _activity = new(StringComparer.Ordinal);
private readonly Dictionary _refreshes = new(StringComparer.Ordinal);
/// Returns the newer of the protected cookie anchor and this node's observed activity.
/// The principal whose session activity is queried.
/// The most recent activity time, or when the principal carries no activity claim.
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;
}
/// Records a validated interaction without changing another login's activity.
/// The principal whose session is recorded.
/// The time of the interaction.
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;
}
}
///
/// Returns the refresh anchors this node recorded for the principal's session, or
/// when none (SEC-1). They supersede the older anchors in the cookie.
///
/// The principal whose session is queried.
/// The recorded role and group refresh instants, if any.
public (DateTimeOffset Role, DateTimeOffset Group)? LastRefreshes(ClaimsPrincipal principal)
{
var id = principal.FindFirst(SessionIdClaim)?.Value;
if (id is null) return null;
lock (_sync)
return _refreshes.TryGetValue(id, out var seen) ? seen : null;
}
///
/// Records that a role/group refresh ran for the session without changing its grants. A passive
/// request (/auth/ping, /_blazor) never re-issues the cookie, so without this the old
/// anchors in the cookie made every later ping repeat the refresh (SEC-1). Only an UNCHANGED outcome
/// may be recorded: a changed one must keep being re-derived until a non-passive request persists it
/// in the cookie, otherwise the stale cookie roles would outlive the refresh.
///
/// The principal whose session is recorded.
/// The role-refresh anchor now in effect.
/// The group-refresh anchor now in effect.
public void RecordRefresh(ClaimsPrincipal principal, DateTimeOffset role, DateTimeOffset group)
{
var id = principal.FindFirst(SessionIdClaim)?.Value;
if (id is null || principal.Identity?.IsAuthenticated != true) return;
lock (_sync)
{
if (!_refreshes.ContainsKey(id) && _refreshes.Count >= Capacity)
_refreshes.Remove(_refreshes.MinBy(p => p.Value.Role).Key);
_refreshes[id] = (role, group);
}
}
}