feat: system and security - start-up, accounts, sign-in, licences, log, jobs, companies (M2)
CI / Build and test (SQLite) (pull_request) Successful in 55s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Failing after 2m4s

The instance now starts on its own from KHAEMHAT_* variables: it migrates the system
and company databases, creates the super user and the first company, and only then
reports ready. Users sign in with password and TOTP through revocable database
sessions, and licences decide which modules and how many companies are available.

- Configuration: every wrong variable reported at once (exit code 2), _FILE variants
- Accounts, password policy and lockout, TOTP with recovery codes, session states
  with token rotation, security audit with a hash chain
- Sign-in pages (mockups 001-003), authorization attributes, security headers and a
  CSP without inline scripts, login rate limit
- Signed licences, activation codes, test mode, licence tool
- DataProtection keys in the system database, encrypted with the instance key
- Memory or Redis cache (in-house RESP client) that falls back to memory
- Database log by section with trace id; cron scheduler with locks and system jobs;
  record history (audit_log)
- Company creation, states, company context and X-Company, 503 in maintenance
- In-memory SQLite databases stay alive for the process (tests, demo)
- Tests for all of the above, ADR 0019, wiki, SECURITY, CHANGELOG, STATUS, DATA-MODEL

Refs #15 #16 #17 #18 #19 #20 #21 #22 #23 #24

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 15:29:24 +01:00
1 parent 4582837b70
commit 032c602b49
107 files changed
+12202 -116

No files matched your search

@@ -0,0 +1,85 @@
using Khaemhat.Core.Security;
namespace Khaemhat.Application.Security;
/// <summary>
/// Who performs an action: an instance account (super user or administrator) or a company user. Services check the
/// actor; the interface only hides what the actor cannot do.
/// </summary>
/// <param name="UserName">The user name, written in the audit columns and logs.</param>
/// <param name="Level">The level.</param>
/// <param name="SystemUserId">The instance account, for super user and administrators.</param>
/// <param name="CompanyUserId">The company user (or the user an administrator is linked to).</param>
/// <param name="CompanyCode">The company the actor works in.</param>
public sealed record Actor(string UserName, UserLevel Level, long? SystemUserId, long? CompanyUserId, string? CompanyCode)
{
/// <summary>The instance itself: bootstrap, scheduled jobs.</summary>
public static Actor System { get; } = new("system", UserLevel.SuperUser, null, null, null);
/// <summary>Whether the actor is an instance account.</summary>
public bool IsInstanceAccount => Level >= UserLevel.Administrator;
/// <summary>Whether the actor is the super user.</summary>
public bool IsSuperUser => Level == UserLevel.SuperUser;
}
/// <summary>An action the actor may not perform. <see cref="Code"/> is one of <see cref="SecurityErrors"/>.</summary>
public sealed class AccessDeniedException : Exception
{
/// <summary>Creates the exception.</summary>
public AccessDeniedException()
: this(SecurityErrors.PermissionDenied, "Access denied.")
{
}
/// <summary>Creates the exception.</summary>
/// <param name="message">The message.</param>
public AccessDeniedException(string message)
: this(SecurityErrors.PermissionDenied, message)
{
}
/// <summary>Creates the exception.</summary>
/// <param name="message">The message.</param>
/// <param name="innerException">The cause.</param>
public AccessDeniedException(string message, Exception innerException)
: base(message, innerException) => Code = SecurityErrors.PermissionDenied;
/// <summary>Creates the exception.</summary>
/// <param name="code">The error code.</param>
/// <param name="message">The message.</param>
public AccessDeniedException(string code, string message)
: base(message) => Code = code;
/// <summary>The error code.</summary>
public string Code { get; }
}
/// <summary>A request the rules refuse (policy, duplicates); <see cref="Errors"/> holds error codes for the interface.</summary>
public sealed class ValidationFailedException : Exception
{
/// <summary>Creates the exception.</summary>
public ValidationFailedException()
: this([])
{
}
/// <summary>Creates the exception.</summary>
/// <param name="message">The message.</param>
public ValidationFailedException(string message)
: base(message) => Errors = [message];
/// <summary>Creates the exception.</summary>
/// <param name="message">The message.</param>
/// <param name="innerException">The cause.</param>
public ValidationFailedException(string message, Exception innerException)
: base(message, innerException) => Errors = [message];
/// <summary>Creates the exception.</summary>
/// <param name="errors">The error codes.</param>
public ValidationFailedException(IReadOnlyList<string> errors)
: base("The request is not valid: " + string.Join(", ", errors ?? [])) => Errors = errors ?? [];
/// <summary>The error codes.</summary>
public IReadOnlyList<string> Errors { get; }
}