- Security: PBKDF2-SHA512 (210,000 iterations) hashes, opaque bearer tokens stored as SHA-256 with sliding expiry and revocation, HttpOnly/Secure/ SameSite=Strict admin cookie with a session-bound CSRF token, sign-in rate limit and lockout, role/permission metadata on every endpoint, audit log, first-run administrator bootstrap. - Sequences with keyset paging, ETags and settings_version conflicts; sync manifest; frames, proxies, thumbnails and exports streamed with Range. - Chunked resumable uploads with hash short-circuit, quota and disk checks, staging on the archive volume, SHA-256 verification and IArchiveIngest. - Job engine host, jobs API, render nodes with heartbeat timeout and reassignment, WebSocket events, LAN discovery beacon, upload cleanup. - Windows service host over advapi32 (--service, --install-service, --uninstall-service); fails fast without the archive module services. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
93 lines
3.7 KiB
C#
93 lines
3.7 KiB
C#
using Titano.Server.Configuration;
|
|
|
|
namespace Titano.Server.Security;
|
|
|
|
/// <summary>
|
|
/// Sign-in throttling (BRIEF §14.2): at most <see cref="ServerOptions.LoginAttemptsPerMinute"/> attempts per client
|
|
/// address and minute, and an account name locked for <see cref="ServerOptions.LockoutMinutes"/> after
|
|
/// <see cref="ServerOptions.LockoutThreshold"/> consecutive failures. Names without an account are tracked the same
|
|
/// way, so a lockout reveals nothing about which names exist. State is per process and resets at restart.
|
|
/// </summary>
|
|
public sealed class LoginGuard(ServerOptions options, TimeProvider time)
|
|
{
|
|
private static readonly TimeSpan Window = TimeSpan.FromMinutes(1);
|
|
private const int PruneAbove = 10_000;
|
|
|
|
private readonly Lock _gate = new();
|
|
private readonly Dictionary<string, Queue<DateTimeOffset>> _attempts = new(StringComparer.Ordinal);
|
|
private readonly Dictionary<string, AccountState> _accounts = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>Counts an attempt from <paramref name="address"/>; false, with the time to wait, once its budget is spent.</summary>
|
|
public bool TryAttempt(string address, out TimeSpan retryAfter)
|
|
{
|
|
var now = time.GetUtcNow();
|
|
lock (_gate)
|
|
{
|
|
if (_attempts.Count > PruneAbove) Prune(now);
|
|
if (!_attempts.TryGetValue(address, out var recent)) _attempts[address] = recent = new Queue<DateTimeOffset>();
|
|
while (recent.Count > 0 && now - recent.Peek() >= Window) recent.Dequeue();
|
|
if (recent.Count >= options.LoginAttemptsPerMinute)
|
|
{
|
|
retryAfter = Window - (now - recent.Peek());
|
|
return false;
|
|
}
|
|
recent.Enqueue(now);
|
|
retryAfter = TimeSpan.Zero;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>True while <paramref name="account"/> (a normalised user name) is locked.</summary>
|
|
public bool IsLocked(string account, out TimeSpan retryAfter)
|
|
{
|
|
var now = time.GetUtcNow();
|
|
lock (_gate)
|
|
{
|
|
if (_accounts.TryGetValue(account, out var state) && state.LockedUntil is { } until && until > now)
|
|
{
|
|
retryAfter = until - now;
|
|
return true;
|
|
}
|
|
retryAfter = TimeSpan.Zero;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void RecordFailure(string account)
|
|
{
|
|
var now = time.GetUtcNow();
|
|
var span = TimeSpan.FromMinutes(options.LockoutMinutes);
|
|
lock (_gate)
|
|
{
|
|
if (_accounts.Count > PruneAbove) Prune(now);
|
|
var state = _accounts.GetValueOrDefault(account);
|
|
if (state?.LockedUntil is { } until && until > now) return;
|
|
if (state is null || state.LockedUntil is not null || now - state.FirstFailure > span) state = new AccountState(0, now, null);
|
|
var failures = state.Failures + 1;
|
|
_accounts[account] = failures >= options.LockoutThreshold
|
|
? new AccountState(0, now, now + span)
|
|
: state with { Failures = failures };
|
|
}
|
|
}
|
|
|
|
public void RecordSuccess(string account)
|
|
{
|
|
lock (_gate) _accounts.Remove(account);
|
|
}
|
|
|
|
private void Prune(DateTimeOffset now)
|
|
{
|
|
foreach (var (address, recent) in _attempts.ToList())
|
|
{
|
|
if (recent.Count == 0 || now - recent.Last() >= Window) _attempts.Remove(address);
|
|
}
|
|
var span = TimeSpan.FromMinutes(options.LockoutMinutes);
|
|
foreach (var (account, state) in _accounts.ToList())
|
|
{
|
|
if ((state.LockedUntil ?? state.FirstFailure + span) < now) _accounts.Remove(account);
|
|
}
|
|
}
|
|
|
|
private sealed record AccountState(int Failures, DateTimeOffset FirstFailure, DateTimeOffset? LockedUntil);
|
|
}
|