fix(security): validate the API-key pepper at start, finish JWT bearer, circuit-aware audit actor
- ApiKeyOptionsValidator (ValidateOnStart on any driver/admin node): a blank or placeholder Security:ApiKeys:Pepper fails host start instead of rejecting every key after a green boot (R03:2.3). - JWT is finished and opt-in by key (R03:2.4): with Security:Jwt:SigningKey set, a Bearer scheme (JwtBearerAuthenticationHandler over JwtTokenService.TryValidate / BuildValidationParameters, fail closed) accepts the tokens /auth/token mints; the default scheme is a cookie-or-bearer policy scheme and the fallback policy names it, so /api/* and every RequireAuthorization endpoint take a browser session OR a machine token. JwtOptions are validated at start (JwtOptionsValidator). Without a key, /auth/token is not mapped (404, not the former constructor 500) and no bearer scheme exists. A direct AddJwtBearer needs Microsoft.AspNetCore.Authentication.JwtBearer in Directory.Packages.props (not this lane's file) — the handler is a drop-in to swap. - AuthenticationStateAuditActorAccessor is the registered IAuditActorAccessor: the Blazor circuit's principal first, HttpContext as the fallback (R03:2.5). HttpAuditActorAccessor shares its resolver. - CookieAuthenticationStateProvider (unregistered, fire-and-forget ping loop) is deleted (R03:1.5/2.5). - /auth/login logs the LDAP exception at Warning before answering 503 (R03:2.7). - Admin operations reply with a fixed message + correlation id instead of ex.Message (R03:4.1). Refs #733 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -15,4 +15,26 @@ public sealed class JwtOptions
|
||||
|
||||
/// <summary>Default token expiry. Mirrors ScadaLink (15 min).</summary>
|
||||
public int ExpiryMinutes { get; set; } = 15;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the JWT feature is on: a non-blank <see cref="SigningKey"/>. When off, <c>/auth/token</c>
|
||||
/// is not mapped and no bearer scheme is registered, so an admin node that never configured a key
|
||||
/// keeps booting exactly as before; when on, the key is validated at host start
|
||||
/// (<c>ValidateOnStart</c>) instead of throwing HTTP 500 on the first token request.
|
||||
/// </summary>
|
||||
public bool IsConfigured => !string.IsNullOrWhiteSpace(SigningKey);
|
||||
|
||||
/// <summary>The validation rule shared by the startup validator and the tests: why the options are invalid, or null.</summary>
|
||||
/// <returns>A failure message, or <see langword="null"/> when valid.</returns>
|
||||
public string? Validate()
|
||||
{
|
||||
if (!IsConfigured) return null;
|
||||
var keyBytes = System.Text.Encoding.UTF8.GetByteCount(SigningKey);
|
||||
if (keyBytes < MinSigningKeyBytes)
|
||||
return $"{SectionName}:SigningKey must be at least {MinSigningKeyBytes} bytes (256 bits) UTF-8 for HMAC-SHA256; the configured key is {keyBytes} byte(s).";
|
||||
if (string.IsNullOrWhiteSpace(Issuer)) return $"{SectionName}:Issuer must not be blank.";
|
||||
if (string.IsNullOrWhiteSpace(Audience)) return $"{SectionName}:Audience must not be blank.";
|
||||
if (ExpiryMinutes <= 0) return $"{SectionName}:ExpiryMinutes must be > 0.";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Security.Jwt;
|
||||
|
||||
/// <summary>
|
||||
/// Startup validator for <see cref="JwtOptions"/> (R03:2.4): when a signing key is configured it must
|
||||
/// be usable, and the host must learn that at start (<c>ValidateOnStart</c>) rather than from an HTTP 500
|
||||
/// on the first <c>/auth/token</c>. An unconfigured section (no key) is valid — the feature is simply off.
|
||||
/// </summary>
|
||||
public sealed class JwtOptionsValidator : IValidateOptions<JwtOptions>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public ValidateOptionsResult Validate(string? name, JwtOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
var failure = options.Validate();
|
||||
return failure is null ? ValidateOptionsResult.Success : ValidateOptionsResult.Fail(failure);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user