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
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:
1 parent
4582837b70
commit
032c602b49
107 files changed
+12202
-116
No files matched your search
@@ -0,0 +1,237 @@
|
||||
using Khaemhat.Core.Entities;
|
||||
|
||||
namespace Khaemhat.Core.Security;
|
||||
|
||||
/// <summary>
|
||||
/// The three access levels of SPEC §7.1, ordered: a higher value can do everything a lower one can.
|
||||
/// <see cref="SystemUser"/> rows hold <see cref="Administrator"/> or <see cref="SuperUser"/>; company users are
|
||||
/// <see cref="Standard"/>.
|
||||
/// </summary>
|
||||
public enum UserLevel
|
||||
{
|
||||
/// <summary>A company user, limited by the permissions of the roles.</summary>
|
||||
Standard = 0,
|
||||
|
||||
/// <summary>An instance administrator, created by the super user.</summary>
|
||||
Administrator = 1,
|
||||
|
||||
/// <summary>The only super user, created from the container variables at first start.</summary>
|
||||
SuperUser = 2,
|
||||
}
|
||||
|
||||
/// <summary>The kind of a company user.</summary>
|
||||
public enum UserKind
|
||||
{
|
||||
/// <summary>A person who signs in with a password.</summary>
|
||||
Human = 0,
|
||||
|
||||
/// <summary>A technical user for the API: no password, used only through API keys.</summary>
|
||||
Technical = 1,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The credential fields shared by instance accounts (<c>sys_users</c>) and company users (<c>users</c>), so that
|
||||
/// password, lockout, TOTP and recovery rules are written once.
|
||||
/// </summary>
|
||||
public interface IAccount
|
||||
{
|
||||
/// <summary>The identifier.</summary>
|
||||
long Id { get; }
|
||||
|
||||
/// <summary>The sign-in name, unique in its database (compared case-insensitively, stored lower-case).</summary>
|
||||
string UserName { get; set; }
|
||||
|
||||
/// <summary>The name shown in the interface.</summary>
|
||||
string DisplayName { get; set; }
|
||||
|
||||
/// <summary>The e-mail address for recovery and notifications.</summary>
|
||||
string? Email { get; set; }
|
||||
|
||||
/// <summary>The password hash (<c>pbkdf2-sha256$...</c>); <see langword="null"/> for technical users.</summary>
|
||||
string? PasswordHash { get; set; }
|
||||
|
||||
/// <summary>The previous password hashes, newest first, as a JSON array (policy: no reuse of the last five).</summary>
|
||||
string? PasswordHistory { get; set; }
|
||||
|
||||
/// <summary>When the password was last set.</summary>
|
||||
DateTime? PasswordChangedAt { get; set; }
|
||||
|
||||
/// <summary>Whether the next sign-in must change the password (first access, temporary password, expiry).</summary>
|
||||
bool MustChangePassword { get; set; }
|
||||
|
||||
/// <summary>When a temporary password stops working; <see langword="null"/> for a normal password.</summary>
|
||||
DateTime? TemporaryPasswordExpiresAt { get; set; }
|
||||
|
||||
/// <summary>Consecutive failed sign-ins since the last success.</summary>
|
||||
int FailedLoginCount { get; set; }
|
||||
|
||||
/// <summary>The account is locked until this moment after too many failures.</summary>
|
||||
DateTime? LockedUntil { get; set; }
|
||||
|
||||
/// <summary>The TOTP secret, encrypted with the instance key.</summary>
|
||||
string? TotpSecret { get; set; }
|
||||
|
||||
/// <summary>Whether the two-step verification is active.</summary>
|
||||
bool TotpEnabled { get; set; }
|
||||
|
||||
/// <summary>The last accepted TOTP time step: a code is never accepted twice.</summary>
|
||||
long? TotpLastStep { get; set; }
|
||||
|
||||
/// <summary>The SHA-256 hashes of the unused recovery codes, as a JSON array.</summary>
|
||||
string? TotpRecoveryCodes { get; set; }
|
||||
|
||||
/// <summary>The SHA-256 hash of the pending password-recovery token.</summary>
|
||||
string? RecoveryTokenHash { get; set; }
|
||||
|
||||
/// <summary>When the recovery token expires.</summary>
|
||||
DateTime? RecoveryTokenExpiresAt { get; set; }
|
||||
|
||||
/// <summary>Whether the account can sign in.</summary>
|
||||
bool IsActive { get; set; }
|
||||
|
||||
/// <summary>The last successful sign-in.</summary>
|
||||
DateTime? LastLoginAt { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>An instance account (<c>sys_users</c>): the super user or an administrator (SPEC §7.1).</summary>
|
||||
public sealed class SystemUser : Entity<long>, IAccount
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? Email { get; set; }
|
||||
|
||||
/// <summary><see cref="UserLevel.SuperUser"/> or <see cref="UserLevel.Administrator"/>.</summary>
|
||||
public UserLevel Level { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? PasswordHash { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? PasswordHistory { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? PasswordChangedAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool MustChangePassword { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? TemporaryPasswordExpiresAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int FailedLoginCount { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? LockedUntil { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? TotpSecret { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TotpEnabled { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public long? TotpLastStep { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? TotpRecoveryCodes { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? RecoveryTokenHash { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? RecoveryTokenExpiresAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsActive { get; set; } = true;
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? LastLoginAt { get; set; }
|
||||
|
||||
/// <summary>The company of the linked user, for an administrator who works as a company user without a second sign-in.</summary>
|
||||
public string? LinkedCompanyCode { get; set; }
|
||||
|
||||
/// <summary>The linked company user.</summary>
|
||||
public long? LinkedUserId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>A company user (<c>users</c> in the company database).</summary>
|
||||
public sealed class CompanyUser : Entity<long>, IAccount
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? Email { get; set; }
|
||||
|
||||
/// <summary>A person or a technical user for the API.</summary>
|
||||
public UserKind Kind { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? PasswordHash { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? PasswordHistory { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? PasswordChangedAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool MustChangePassword { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? TemporaryPasswordExpiresAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int FailedLoginCount { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? LockedUntil { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? TotpSecret { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TotpEnabled { get; set; }
|
||||
|
||||
/// <summary>Whether this user must set up the two-step verification (configurable per user).</summary>
|
||||
public bool TotpRequired { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public long? TotpLastStep { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? TotpRecoveryCodes { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? RecoveryTokenHash { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? RecoveryTokenExpiresAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsActive { get; set; } = true;
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? LastLoginAt { get; set; }
|
||||
|
||||
/// <summary>Sees only the customers assigned to the user (agents).</summary>
|
||||
public bool OwnCustomersOnly { get; set; }
|
||||
|
||||
/// <summary>Sees costs and margins.</summary>
|
||||
public bool CanSeeCosts { get; set; }
|
||||
|
||||
/// <summary>Can export lists and reports.</summary>
|
||||
public bool CanExport { get; set; }
|
||||
|
||||
/// <summary>Can use the API.</summary>
|
||||
public bool CanUseApi { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using Khaemhat.Core.Entities;
|
||||
|
||||
namespace Khaemhat.Core.Security;
|
||||
|
||||
/// <summary>How far a sign-in has gone. Only <see cref="Active"/> sessions open the application.</summary>
|
||||
public enum SessionState
|
||||
{
|
||||
/// <summary>The password was right; the TOTP code is still missing.</summary>
|
||||
PendingTotp = 0,
|
||||
|
||||
/// <summary>The two-step verification must be set up before going on (mandatory for instance accounts).</summary>
|
||||
PendingTotpSetup = 1,
|
||||
|
||||
/// <summary>The password must be changed before going on.</summary>
|
||||
PendingPasswordChange = 2,
|
||||
|
||||
/// <summary>A complete session.</summary>
|
||||
Active = 3,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A sign-in session (SPEC §7.3). The cookie carries a random 256-bit token; the table stores only its SHA-256 hash,
|
||||
/// so a copy of the database does not let anyone in.
|
||||
/// </summary>
|
||||
public abstract class SessionRecord : Entity<long>
|
||||
{
|
||||
/// <summary>The SHA-256 hash of the token, in lower-case hexadecimal.</summary>
|
||||
public string TokenHash { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The account.</summary>
|
||||
public long UserId { get; set; }
|
||||
|
||||
/// <summary>The state of the sign-in.</summary>
|
||||
public SessionState State { get; set; }
|
||||
|
||||
/// <summary>The company the session works in (chosen at sign-in, changeable by instance accounts).</summary>
|
||||
public string? CompanyCode { get; set; }
|
||||
|
||||
/// <summary>The client address.</summary>
|
||||
public string? IpAddress { get; set; }
|
||||
|
||||
/// <summary>The browser, as sent by the client (truncated).</summary>
|
||||
public string? UserAgent { get; set; }
|
||||
|
||||
/// <summary>Whether the cookie survives the browser ("remember me on this device").</summary>
|
||||
public bool Persistent { get; set; }
|
||||
|
||||
/// <summary>The last request seen (updated at most once a minute).</summary>
|
||||
public DateTime LastSeenAt { get; set; }
|
||||
|
||||
/// <summary>When the session expires unless used again (sliding expiry).</summary>
|
||||
public DateTime ExpiresAt { get; set; }
|
||||
|
||||
/// <summary>When the session was revoked.</summary>
|
||||
public DateTime? RevokedAt { get; set; }
|
||||
|
||||
/// <summary>Why the session was revoked (sign-out, administrator, password change, maintenance...).</summary>
|
||||
public string? RevokedReason { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>A session of an instance account (<c>sys_sessions</c>).</summary>
|
||||
public sealed class SystemSession : SessionRecord;
|
||||
|
||||
/// <summary>A session of a company user (<c>user_sessions</c>).</summary>
|
||||
public sealed class UserSession : SessionRecord;
|
||||
|
||||
/// <summary>A predefined role kept in the system database and copied into each company (<c>sys_roles_templates</c>).</summary>
|
||||
public sealed class RoleTemplate : Entity<long>
|
||||
{
|
||||
/// <summary>The role code, for example <c>ACCOUNTING</c>.</summary>
|
||||
public string Code { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The name shown in the interface.</summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>What the role is for.</summary>
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>The permission codes as a JSON array; <c>*</c> grants every permission.</summary>
|
||||
public string Permissions { get; set; } = "[]";
|
||||
|
||||
/// <summary>The order in lists.</summary>
|
||||
public int SortOrder { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>A role of a company (<c>roles</c>).</summary>
|
||||
public sealed class Role : Entity<long>
|
||||
{
|
||||
/// <summary>The role code, unique in the company.</summary>
|
||||
public string Code { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The name shown in the interface.</summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>What the role is for.</summary>
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>A predefined role copied from a template: it can be cloned but not changed.</summary>
|
||||
public bool IsPredefined { get; set; }
|
||||
|
||||
/// <summary>The template the role comes from, or the role it was cloned from.</summary>
|
||||
public string? TemplateCode { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>A permission granted by a role (<c>role_permissions</c>).</summary>
|
||||
public sealed class RoleGrant : Entity<long>
|
||||
{
|
||||
/// <summary>The role.</summary>
|
||||
public long RoleId { get; set; }
|
||||
|
||||
/// <summary>The permission code, or <c>*</c>.</summary>
|
||||
public string Permission { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>A role assigned to a company user (<c>user_roles</c>).</summary>
|
||||
public sealed class UserRole : Entity<long>
|
||||
{
|
||||
/// <summary>The user.</summary>
|
||||
public long UserId { get; set; }
|
||||
|
||||
/// <summary>The role.</summary>
|
||||
public long RoleId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>A permission granted or denied to one user over the roles (<c>user_permission_overrides</c>). Denials win.</summary>
|
||||
public sealed class UserPermissionOverride : Entity<long>
|
||||
{
|
||||
/// <summary>The user.</summary>
|
||||
public long UserId { get; set; }
|
||||
|
||||
/// <summary>The permission code.</summary>
|
||||
public string Permission { get; set; } = string.Empty;
|
||||
|
||||
/// <summary><see langword="true"/> grants, <see langword="false"/> denies.</summary>
|
||||
public bool Allowed { get; set; }
|
||||
}
|
||||
Reference in new issue
Block a user