- 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
91 lines
3.7 KiB
C#
91 lines
3.7 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
using Microsoft.Net.Http.Headers;
|
|
using Titano.Core.Localization;
|
|
using Titano.Protocol;
|
|
using Titano.Server.Configuration;
|
|
|
|
namespace Titano.Server.Api;
|
|
|
|
/// <summary>
|
|
/// Error responses of API v1: <c>{ code, message, details }</c> (BRIEF §14.3). <c>code</c> is a stable snake_case
|
|
/// identifier for programs; <c>message</c> is one localised sentence (resource <c>Server.Error.<Code></c>, language
|
|
/// from <c>Accept-Language</c>, else the server language); <c>details</c> is optional machine-readable context.
|
|
/// </summary>
|
|
public static class ApiErrors
|
|
{
|
|
public static IResult Result(HttpContext context, int status, string code, string? details = null, params object?[] args) =>
|
|
Results.Json(new ApiError(code, Message(context, code, args), details), ProtocolJson.Default.ApiError, statusCode: status);
|
|
|
|
public static Task WriteAsync(HttpContext context, int status, string code, string? details = null) =>
|
|
Result(context, status, code, details).ExecuteAsync(context);
|
|
|
|
public static IResult NotFound(HttpContext context, string? what = null) => Result(context, StatusCodes.Status404NotFound, "not_found", what);
|
|
|
|
public static IResult BadRequest(HttpContext context, string code, string? details = null, params object?[] args) =>
|
|
Result(context, StatusCodes.Status400BadRequest, code, details, args);
|
|
|
|
public static IResult Conflict(HttpContext context, string code, string? details = null) => Result(context, StatusCodes.Status409Conflict, code, details);
|
|
|
|
public static IResult Forbidden(HttpContext context) => Result(context, StatusCodes.Status403Forbidden, "forbidden");
|
|
|
|
public static string Message(HttpContext context, string code, params object?[] args)
|
|
{
|
|
var culture = RequestCulture.Of(context);
|
|
var template = Loc.Get("Server.Error." + Pascal(code), culture) ?? code;
|
|
return args.Length == 0 ? template : string.Format(culture, template, args);
|
|
}
|
|
|
|
private static string Pascal(string code)
|
|
{
|
|
var sb = new StringBuilder(code.Length);
|
|
var upper = true;
|
|
foreach (var c in code)
|
|
{
|
|
if (c == '_')
|
|
{
|
|
upper = true;
|
|
continue;
|
|
}
|
|
sb.Append(upper ? char.ToUpperInvariant(c) : c);
|
|
upper = false;
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>The language of a request's messages: the best shipped match of <c>Accept-Language</c>, else the server language.</summary>
|
|
public static class RequestCulture
|
|
{
|
|
public static CultureInfo Of(HttpContext context)
|
|
{
|
|
if (context.Items.TryGetValue(typeof(RequestCulture), out var cached) && cached is CultureInfo known) return known;
|
|
var fallback = context.RequestServices.GetRequiredService<ServerOptions>().Language;
|
|
var culture = FromHeader(context.Request.Headers.AcceptLanguage.ToString()) ?? Parse(fallback) ?? CultureInfo.GetCultureInfo("en");
|
|
context.Items[typeof(RequestCulture)] = culture;
|
|
return culture;
|
|
}
|
|
|
|
private static CultureInfo? FromHeader(string header)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(header) || !StringWithQualityHeaderValue.TryParseList([header], out var values)) return null;
|
|
foreach (var value in values.OrderByDescending(v => v.Quality ?? 1))
|
|
{
|
|
if (value.Value.Value is { } tag && tag != "*" && Parse(tag) is { } culture) return culture;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static CultureInfo? Parse(string tag)
|
|
{
|
|
try
|
|
{
|
|
return Languages.BestMatch(CultureInfo.GetCultureInfo(tag));
|
|
}
|
|
catch (CultureNotFoundException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|