Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m28s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m42s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Implements the ratified #295 design (PR1, server-only, backward compatible). The /api surface now accepts a valid X-Api-Key (machine) OR an authenticated session (browser cookie, local login or OIDC), gated by the evolved ApiAuthorizationFilter (renamed from ApiKeyAuthorizationFilter; same fail-closed EndpointRequiresKey predicate). Machine/key behavior is byte-identical and the SPA keeps working via its stored key — the SPA login flow lands in PR2. - ApiAuthorizationFilter: key-first (CSRF-immune) then session; session-authed mutations require the X-CSRF header (403 otherwise). Attributes renamed [RequiresApiKey]->[RequiresAuthentication], [SkipApiKeyAuthorization]->[SkipApiAuthorization]. - Cookie scheme ctv-session always registered (Lax/SameAsRequest/14d sliding, 401 not redirect for /api); OIDC handler revived when configured (profile scope, userinfo, auth-method claim); UseAuthentication/UseAuthorization/UseRateLimiter revived in the legacy MapWhen branch. - Local admin = single credential in ConfigElement rows (username / PBKDF2 hash via Microsoft.Extensions.Identity.Core / rotating security stamp) — NO DB migration. Password change rotates the stamp; CookieSecurityStampValidator revokes stale local sessions. Env-seed recovery (Auth:LocalAdmin:*) via LocalAdminSeedService. - AuthController /api/auth/{config,session,setup,login,logout,password} + browser-nav GET /auth/oidc/login; excluded from OpenAPI (machine-audience spec). Per-IP rate limit on login/setup/password; dummy-hash verify (no user enumeration). - ForwardedHeaders now strict opt-in: X-Forwarded-* ignored unless KnownProxies/Networks configured (rate-limiter IP + cookie-Secure integrity). Deployment: operators behind a proxy must set ForwardedHeaders:KnownProxies. - Tests: session/CSRF filter cases + 17 Application/Auth handler tests; full ErsatzTV.Tests green (1499). No OpenAPI/generated-artifact drift. - Docs: api-conventions section 9 rewritten; decisions.md entry (supersedes #206 inert-OIDC note). Refs #295 #197 #206 #58 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
69 lines
2.7 KiB
C#
69 lines
2.7 KiB
C#
using System.Linq.Expressions;
|
|
using ErsatzTV.Application.Logs;
|
|
using ErsatzTV.Core.Api.Logs;
|
|
using ErsatzTV.Filters;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace ErsatzTV.Controllers.Api;
|
|
|
|
[ApiController]
|
|
[RequiresAuthentication]
|
|
public class LogsController(IMediator mediator) : ControllerBase
|
|
{
|
|
private const int MaxPageSize = 100;
|
|
|
|
// Mirrors the sortable columns from the legacy Blazor Logs.razor (MudTableSortLabel on
|
|
// Timestamp/Level; Message was never sortable there either).
|
|
private static readonly System.Collections.Generic.HashSet<string> AllowedSortFields =
|
|
new(StringComparer.OrdinalIgnoreCase) { "timestamp", "level" };
|
|
|
|
[HttpGet("/api/logs", Name = "GetLogs")]
|
|
[Tags("Logs")]
|
|
[EndpointSummary("Get recent log entries")]
|
|
[EndpointDescription(
|
|
"sortField is validated against an allow-list (timestamp, level); an unrecognized value " +
|
|
"falls back to timestamp. sortDirection accepts asc/desc and falls back to desc (the " +
|
|
"pre-existing default, newest first).")]
|
|
[EndpointGroupName("general")]
|
|
[ProducesResponseType(typeof(PagedLogEntriesResponseModel), StatusCodes.Status200OK)]
|
|
public async Task<PagedLogEntriesResponseModel> GetLogs(
|
|
[FromQuery] int pageNum = 0,
|
|
[FromQuery] int pageSize = 100,
|
|
[FromQuery] string filter = "",
|
|
[FromQuery] string sortField = "timestamp",
|
|
[FromQuery] string sortDirection = "desc",
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
int clampedPageNum = Math.Max(0, pageNum);
|
|
int clampedPageSize = Math.Clamp(pageSize, 1, MaxPageSize);
|
|
|
|
string normalizedSortField = AllowedSortFields.Contains(sortField ?? string.Empty)
|
|
? sortField!.ToLowerInvariant()
|
|
: "timestamp";
|
|
bool descending = !string.Equals(sortDirection, "asc", StringComparison.OrdinalIgnoreCase);
|
|
|
|
Expression<Func<LogEntryViewModel, object>> sortExpression = normalizedSortField switch
|
|
{
|
|
"level" => le => le.Level,
|
|
_ => le => le.Timestamp
|
|
};
|
|
|
|
PagedLogEntriesViewModel result = await mediator.Send(
|
|
new GetRecentLogEntries(clampedPageNum, clampedPageSize, filter ?? string.Empty)
|
|
{
|
|
SortExpression = sortExpression,
|
|
SortDescending = descending
|
|
},
|
|
cancellationToken);
|
|
|
|
return new PagedLogEntriesResponseModel(
|
|
result.TotalCount,
|
|
result.Page.Map(ProjectToResponseModel).ToList());
|
|
}
|
|
|
|
private static LogEntryResponseModel ProjectToResponseModel(LogEntryViewModel viewModel) =>
|
|
new(viewModel.Timestamp, viewModel.Level.ToString(), viewModel.Message);
|
|
}
|