GET /api/v1/audit?action=login,login_failed lists sign-ins with failed attempts (AuditRepository.ListByActionsAsync). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
70 lines
3.4 KiB
C#
70 lines
3.4 KiB
C#
using Titano.Core.Model;
|
|
|
|
namespace Titano.Data.Repositories;
|
|
|
|
/// <summary>Append-only record of who did what (BRIEF §5 AuditLog, §14.2).</summary>
|
|
public sealed class AuditRepository
|
|
{
|
|
private const string Columns = "id, at, user_id, username, action, target_kind, target_id, details, remote_address";
|
|
|
|
private readonly DbAccess _db;
|
|
|
|
internal AuditRepository(DbAccess db) => _db = db;
|
|
|
|
public async Task AppendAsync(AuditEntry entry, CancellationToken cancellationToken = default) =>
|
|
await _db.ExecuteAsync($"INSERT INTO audit_log ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
|
[entry.Id, entry.At, entry.UserId, entry.Username, entry.Action, entry.TargetKind, entry.TargetId, entry.Details, entry.RemoteAddress],
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
/// <summary>Entries newest first, optionally of one user.</summary>
|
|
public Task<Page<AuditEntry>> ListAsync(Guid? userId = null, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) =>
|
|
ListAsync(userId, action: null, limit, cursor, cancellationToken);
|
|
|
|
/// <summary>Entries newest first, optionally of one user and one action.</summary>
|
|
public Task<Page<AuditEntry>> ListAsync(Guid? userId, string? action, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) =>
|
|
ListByActionsAsync(userId, action is null ? [] : [action], limit, cursor, cancellationToken);
|
|
|
|
/// <summary>Entries newest first, optionally of one user, of any of the actions (none: all), e.g. the sign-in log with failed attempts.</summary>
|
|
public Task<Page<AuditEntry>> ListByActionsAsync(Guid? userId, IReadOnlyCollection<string> actions, int limit = 100, string? cursor = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(actions);
|
|
var p = new SqlParameters();
|
|
var where = new List<string>();
|
|
if (userId is { } user) where.Add($"user_id = {p.Add(user)}");
|
|
if (actions.Count > 0) where.Add($"action IN ({string.Join(", ", actions.Select(a => p.Add(a)))})");
|
|
if (cursor is not null)
|
|
{
|
|
var (value, id) = Cursor.Decode("audit", cursor);
|
|
where.Add(Cursor.After("at", "id", descending: true, value, id, p.Add));
|
|
}
|
|
var take = Cursor.ClampLimit(limit);
|
|
var sql = $"SELECT {Columns} FROM audit_log{(where.Count > 0 ? " WHERE " + string.Join(" AND ", where) : string.Empty)} " +
|
|
$"ORDER BY at DESC, id DESC LIMIT {p.Add(take + 1)}";
|
|
return _db.RunAsync(async session =>
|
|
{
|
|
var entries = await session.QueryListAsync(sql, p.ToArray(), Map, cancellationToken).ConfigureAwait(false);
|
|
string? next = null;
|
|
if (entries.Count > take)
|
|
{
|
|
entries.RemoveAt(take);
|
|
next = Cursor.Encode("audit", entries[^1].At, entries[^1].Id);
|
|
}
|
|
return new Page<AuditEntry>(entries, next);
|
|
}, cancellationToken);
|
|
}
|
|
|
|
private static AuditEntry Map(DbRow r) => new()
|
|
{
|
|
Id = r.GetGuid(0),
|
|
At = r.GetDateTimeOffset(1),
|
|
UserId = r.GetGuidOrNull(2),
|
|
Username = r.GetStringOrNull(3),
|
|
Action = r.GetString(4),
|
|
TargetKind = r.GetStringOrNull(5),
|
|
TargetId = r.GetStringOrNull(6),
|
|
Details = r.GetStringOrNull(7),
|
|
RemoteAddress = r.GetStringOrNull(8),
|
|
};
|
|
}
|