fix(auth): force a password change after reset_admin
reset_admin now sets a persisted must_change_password flag. While it is set, login and confirm_mfa return PasswordChangeRequired, a ticket that only allows change_required_password, instead of a session. confirm_mfa therefore returns LoginStep. change_password clears the flag and refuses the current password as the new one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2b71ae41c0
commit
411a21ca6e
4 files changed
+246
-31
No files matched your search
@@ -7,8 +7,8 @@
|
||||
//! failed attempt must return `Ok` from its closure, or its count is discarded.
|
||||
|
||||
use super::credentials::{
|
||||
hash_password, hash_recovery_code, new_recovery_codes, new_totp_secret, totp_uri_and_qr,
|
||||
validate_password, verify_password, verify_totp,
|
||||
hash_password, hash_recovery_code, new_recovery_codes, new_totp_secret, normalize,
|
||||
totp_uri_and_qr, validate_password, verify_password, verify_totp,
|
||||
};
|
||||
use super::store::{AuthState, AuthStore, Bootstrap, Role, Session, User};
|
||||
use super::throttle::{lock_until, IpLimiter, UnknownAccounts};
|
||||
@@ -23,6 +23,7 @@ const BOOTSTRAP_CODE_BYTES: usize = 16; // 128 bits
|
||||
const BOOTSTRAP_TTL_SECS: u64 = 3600;
|
||||
const PENDING_TTL_SECS: u64 = 300;
|
||||
const PENDING_PURPOSE: &str = "pending_mfa";
|
||||
const PASSWORD_CHANGE_PURPOSE: &str = "password_change";
|
||||
const SESSION_TOKEN_BYTES: usize = 32; // 256 bits
|
||||
const SESSION_ABSOLUTE_SECS: u64 = 43_200;
|
||||
const USERNAME_MAX_CHARS: usize = 64;
|
||||
@@ -67,6 +68,9 @@ pub enum LoginStep {
|
||||
Session(NewSession),
|
||||
MfaRequired(PendingMfa),
|
||||
MfaEnrollRequired(PendingMfa),
|
||||
/// All factors passed, but the token only allows
|
||||
/// `change_required_password`. It is not a session.
|
||||
PasswordChangeRequired(PendingMfa),
|
||||
}
|
||||
|
||||
/// What the user needs to add the account to an authenticator app.
|
||||
@@ -325,7 +329,7 @@ impl AuthService {
|
||||
password_hash: password_hash.clone(),
|
||||
..Default::default()
|
||||
});
|
||||
Ok(Some(self.seal_pending(&id, 0, now)?))
|
||||
Ok(Some(self.seal_pending(&id, 0, PENDING_PURPOSE, now)?))
|
||||
})?;
|
||||
let Some(pending) = done else {
|
||||
self.log("bootstrap.complete", None, None, false, None)?;
|
||||
@@ -335,10 +339,16 @@ impl AuthService {
|
||||
Ok(pending)
|
||||
}
|
||||
|
||||
fn seal_pending(&self, user_id: &str, epoch: u64, now: u64) -> Result<PendingMfa> {
|
||||
fn seal_pending(
|
||||
&self,
|
||||
user_id: &str,
|
||||
epoch: u64,
|
||||
purpose: &str,
|
||||
now: u64,
|
||||
) -> Result<PendingMfa> {
|
||||
let exp = now.saturating_add(PENDING_TTL_SECS);
|
||||
let claims = PendingClaims {
|
||||
purpose: PENDING_PURPOSE.to_string(),
|
||||
purpose: purpose.to_string(),
|
||||
user_id: user_id.to_string(),
|
||||
epoch,
|
||||
exp,
|
||||
@@ -350,10 +360,10 @@ impl AuthService {
|
||||
}
|
||||
|
||||
/// Any defect is the same refusal: forged, expired, or for another purpose.
|
||||
fn open_pending(&self, token: &str, now: u64) -> Result<PendingClaims> {
|
||||
fn open_pending(&self, token: &str, purpose: &str, now: u64) -> Result<PendingClaims> {
|
||||
let raw = self.secrets.open_sealed(token).map_err(|_| invalid())?;
|
||||
let claims: PendingClaims = serde_json::from_slice(&raw).map_err(|_| invalid())?;
|
||||
if claims.purpose != PENDING_PURPOSE || now > claims.exp {
|
||||
if claims.purpose != purpose || now > claims.exp {
|
||||
return Err(invalid());
|
||||
}
|
||||
Ok(claims)
|
||||
@@ -434,9 +444,16 @@ impl AuthService {
|
||||
refund_attempt(u);
|
||||
let epoch = u.auth_epoch;
|
||||
let step = if u.totp_sealed.is_some() {
|
||||
LoginStep::MfaRequired(self.seal_pending(&id, epoch, now)?)
|
||||
LoginStep::MfaRequired(self.seal_pending(&id, epoch, PENDING_PURPOSE, now)?)
|
||||
} else if u.role == Role::Admin {
|
||||
LoginStep::MfaEnrollRequired(self.seal_pending(&id, epoch, now)?)
|
||||
LoginStep::MfaEnrollRequired(self.seal_pending(&id, epoch, PENDING_PURPOSE, now)?)
|
||||
} else if u.must_change_password {
|
||||
LoginStep::PasswordChangeRequired(self.seal_pending(
|
||||
&id,
|
||||
epoch,
|
||||
PASSWORD_CHANGE_PURPOSE,
|
||||
now,
|
||||
)?)
|
||||
} else {
|
||||
LoginStep::Session(complete_login(st, &id, now)?)
|
||||
};
|
||||
@@ -457,6 +474,7 @@ impl AuthService {
|
||||
LoginStep::Session(_) => "session",
|
||||
LoginStep::MfaRequired(_) => "mfa required",
|
||||
LoginStep::MfaEnrollRequired(_) => "mfa enrollment required",
|
||||
LoginStep::PasswordChangeRequired(_) => "password change required",
|
||||
};
|
||||
self.log("login", Some(&name), Some(ip), true, Some(detail))?;
|
||||
Ok(step)
|
||||
@@ -464,16 +482,16 @@ impl AuthService {
|
||||
|
||||
/// Second factor: a TOTP code or a recovery code. Failures count against
|
||||
/// the account, since 6 digits cannot stand unlimited guesses.
|
||||
pub fn confirm_mfa(&self, pending: &str, code: &str, now: u64) -> Result<NewSession> {
|
||||
pub fn confirm_mfa(&self, pending: &str, code: &str, now: u64) -> Result<LoginStep> {
|
||||
enum Outcome {
|
||||
Done {
|
||||
session: NewSession,
|
||||
step: LoginStep,
|
||||
name: String,
|
||||
method: &'static str,
|
||||
},
|
||||
Bad(String),
|
||||
}
|
||||
let claims = self.open_pending(pending, now)?;
|
||||
let claims = self.open_pending(pending, PENDING_PURPOSE, now)?;
|
||||
let recovery_hash = hash_recovery_code(code);
|
||||
let outcome = self.store.update(|st| {
|
||||
let u = user_mut(st, &claims.user_id)
|
||||
@@ -493,25 +511,31 @@ impl AuthService {
|
||||
count_failure(u, now);
|
||||
return Ok(Outcome::Bad(name));
|
||||
};
|
||||
let session = complete_login(st, &claims.user_id, now)?;
|
||||
Ok(Outcome::Done {
|
||||
session,
|
||||
name,
|
||||
method,
|
||||
})
|
||||
let step = if u.must_change_password {
|
||||
// Factors passed, so the throttle clears and this pending
|
||||
// token dies, but only the password step opens.
|
||||
u.failed_attempts = 0;
|
||||
u.locked_until = 0;
|
||||
u.auth_epoch = u.auth_epoch.wrapping_add(1);
|
||||
LoginStep::PasswordChangeRequired(self.seal_pending(
|
||||
&claims.user_id,
|
||||
u.auth_epoch,
|
||||
PASSWORD_CHANGE_PURPOSE,
|
||||
now,
|
||||
)?)
|
||||
} else {
|
||||
LoginStep::Session(complete_login(st, &claims.user_id, now)?)
|
||||
};
|
||||
Ok(Outcome::Done { step, name, method })
|
||||
})?;
|
||||
match outcome {
|
||||
Outcome::Bad(name) => {
|
||||
self.log("mfa.confirm", Some(&name), None, false, None)?;
|
||||
Err(invalid())
|
||||
}
|
||||
Outcome::Done {
|
||||
session,
|
||||
name,
|
||||
method,
|
||||
} => {
|
||||
Outcome::Done { step, name, method } => {
|
||||
self.log("mfa.confirm", Some(&name), None, true, Some(method))?;
|
||||
Ok(session)
|
||||
Ok(step)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -519,7 +543,7 @@ impl AuthService {
|
||||
/// The user a pending token may enroll. Refused once MFA is active, so a
|
||||
/// password alone can never replace an existing second factor.
|
||||
pub fn pending_user(&self, pending: &str, now: u64) -> Result<String> {
|
||||
let claims = self.open_pending(pending, now)?;
|
||||
let claims = self.open_pending(pending, PENDING_PURPOSE, now)?;
|
||||
let allowed = self.store.read(|st| {
|
||||
st.users.iter().any(|u| {
|
||||
u.id == claims.user_id && u.auth_epoch == claims.epoch && u.totp_sealed.is_none()
|
||||
@@ -691,6 +715,12 @@ impl AuthService {
|
||||
now: u64,
|
||||
) -> Result<()> {
|
||||
validate_password(new)?;
|
||||
// Comparing the two inputs reveals nothing about the stored password.
|
||||
if normalize(current) == normalize(new) {
|
||||
return Err(AcauaError::InvalidInput(
|
||||
"new password must differ from the current one".to_string(),
|
||||
));
|
||||
}
|
||||
let (name, hash) = self.store.update(|st| {
|
||||
let u = user_or_missing(st, user_id)?;
|
||||
reserve_attempt(u, now).map_err(throttled)?;
|
||||
@@ -707,6 +737,7 @@ impl AuthService {
|
||||
};
|
||||
refund_attempt(u);
|
||||
u.password_hash = new_hash;
|
||||
u.must_change_password = false;
|
||||
u.auth_epoch = u.auth_epoch.wrapping_add(1);
|
||||
end_sessions(st, user_id, keep_session_token);
|
||||
Ok(true)
|
||||
@@ -718,6 +749,34 @@ impl AuthService {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Spends a `PasswordChangeRequired` ticket: changes the password under
|
||||
/// the rules of `change_password`, then opens the session.
|
||||
pub fn change_required_password(
|
||||
&self,
|
||||
ticket: &str,
|
||||
current: &str,
|
||||
new: &str,
|
||||
now: u64,
|
||||
) -> Result<NewSession> {
|
||||
let claims = self.open_pending(ticket, PASSWORD_CHANGE_PURPOSE, now)?;
|
||||
let live = self.store.read(|st| {
|
||||
st.users.iter().any(|u| {
|
||||
u.id == claims.user_id && u.auth_epoch == claims.epoch && u.must_change_password
|
||||
})
|
||||
})?;
|
||||
if !live {
|
||||
return Err(invalid());
|
||||
}
|
||||
self.change_password(&claims.user_id, current, new, None, now)?;
|
||||
self.store.update(|st| {
|
||||
let u = user_mut(st, &claims.user_id).ok_or_else(invalid)?;
|
||||
if u.must_change_password {
|
||||
return Err(invalid());
|
||||
}
|
||||
complete_login(st, &claims.user_id, now)
|
||||
})
|
||||
}
|
||||
|
||||
/// Refused for administrators. Ends the other sessions.
|
||||
pub fn disable_mfa(
|
||||
&self,
|
||||
@@ -745,7 +804,8 @@ impl AuthService {
|
||||
|
||||
/// Local recovery for an administrator who lost access: removes MFA and
|
||||
/// sessions, clears the throttle, and returns a temporary password that
|
||||
/// is shown once. The next login must enroll again.
|
||||
/// is shown once. The next login must enroll again and change the password
|
||||
/// before any ordinary session.
|
||||
pub fn reset_admin(&self, username: &str, _now: u64) -> Result<String> {
|
||||
let key = username_key(username);
|
||||
let temp = random_token(TEMP_PASSWORD_BYTES);
|
||||
@@ -759,6 +819,7 @@ impl AuthService {
|
||||
return Ok(None);
|
||||
};
|
||||
u.password_hash = password_hash;
|
||||
u.must_change_password = true;
|
||||
u.totp_sealed = None;
|
||||
u.totp_pending_sealed = None;
|
||||
u.totp_last_step = None;
|
||||
|
||||
Reference in new issue
Block a user