Voeg Api.AndreaSlopFotografie toe met setup-token, proxy-bewuste limiters en ASF-admin-thema
- Core: opt-in ForwardedHeaders:KnownProxies en rate limiters per clientadres (zonder configuratie ongewijzigd gedrag) - Identity: optioneel setup-token (X-Setup-Token), beide setup-endpoints onder de login-limiter, beveiligingsgebeurtenis setup_token_rejected (5007) - Nieuw client-project SlpModularCms.Api.AndreaSlopFotografie (Identity + Availability) met bijbehorende tests - Frontend: thema asf (VITE_THEME), VITE_DEFAULT_LANGUAGE, tokenveld op de setup-pagina - CI: jobs publish/deploy-asf-test achter ASF_TEST_DEPLOY_ENABLED - Fix U1: 301-Location wordt nu percent-encoded (gevonden door de property-test) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
using SlpModularCms.Core.Identity.Services;
|
||||
using SlpModularCms.Core.Observability;
|
||||
using SlpModularCms.Modules.Identity.Setup;
|
||||
|
||||
namespace SlpModularCms.Modules.Identity.Controllers;
|
||||
|
||||
@@ -8,23 +13,42 @@ namespace SlpModularCms.Modules.Identity.Controllers;
|
||||
[Route("[controller]")]
|
||||
public class SetupController : ControllerBase
|
||||
{
|
||||
private readonly ISetupService _setupService;
|
||||
public const string TokenHeader = "X-Setup-Token";
|
||||
|
||||
public SetupController(ISetupService setupService)
|
||||
private readonly ISetupService _setupService;
|
||||
private readonly ISetupTokenValidator _tokenValidator;
|
||||
private readonly ILogger<SetupController> _logger;
|
||||
|
||||
public SetupController(
|
||||
ISetupService setupService,
|
||||
ISetupTokenValidator tokenValidator,
|
||||
ILogger<SetupController> logger)
|
||||
{
|
||||
_setupService = setupService;
|
||||
_tokenValidator = tokenValidator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("status")]
|
||||
[EnableRateLimiting("login")]
|
||||
public async Task<IActionResult> GetStatus()
|
||||
{
|
||||
var initialized = await _setupService.IsSystemInitializedAsync();
|
||||
return Ok(new { Initialized = initialized });
|
||||
return Ok(new { Initialized = initialized, TokenRequired = _tokenValidator.TokenRequired });
|
||||
}
|
||||
|
||||
[HttpPost("owner")]
|
||||
[EnableRateLimiting("login")]
|
||||
public async Task<IActionResult> CreateOwner([FromBody] CreateOwnerRequest request)
|
||||
{
|
||||
// Missing and wrong tokens give the identical response, so the endpoint reveals nothing about
|
||||
// whether a token exists or how close a guess was. The token itself is never logged.
|
||||
if (!_tokenValidator.IsAuthorized(Request.Headers[TokenHeader].ToString()))
|
||||
{
|
||||
SecurityEvents.SetupTokenRejected(_logger, Request.Path);
|
||||
return Problem(title: "Setup token ontbreekt of is onjuist.", statusCode: StatusCodes.Status401Unauthorized);
|
||||
}
|
||||
|
||||
await _setupService.CreateInitialOwnerAsync(request.Name, request.Email, request.Password);
|
||||
return Ok(new { Message = "Systeem succesvol geïnitialiseerd. De eerste eigenaar is aangemaakt." });
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Modules;
|
||||
using SlpModularCms.Modules.Identity.Setup;
|
||||
|
||||
namespace SlpModularCms.Modules.Identity;
|
||||
|
||||
@@ -13,7 +15,11 @@ public class IdentityModule : IModule
|
||||
|
||||
public void RegisterServices(IServiceCollection services)
|
||||
{
|
||||
// Specifieke services voor deze module (indien niet al in Core)
|
||||
services.AddOptions<SetupOptions>()
|
||||
.BindConfiguration(SetupOptions.SectionName)
|
||||
.Validate(SetupOptions.IsValid, "Identity:Setup:Token must not consist only of whitespace.")
|
||||
.ValidateOnStart();
|
||||
services.AddSingleton<ISetupTokenValidator, SetupTokenValidator>();
|
||||
}
|
||||
|
||||
public void UseModule(IApplicationBuilder app)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace SlpModularCms.Modules.Identity.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Optional protection of the first-administrator setup (section <c>Identity:Setup</c>).
|
||||
/// With no token configured the setup endpoints behave exactly as before.
|
||||
/// </summary>
|
||||
public sealed class SetupOptions
|
||||
{
|
||||
public const string SectionName = "Identity:Setup";
|
||||
|
||||
/// <summary>The secret the person creating the first administrator must present. Empty = not required.</summary>
|
||||
public string? Token { get; set; }
|
||||
|
||||
/// <summary>True when a token is configured and therefore required.</summary>
|
||||
public bool TokenRequired => !string.IsNullOrEmpty(Token);
|
||||
|
||||
/// <summary>
|
||||
/// A token that is set but consists only of whitespace is a configuration mistake (it would look
|
||||
/// protected but could be guessed trivially), so it is rejected at startup. Null or empty means
|
||||
/// "no token".
|
||||
/// </summary>
|
||||
public static bool IsValid(SetupOptions options) =>
|
||||
string.IsNullOrEmpty(options.Token) || !string.IsNullOrWhiteSpace(options.Token);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace SlpModularCms.Modules.Identity.Setup;
|
||||
|
||||
/// <summary>Decides whether a request may create the first administrator.</summary>
|
||||
public interface ISetupTokenValidator
|
||||
{
|
||||
/// <summary>True when a setup token is configured.</summary>
|
||||
bool TokenRequired { get; }
|
||||
|
||||
/// <summary>
|
||||
/// True when no token is required, or when <paramref name="provided"/> equals the configured one.
|
||||
/// Never throws for any input.
|
||||
/// </summary>
|
||||
bool IsAuthorized(string? provided);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares SHA-256 digests with a fixed-time comparison, so neither the length nor the content of
|
||||
/// the configured token leaks through response timing.
|
||||
/// </summary>
|
||||
public sealed class SetupTokenValidator : ISetupTokenValidator
|
||||
{
|
||||
private readonly byte[]? _expectedDigest;
|
||||
|
||||
public SetupTokenValidator(IOptions<SetupOptions> options)
|
||||
{
|
||||
var token = options.Value.Token;
|
||||
_expectedDigest = string.IsNullOrEmpty(token) ? null : Hash(token);
|
||||
}
|
||||
|
||||
public bool TokenRequired => _expectedDigest is not null;
|
||||
|
||||
public bool IsAuthorized(string? provided)
|
||||
{
|
||||
if (_expectedDigest is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(provided))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return CryptographicOperations.FixedTimeEquals(_expectedDigest, Hash(provided));
|
||||
}
|
||||
|
||||
private static byte[] Hash(string value) => SHA256.HashData(Encoding.UTF8.GetBytes(value));
|
||||
}
|
||||
Reference in New Issue
Block a user