feat(auth): sessions and scoped API tokens

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:34:37 -03:00
1 parent 411a21ca6e
commit c2d352ae30
7 files changed
+931 -13

No files matched your search

+12 -10
View File
@@ -98,18 +98,18 @@ struct PendingClaims {
pub struct AuthService {
pub(super) store: AuthStore,
pub(super) secrets: Secrets,
audit: AuditLog,
pub(super) audit: AuditLog,
pub(super) ips: IpLimiter,
unknown: UnknownAccounts,
#[cfg_attr(not(test), allow(dead_code))]
pub(super) auth_dir: PathBuf,
}
fn throttled(retry_after_secs: u64) -> AcauaError {
pub(super) fn throttled(retry_after_secs: u64) -> AcauaError {
AuthFailure::Throttled { retry_after_secs }.into()
}
fn invalid() -> AcauaError {
pub(super) fn invalid() -> AcauaError {
AuthFailure::InvalidCredentials.into()
}
@@ -145,20 +145,20 @@ fn has_admin(st: &AuthState) -> bool {
st.users.iter().any(|u| u.role == Role::Admin)
}
fn user_mut<'a>(st: &'a mut AuthState, id: &str) -> Option<&'a mut User> {
pub(super) fn user_mut<'a>(st: &'a mut AuthState, id: &str) -> Option<&'a mut User> {
st.users.iter_mut().find(|u| u.id == id)
}
fn user_or_missing<'a>(st: &'a mut AuthState, id: &str) -> Result<&'a mut User> {
pub(super) fn user_or_missing<'a>(st: &'a mut AuthState, id: &str) -> Result<&'a mut User> {
user_mut(st, id).ok_or_else(|| AuthFailure::UserNotFound.into())
}
fn count_failure(u: &mut User, now: u64) {
pub(super) fn count_failure(u: &mut User, now: u64) {
u.failed_attempts = u.failed_attempts.saturating_add(1);
u.locked_until = lock_until(u.failed_attempts, now);
}
fn check_lock(u: &User, now: u64) -> std::result::Result<(), u64> {
pub(super) fn check_lock(u: &User, now: u64) -> std::result::Result<(), u64> {
if u.locked_until > now {
Err(u.locked_until - now)
} else {
@@ -168,7 +168,7 @@ fn check_lock(u: &User, now: u64) -> std::result::Result<(), u64> {
/// Counts the attempt as failed before Argon2 runs, so parallel guesses
/// cannot all pass the check. `refund_attempt` undoes it on a right password.
fn reserve_attempt(u: &mut User, now: u64) -> std::result::Result<(), u64> {
pub(super) fn reserve_attempt(u: &mut User, now: u64) -> std::result::Result<(), u64> {
check_lock(u, now)?;
count_failure(u, now);
Ok(())
@@ -176,7 +176,7 @@ fn reserve_attempt(u: &mut User, now: u64) -> std::result::Result<(), u64> {
/// The password was right: this attempt was no failure. Earlier failures stay,
/// because they may be MFA guesses; only a completed login clears them.
fn refund_attempt(u: &mut User) {
pub(super) fn refund_attempt(u: &mut User) {
u.failed_attempts = u.failed_attempts.saturating_sub(1);
u.locked_until = 0;
}
@@ -250,7 +250,7 @@ impl AuthService {
})
}
fn log(
pub(super) fn log(
&self,
action: &str,
actor: Option<&str>,
@@ -686,6 +686,7 @@ impl AuthService {
let name = u.username.clone();
st.users.retain(|u| u.id != user_id);
end_sessions(st, user_id, None);
st.tokens.retain(|t| t.user_id != user_id);
Ok(name)
});
self.log_admin_result("user.delete", actor, &result)?;
@@ -829,6 +830,7 @@ impl AuthService {
u.auth_epoch = u.auth_epoch.wrapping_add(1);
let (id, name) = (u.id.clone(), u.username.clone());
end_sessions(st, &id, None);
st.tokens.retain(|t| t.user_id != id);
Ok(Some(name))
})?;
let Some(name) = target else {