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:
Lucas RayanandClaude Opus 5.5 committed 2026-11-04 19:29:52 -03:00
1 parent 2b71ae41c0
commit 411a21ca6e
4 files changed
+246 -31

No files matched your search

+156 -4
View File
@@ -195,7 +195,7 @@ fn admin_cannot_finish_login_without_mfa() {
AuthFailure::InvalidCredentials
);
let next = code_at(&s, &id, T0 + 60);
let session = s.confirm_mfa(&p.token, &next, T0 + 60).unwrap();
let session = session_of(s.confirm_mfa(&p.token, &next, T0 + 60).unwrap());
assert_eq!(session.user_id, id);
assert_eq!(sessions(&s, &id), 1);
// Once MFA is active a pending token gives no enrollment access.
@@ -611,9 +611,10 @@ fn password_or_mfa_change_ends_other_sessions() {
// Disabling MFA keeps only the named session.
let p = pending_of(s.login("ana", PW2, ip(), T0 + 60).unwrap());
let a = s
.confirm_mfa(&p.token, &code_at_active(&s, &id, T0 + 60), T0 + 60)
.unwrap();
let a = session_of(
s.confirm_mfa(&p.token, &code_at_active(&s, &id, T0 + 60), T0 + 60)
.unwrap(),
);
let p = pending_of(s.login("ana", PW2, ip(), T0 + 90).unwrap());
s.confirm_mfa(&p.token, &code_at_active(&s, &id, T0 + 90), T0 + 90)
.unwrap();
@@ -793,3 +794,154 @@ fn open_fails_on_missing_key_without_touching_state() {
);
assert_eq!(std::fs::read(auth.join("state.json")).unwrap(), before);
}
/// Admin after `reset_admin`, enrolled again. Returns (id, temp password).
fn reset_and_enroll(s: &AuthService) -> (String, String) {
let id = admin(s);
let temp = s.reset_admin("root", T0 + 200).unwrap();
enroll(s, &id, T0 + 200);
(id, temp)
}
fn change_ticket(step: LoginStep) -> PendingMfa {
match step {
LoginStep::PasswordChangeRequired(p) => p,
other => panic!("expected PasswordChangeRequired, got {other:?}"),
}
}
#[test]
fn must_change_password_survives_enrollment_and_reopen() {
let (d, s) = svc();
let (id, temp) = reset_and_enroll(&s);
drop(s);
let s = AuthService::open(d.path(), None).unwrap();
let flag = s
.store
.read(|st| {
st.users
.iter()
.find(|u| u.id == id)
.unwrap()
.must_change_password
})
.unwrap();
assert!(flag);
let p = pending_of(s.login("root", &temp, ip(), T0 + 300).unwrap());
let code = code_at_active(&s, &id, T0 + 300);
change_ticket(s.confirm_mfa(&p.token, &code, T0 + 300).unwrap());
}
#[test]
fn no_ordinary_session_while_password_change_is_required() {
let (_d, s) = svc();
let (id, temp) = reset_and_enroll(&s);
let p = pending_of(s.login("root", &temp, ip(), T0 + 300).unwrap());
let code = code_at_active(&s, &id, T0 + 300);
let ticket = change_ticket(s.confirm_mfa(&p.token, &code, T0 + 300).unwrap());
assert_eq!(sessions(&s, &id), 0);
// The ticket is good for nothing else, and a pending token is no ticket.
assert_eq!(
failure(s.pending_user(&ticket.token, T0 + 300)),
AuthFailure::InvalidCredentials
);
let next = code_at_active(&s, &id, T0 + 330);
assert_eq!(
failure(s.confirm_mfa(&ticket.token, &next, T0 + 330)),
AuthFailure::InvalidCredentials
);
let p2 = pending_of(s.login("root", &temp, ip(), T0 + 330).unwrap());
assert_eq!(
failure(s.change_required_password(&p2.token, &temp, PW2, T0 + 330)),
AuthFailure::InvalidCredentials
);
// Expired ticket.
let t2 = change_ticket(s.confirm_mfa(&p2.token, &next, T0 + 330).unwrap());
assert_eq!(
failure(s.change_required_password(&t2.token, &temp, PW2, T0 + 631)),
AuthFailure::InvalidCredentials
);
assert_eq!(sessions(&s, &id), 0);
let n = s
.change_required_password(&t2.token, &temp, PW2, T0 + 330)
.unwrap();
assert_eq!(n.user_id, id);
assert_eq!(sessions(&s, &id), 1);
// Single use, the flag is gone, the temporary password is dead.
assert_eq!(
failure(s.change_required_password(&t2.token, PW2, PW, T0 + 330)),
AuthFailure::InvalidCredentials
);
assert_eq!(
failure(s.login("root", &temp, ip(), T0 + 360)),
AuthFailure::InvalidCredentials
);
let p = pending_of(s.login("root", PW2, ip(), T0 + 360).unwrap());
let code = code_at_active(&s, &id, T0 + 360);
session_of(s.confirm_mfa(&p.token, &code, T0 + 360).unwrap());
}
#[test]
fn flag_also_blocks_password_only_login() {
// A reset admin later demoted, without MFA, reaches the password-only path.
let (_d, s) = svc();
let root = admin(&s);
s.create_user("root", "second", PW, Role::Admin, T0)
.unwrap();
let second = uid(&s, "second");
let temp = s.reset_admin("second", T0).unwrap();
s.set_role("root", &second, Role::Analyst, T0).unwrap();
let t = change_ticket(s.login("second", &temp, ip(), T0).unwrap());
assert_eq!(sessions(&s, &second), 0);
s.change_required_password(&t.token, &temp, PW2, T0)
.unwrap();
session_of(s.login("second", PW2, ip(), T0).unwrap());
assert_eq!(sessions(&s, &root), 0);
}
#[test]
fn change_password_clears_flag_and_refuses_the_same_password() {
let (_d, s) = svc();
let (id, temp) = reset_and_enroll(&s);
// Same password, also in another Unicode form: refused, nothing counted.
assert!(matches!(
s.change_password(&id, &temp, &temp, None, T0 + 300)
.unwrap_err(),
AcauaError::InvalidInput(_)
));
assert_eq!(
s.store
.read(|st| st
.users
.iter()
.find(|u| u.id == id)
.unwrap()
.failed_attempts)
.unwrap(),
0
);
assert!(matches!(
s.change_password(
&id,
"maçã açaí pitanga",
"mac\u{0327}a\u{0303} ac\u{0327}ai\u{0301} pitanga",
None,
T0
)
.unwrap_err(),
AcauaError::InvalidInput(_)
));
s.change_password(&id, &temp, PW2, None, T0 + 300).unwrap();
let flag = s
.store
.read(|st| {
st.users
.iter()
.find(|u| u.id == id)
.unwrap()
.must_change_password
})
.unwrap();
assert!(!flag);
}