feat(auth): accounts, first-run setup and MFA login flow

Adds AuthService with single-use setup code, password login, TOTP and
recovery codes, enrollment, account management and local admin reset.
Attempts are reserved before Argon2 so parallel guesses cannot outrun the
per-account delay or the IP limit, and unknown usernames follow the same
schedule as real accounts. Passwords are NFC-normalized; repeated-character
and all-digit passwords are refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-04 19:24:46 -03:00
1 parent deea3e245b
commit 2b71ae41c0
9 files changed
+1998 -10

No files matched your search

+59 -6
View File
@@ -10,21 +10,73 @@ pub const STATE_VERSION: u32 = 1;
// Later tasks add fields; `#[serde(default)]` keeps older files readable.
// Unknown fields are ignored on read (and so dropped on rewrite).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
Admin,
/// The default is the least-privileged role.
#[default]
Analyst,
}
#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct User {
pub id: String,
pub username: String,
pub created_at: String,
/// Unix seconds, like every timestamp here.
pub created_at: u64,
pub role: Role,
/// Argon2id PHC string.
pub password_hash: String,
/// Active TOTP secret, sealed with `Secrets`.
pub totp_sealed: Option<String>,
/// Secret awaiting its first code, sealed. Not usable to log in.
pub totp_pending_sealed: Option<String>,
/// Newest accepted TOTP step; a step is never accepted twice.
pub totp_last_step: Option<u64>,
/// SHA-256 hex of each unused recovery code.
pub recovery_hashes: Vec<String>,
/// Failures since the last completed login; survives restarts.
pub failed_attempts: u32,
/// No attempt is evaluated before this instant.
pub locked_until: u64,
/// Changes when the password changes and at each completed login, which
/// voids the pending MFA tokens issued before it.
pub auth_epoch: u64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
impl std::fmt::Debug for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("User")
.field("id", &self.id)
.field("username", &self.username)
.field("role", &self.role)
.field("mfa", &self.totp_sealed.is_some())
.finish_non_exhaustive()
}
}
#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Session {
pub id: String,
pub user_id: String,
pub created_at: String,
pub expires_at: String,
/// SHA-256 hex of the session token; the token itself is never stored.
pub token_hash: String,
pub created_at: u64,
pub last_seen: u64,
pub expires_at: u64,
}
impl std::fmt::Debug for Session {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Session")
.field("id", &self.id)
.field("user_id", &self.user_id)
.field("expires_at", &self.expires_at)
.finish_non_exhaustive()
}
}
#[derive(Clone, Default, Serialize, Deserialize)]
@@ -52,7 +104,8 @@ impl std::fmt::Debug for ApiToken {
#[serde(default)]
pub struct Bootstrap {
pub token_hash: String,
pub created_at: String,
/// Unix seconds.
pub created_at: u64,
}
impl std::fmt::Debug for Bootstrap {