use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;
use id_effect::{Effect, UtcDateTime, WallClock, fail, succeed, suspend};
use id_effect_authz::{ClaimContains, ClaimPolicy, claim_contains};
use serde_json::Value;
use totp_rs::{Builder, Secret};
pub use totp_rs::Algorithm;
use crate::password::{PasswordError, PasswordHash, PasswordHasher};
use crate::principal::AuthPrincipal;
use crate::session::{Session, SessionError, Sessions};
use crate::throttle::{MemoryThrottleStore, ThrottleStore, elapsed};
pub const DEFAULT_MAX_FAILURES: u32 = 5;
pub const DEFAULT_LOCKOUT: Duration = Duration::from_secs(15 * 60);
pub const DANGEROUS_WITHOUT_LOCKOUT: &str = "id_effect_auth::totp::Totp::dangerous_without_lockout";
const SECRET_BYTES: usize = 20;
pub const MFA_VERIFIED_AT_ATTR: &str = "mfa_verified_at";
pub const AMR_CLAIM: &str = "amr";
pub const MFA_AT_CLAIM: &str = "mfa_at";
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TotpError {
Config(String),
Random(String),
Storage(String),
Password(PasswordError),
Locked {
retry_after: Duration,
},
}
impl fmt::Display for TotpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Config(m) => write!(f, "invalid TOTP configuration: {m}"),
Self::Random(m) => write!(f, "TOTP secret generation: {m}"),
Self::Storage(m) => write!(f, "TOTP step storage: {m}"),
Self::Password(e) => write!(f, "recovery code hashing: {e}"),
Self::Locked { retry_after } => write!(
f,
"too many wrong codes; retry in {} s",
crate::error::retry_after_secs(*retry_after)
),
}
}
}
impl std::error::Error for TotpError {}
impl From<PasswordError> for TotpError {
fn from(e: PasswordError) -> Self {
Self::Password(e)
}
}
fn random(bytes: &mut [u8]) -> Result<(), TotpError> {
getrandom::fill(bytes).map_err(|e| TotpError::Random(e.to_string()))
}
#[derive(Clone, PartialEq, Eq)]
pub struct TotpSecret(Secret);
impl TotpSecret {
pub fn generate() -> Result<Self, TotpError> {
let mut bytes = [0_u8; SECRET_BYTES];
random(&mut bytes)?;
Ok(Self(Secret::from(bytes)))
}
pub fn from_bytes(bytes: &[u8]) -> Self {
Self(Secret::from(bytes))
}
pub fn from_base32(encoded: &str) -> Result<Self, TotpError> {
Secret::try_from_base32(encoded)
.map(Self)
.map_err(|e| TotpError::Config(e.to_string()))
}
pub fn to_base32(&self) -> String {
self.0.to_base32()
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}
impl fmt::Debug for TotpSecret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TotpSecret(<redacted>)")
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TotpConfig {
pub issuer: String,
pub algorithm: Algorithm,
pub digits: u8,
pub step_secs: u64,
pub skew: u16,
}
impl TotpConfig {
pub fn new(issuer: impl Into<String>) -> Self {
Self {
issuer: issuer.into(),
algorithm: Algorithm::SHA1,
digits: 6,
step_secs: 30,
skew: 1,
}
}
}
#[derive(Clone)]
pub struct Enrolment {
secret: TotpSecret,
uri: String,
}
impl fmt::Debug for Enrolment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Enrolment(<redacted>)")
}
}
impl Enrolment {
#[inline]
pub fn secret(&self) -> &TotpSecret {
&self.secret
}
#[inline]
pub fn uri(&self) -> &str {
&self.uri
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TotpVerification {
Valid,
Invalid,
Replayed,
}
impl TotpVerification {
#[inline]
pub fn is_valid(self) -> bool {
self == Self::Valid
}
}
pub trait TotpStepStore: Send + Sync + 'static {
fn consume(&self, user_id: &str, step: u64) -> Effect<bool, TotpError>;
}
#[derive(Default)]
struct MemorySteps {
last: HashMap<String, u64>,
fail: Option<String>,
}
pub const MEMORY_TOTP_STEP_STORE_POSTURE: &str = "id_effect_auth::MemoryTotpStepStore::memory_only";
#[derive(Clone)]
pub struct MemoryTotpStepStore {
inner: Arc<Mutex<MemorySteps>>,
}
impl Default for MemoryTotpStepStore {
fn default() -> Self {
id_effect::observability::posture::record_dangerous(
MEMORY_TOTP_STEP_STORE_POSTURE,
"M18: TOTP replay state is memory-only; a code accepted on one instance is accepted again \
on another",
);
Self {
inner: Arc::default(),
}
}
}
impl fmt::Debug for MemoryTotpStepStore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MemoryTotpStepStore")
.finish_non_exhaustive()
}
}
impl MemoryTotpStepStore {
#[inline]
pub fn new() -> Self {
Self::default()
}
pub fn last_step(&self, user_id: &str) -> Option<u64> {
let st = self.inner.lock().unwrap_or_else(PoisonError::into_inner);
st.last.get(user_id).copied()
}
pub fn fail_with(&self, message: Option<&str>) {
let mut st = self.inner.lock().unwrap_or_else(PoisonError::into_inner);
st.fail = message.map(str::to_owned);
}
}
impl TotpStepStore for MemoryTotpStepStore {
fn consume(&self, user_id: &str, step: u64) -> Effect<bool, TotpError> {
let (inner, user_id) = (Arc::clone(&self.inner), user_id.to_owned());
suspend(move || {
let mut st = inner.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(m) = st.fail.clone() {
return fail(TotpError::Storage(m));
}
let later = st.last.get(&user_id).is_none_or(|&last| step > last);
if later {
st.last.insert(user_id, step);
}
succeed(later)
})
}
}
#[cfg(feature = "pg")]
mod pg {
use std::fmt;
use id_effect::{Effect, fail, succeed};
use id_effect_sql::{SqlClient, SqlClientService, SqlError, params};
use super::{TotpError, TotpStepStore};
use crate::session::statements;
const SCHEMA: &str = include_str!("../migrations/0003_id_effect_auth_state.up.sql");
pub(super) const CONSUME: &str = "INSERT INTO id_effect_totp_steps AS s (user_id, last_step) \
VALUES ($1, $2) \
ON CONFLICT (user_id) DO UPDATE SET last_step = EXCLUDED.last_step \
WHERE s.last_step < EXCLUDED.last_step";
#[derive(Clone)]
pub struct PgTotpStepStore {
client: SqlClientService,
}
impl fmt::Debug for PgTotpStepStore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PgTotpStepStore")
.field("table", &"id_effect_totp_steps")
.finish_non_exhaustive()
}
}
impl PgTotpStepStore {
#[inline]
pub fn new(client: impl SqlClient) -> Self {
Self::from_service(SqlClientService::new(client))
}
#[inline]
pub fn from_service(client: SqlClientService) -> Self {
Self { client }
}
pub fn ensure_schema(&self) -> Effect<(), TotpError> {
let client = self.client.clone();
statements(SCHEMA)
.into_iter()
.fold(succeed(()), move |done, sql| {
let client = client.clone();
done.flat_map(move |()| client.execute(&sql, &[]).map(|_| ()).map_err(storage))
})
}
}
fn storage(e: SqlError) -> TotpError {
TotpError::Storage(e.to_string())
}
impl TotpStepStore for PgTotpStepStore {
fn consume(&self, user_id: &str, step: u64) -> Effect<bool, TotpError> {
let Ok(step) = i64::try_from(step) else {
return fail(TotpError::Storage(format!("TOTP step {step} out of range")));
};
self
.client
.execute(CONSUME, ¶ms![user_id, step])
.map(|n| n > 0)
.map_err(storage)
}
}
}
#[cfg(feature = "pg")]
pub use pg::PgTotpStepStore;
#[derive(Clone)]
struct Lockout {
failures: Arc<dyn ThrottleStore>,
max_failures: u32,
window: Duration,
}
#[derive(Clone)]
pub struct Totp {
config: Arc<TotpConfig>,
steps: Arc<dyn TotpStepStore>,
clock: WallClock,
lockout: Option<Lockout>,
}
impl fmt::Debug for Totp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Totp")
.field("config", &self.config)
.field("clock", &self.clock)
.finish_non_exhaustive()
}
}
impl Totp {
pub fn new(config: TotpConfig, steps: impl TotpStepStore) -> Self {
Self::with_stores(config, steps, MemoryThrottleStore::new())
}
pub fn with_stores(
config: TotpConfig,
steps: impl TotpStepStore,
failures: impl ThrottleStore,
) -> Self {
Self {
config: Arc::new(config),
steps: Arc::new(steps),
clock: WallClock::system(),
lockout: Some(Lockout {
failures: Arc::new(failures),
max_failures: DEFAULT_MAX_FAILURES,
window: DEFAULT_LOCKOUT,
}),
}
}
#[must_use]
pub fn with_lockout(mut self, max_failures: u32, window: Duration) -> Self {
let failures = match self.lockout.take() {
Some(l) => l.failures,
None => Arc::new(MemoryThrottleStore::new()),
};
self.lockout = Some(Lockout {
failures,
max_failures: max_failures.max(1),
window,
});
self
}
#[must_use]
pub fn with_failure_store(mut self, store: impl ThrottleStore) -> Self {
let (max_failures, window) = self
.lockout
.as_ref()
.map_or((DEFAULT_MAX_FAILURES, DEFAULT_LOCKOUT), |l| {
(l.max_failures, l.window)
});
self.lockout = Some(Lockout {
failures: Arc::new(store),
max_failures,
window,
});
self
}
#[must_use]
pub fn dangerous_without_lockout(mut self) -> Self {
id_effect::observability::posture::record_dangerous(
DANGEROUS_WITHOUT_LOCKOUT,
"H6: TOTP verification does not lock after failures, so second-factor guessing is \
unlimited",
);
self.lockout = None;
self
}
#[deprecated(
since = "0.11.0",
note = "renamed to dangerous_without_lockout (ADR 0013 naming)"
)]
#[must_use]
pub fn without_lockout_dangerous(self) -> Self {
self.dangerous_without_lockout()
}
#[must_use]
pub fn with_clock(mut self, clock: WallClock) -> Self {
self.clock = clock;
self
}
#[inline]
pub fn config(&self) -> &TotpConfig {
&self.config
}
fn rfc(&self, secret: &TotpSecret, account: &str) -> Result<totp_rs::Totp, TotpError> {
let c = &self.config;
Builder::new()
.with_algorithm(c.algorithm)
.with_digits(c.digits)
.with_step_duration(c.step_secs)
.with_skew(c.skew)
.with_secret(secret.0.clone())
.with_issuer(Some(c.issuer.as_str()))
.with_account_name(account)
.build()
.map_err(|e| TotpError::Config(e.to_string()))
}
pub fn enrol(&self, account: &str) -> Result<Enrolment, TotpError> {
let secret = TotpSecret::generate()?;
let uri = self.uri(&secret, account)?;
Ok(Enrolment { secret, uri })
}
pub fn uri(&self, secret: &TotpSecret, account: &str) -> Result<String, TotpError> {
self
.rfc(secret, account)?
.to_url()
.map_err(|e| TotpError::Config(e.to_string()))
}
pub fn code_at(&self, secret: &TotpSecret, at: UtcDateTime) -> Result<String, TotpError> {
Ok(self.rfc(secret, "")?.generate(unix_secs(at)).to_string())
}
pub fn verify(
&self,
user_id: &str,
secret: &TotpSecret,
code: &str,
) -> Effect<TotpVerification, TotpError> {
let totp = match self.rfc(secret, "") {
Ok(t) => t,
Err(e) => return fail(e),
};
let code: String = code.chars().filter(|c| !c.is_whitespace()).collect();
let (steps, user_id) = (Arc::clone(&self.steps), user_id.to_owned());
let lockout = self.lockout.clone();
self.clock.now().flat_map(move |now| {
let key = format!("totp:{user_id}");
let allowed = match &lockout {
Some(l) => l.check(&key, now),
None => succeed(()),
};
allowed.flat_map(move |()| {
let checked = match totp.check(&code, unix_secs(now)) {
None => succeed(TotpVerification::Invalid),
Some(counter) => steps.consume(&user_id, counter).map(|fresh| {
if fresh {
TotpVerification::Valid
} else {
TotpVerification::Replayed
}
}),
};
checked.flat_map(move |outcome| match (lockout, outcome) {
(Some(l), TotpVerification::Invalid) => l.record(&key, now).map(move |()| outcome),
(Some(l), TotpVerification::Valid) => l
.failures
.clear(&key)
.map_err(storage)
.map(move |()| outcome),
_ => succeed(outcome),
})
})
})
}
pub fn step_up(
&self,
sessions: &Sessions,
mut session: Session,
) -> Effect<Option<Session>, SessionError> {
let (sessions, clock, id) = (sessions.clone(), self.clock.clone(), session.id().clone());
sessions.load(&id).flat_map(move |live| {
if live.is_none() {
return succeed(None);
}
clock.now().flat_map(move |now| {
mark_mfa(&mut session, now);
let rotate = sessions.clone();
sessions
.save(session)
.flat_map(move |()| rotate.rotate(&id))
})
})
}
}
fn storage(e: crate::AuthError) -> TotpError {
TotpError::Storage(e.to_string())
}
impl Lockout {
fn check(&self, key: &str, now: UtcDateTime) -> Effect<(), TotpError> {
let (max, window) = (self.max_failures, self.window);
self
.failures
.get(key)
.map_err(storage)
.flat_map(move |found| match found {
Some(f) if f.count >= max && elapsed(f.last, now) < window => fail(TotpError::Locked {
retry_after: window.saturating_sub(elapsed(f.last, now)),
}),
_ => succeed(()),
})
}
fn record(&self, key: &str, now: UtcDateTime) -> Effect<(), TotpError> {
self
.failures
.record_failure(key, now, self.window)
.map_err(storage)
.map(|_| ())
}
}
fn unix_secs(t: UtcDateTime) -> u64 {
u64::try_from(t.0.as_second()).unwrap_or(0)
}
pub fn mark_mfa(session: &mut Session, at: UtcDateTime) {
session.set_attr(MFA_VERIFIED_AT_ATTR, at.0.as_second().to_string());
}
pub fn mfa_verified_at(session: &Session) -> Option<UtcDateTime> {
let secs: i64 = session.attr(MFA_VERIFIED_AT_ATTR)?.parse().ok()?;
jiff::Timestamp::from_second(secs).ok().map(UtcDateTime)
}
pub fn is_mfa_fresh(session: &Session, now: UtcDateTime, max_age: Duration) -> bool {
mfa_verified_at(session).is_some_and(|at| at <= now && at.distance_duration(&now) <= max_age)
}
pub fn with_mfa_claims(principal: AuthPrincipal, session: &Session) -> AuthPrincipal {
let Some(at) = mfa_verified_at(session) else {
return principal;
};
let mut amr = match principal.claims().get(AMR_CLAIM) {
Some(Value::Array(methods)) => methods.clone(),
Some(other) => vec![other.clone()],
None => Vec::new(),
};
for method in ["otp", "mfa"] {
let method = Value::from(method);
if !amr.contains(&method) {
amr.push(method);
}
}
principal
.with_claim(AMR_CLAIM, Value::Array(amr))
.with_claim(MFA_AT_CLAIM, at.0.as_second())
}
pub fn require_mfa() -> ClaimPolicy<ClaimContains> {
claim_contains(AMR_CLAIM, "mfa")
}
const RECOVERY_ALPHABET: &[u8; 32] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
const RECOVERY_LEN: usize = 10;
#[derive(Clone, PartialEq, Eq)]
pub struct RecoveryCode(String);
impl RecoveryCode {
fn generate() -> Result<Self, TotpError> {
let mut bytes = [0_u8; RECOVERY_LEN];
random(&mut bytes)?;
let symbols: Vec<char> = bytes
.iter()
.map(|b| char::from(RECOVERY_ALPHABET[usize::from(b & 31)]))
.collect();
let (a, b) = symbols.split_at(RECOVERY_LEN / 2);
Ok(Self(format!(
"{}-{}",
a.iter().collect::<String>(),
b.iter().collect::<String>()
)))
}
#[inline]
pub fn expose(&self) -> &str {
&self.0
}
}
impl fmt::Debug for RecoveryCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("RecoveryCode(<redacted>)")
}
}
fn normalize(code: &str) -> String {
code
.chars()
.filter(|c| c.is_ascii_alphanumeric())
.map(|c| c.to_ascii_uppercase())
.collect()
}
#[derive(Clone, Debug)]
pub struct GeneratedRecoveryCodes {
pub codes: Vec<RecoveryCode>,
pub hashes: Vec<PasswordHash>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Redemption {
Redeemed {
remaining: Vec<PasswordHash>,
},
Invalid,
}
#[derive(Clone, Debug, Default)]
pub struct RecoveryCodes {
hasher: PasswordHasher,
}
impl RecoveryCodes {
pub fn new(hasher: PasswordHasher) -> Self {
Self { hasher }
}
pub fn generate(&self, count: usize) -> Effect<GeneratedRecoveryCodes, TotpError> {
let codes = match (0..count)
.map(|_| RecoveryCode::generate())
.collect::<Result<Vec<_>, _>>()
{
Ok(codes) => codes,
Err(e) => return fail(e),
};
let mut hashes: Effect<Vec<PasswordHash>, PasswordError> = succeed(Vec::new());
for code in &codes {
let (hasher, plain) = (self.hasher.clone(), normalize(code.expose()));
hashes = hashes.flat_map(move |mut acc| {
hasher.hash(plain).map(move |h| {
acc.push(h);
acc
})
});
}
hashes
.map(move |hashes| GeneratedRecoveryCodes { codes, hashes })
.map_err(TotpError::Password)
}
pub fn redeem(&self, code: &str, stored: Vec<PasswordHash>) -> Effect<Redemption, TotpError> {
let plain = normalize(code);
let mut found: Effect<Option<usize>, PasswordError> = succeed(None);
for (i, hash) in stored.iter().enumerate() {
let (hasher, plain, hash) = (self.hasher.clone(), plain.clone(), hash.clone());
found = found.flat_map(move |prev| {
hasher
.verify(plain, &hash)
.map(move |v| prev.or(v.is_valid().then_some(i)))
});
}
found
.map(move |hit| match hit {
Some(i) => {
let mut remaining = stored;
remaining.remove(i);
Redemption::Redeemed { remaining }
}
None => Redemption::Invalid,
})
.map_err(TotpError::Password)
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use std::time::Instant;
use id_effect::{Cause, Exit};
use id_effect_authz::Principal;
use super::*;
use crate::session::{MemorySessionStore, SessionConfig, SessionId};
fn run<A: Send + 'static, E: Send + fmt::Debug + 'static>(eff: Effect<A, E>) -> Result<A, E> {
match eff.block_on() {
Exit::Success(a) => Ok(a),
Exit::Failure(Cause::Fail(e)) => Err(e),
Exit::Failure(c) => panic!("unexpected cause: {c:?}"),
}
}
fn at(secs: i64) -> UtcDateTime {
UtcDateTime::unsafe_make(secs * 1000)
}
fn fixture() -> (Totp, MemoryTotpStepStore, WallClock) {
let steps = MemoryTotpStepStore::new();
let clock = WallClock::anchored(Instant::now(), at(1_800_000_000));
let totp = Totp::new(TotpConfig::new("Acme"), steps.clone()).with_clock(clock.clone());
(totp, steps, clock)
}
fn session() -> Session {
Session::from_parts(
SessionId::generate().unwrap(),
"u",
BTreeMap::new(),
at(0),
at(0),
at(1),
at(1),
)
}
#[test]
fn secrets_are_160_random_bits_and_round_trip_base32() {
let a = TotpSecret::generate().unwrap();
let b = TotpSecret::generate().unwrap();
assert_ne!(a, b);
assert_eq!(a.as_bytes().len(), 20);
assert_eq!(a.to_base32().len(), 32);
assert_eq!(TotpSecret::from_base32(&a.to_base32()).unwrap(), a);
assert!(matches!(
TotpSecret::from_base32("not base32!"),
Err(TotpError::Config(_))
));
assert_eq!(format!("{a:?}"), "TotpSecret(<redacted>)");
}
#[test]
fn invalid_parameters_are_config_errors() {
let mut config = TotpConfig::new("Acme");
config.digits = 5;
let totp = Totp::new(config, MemoryTotpStepStore::new());
let secret = TotpSecret::generate().unwrap();
assert!(matches!(totp.enrol("a"), Err(TotpError::Config(_))));
assert!(matches!(
totp.code_at(&secret, at(0)),
Err(TotpError::Config(_))
));
assert!(matches!(
run(totp.verify("u", &secret, "123456")),
Err(TotpError::Config(_))
));
let (ok, _, _) = fixture();
assert!(matches!(ok.uri(&secret, "a:b"), Err(TotpError::Config(_))));
assert!(matches!(
ok.code_at(&TotpSecret::from_bytes(&[1; 10]), at(0)),
Err(TotpError::Config(_))
));
}
#[test]
fn verify_tolerates_spaces_and_rejects_garbage() {
let (totp, steps, _) = fixture();
let secret = TotpSecret::generate().unwrap();
let code = totp.code_at(&secret, at(1_800_000_000)).unwrap();
let spaced = format!("{} {}", &code[..3], &code[3..]);
assert_eq!(
run(totp.verify("u", &secret, &spaced)),
Ok(TotpVerification::Valid)
);
assert_eq!(
run(totp.verify("u", &secret, "abcdef")),
Ok(TotpVerification::Invalid)
);
assert_eq!(steps.last_step("u"), Some(1_800_000_000 / 30));
assert!(!TotpVerification::Invalid.is_valid());
assert!(format!("{steps:?}").contains("MemoryTotpStepStore"));
}
#[test]
fn h6_totp_locks_after_five_failures() {
let (totp, _, clock) = fixture();
let secret = TotpSecret::generate().unwrap();
let now = 1_800_000_000;
let wrong = |offset: i64| {
totp.code_at(&secret, at(now - 3_600 + offset)).unwrap()
};
for i in 0..5 {
assert_eq!(
run(totp.verify("u", &secret, &wrong(i))),
Ok(TotpVerification::Invalid)
);
}
let right = totp.code_at(&secret, at(now)).unwrap();
let Err(TotpError::Locked { retry_after }) = run(totp.verify("u", &secret, &right)) else {
panic!("the sixth attempt must be locked")
};
assert!(
retry_after <= DEFAULT_LOCKOUT && retry_after > DEFAULT_LOCKOUT - Duration::from_secs(5)
);
assert!(
TotpError::Locked { retry_after }
.to_string()
.contains("too many wrong codes")
);
assert_eq!(
run(totp.verify("v", &secret, &right)),
Ok(TotpVerification::Valid)
);
assert!(matches!(
run(totp.clone().verify("u", &secret, &right)),
Err(TotpError::Locked { .. })
));
clock.jump(DEFAULT_LOCKOUT);
let later = totp.code_at(&secret, at(now + 900)).unwrap();
assert_eq!(
run(totp.verify("u", &secret, &later)),
Ok(TotpVerification::Valid)
);
for i in 0..4 {
assert_eq!(
run(totp.verify("u", &secret, &wrong(i))),
Ok(TotpVerification::Invalid)
);
}
assert_eq!(
run(totp.verify("u", &secret, &wrong(9))),
Ok(TotpVerification::Invalid),
"the count started over after the valid code"
);
}
#[test]
fn lockout_can_be_tuned_moved_or_dropped() {
let (totp, _, _) = fixture();
let secret = TotpSecret::generate().unwrap();
let wrong = totp.code_at(&secret, at(1_000)).unwrap();
let strict = totp
.clone()
.with_failure_store(MemoryThrottleStore::new())
.with_lockout(1, Duration::from_secs(60));
assert_eq!(
run(strict.verify("u", &secret, &wrong)),
Ok(TotpVerification::Invalid)
);
assert!(matches!(
run(strict.verify("u", &secret, &wrong)),
Err(TotpError::Locked { .. })
));
let open = strict.dangerous_without_lockout();
for _ in 0..10 {
assert_eq!(
run(open.verify("u", &secret, &wrong)),
Ok(TotpVerification::Invalid)
);
}
let back = open.with_failure_store(MemoryThrottleStore::new());
assert_eq!(
run(back.verify("u", &secret, &wrong)),
Ok(TotpVerification::Invalid)
);
let relocked = back
.dangerous_without_lockout()
.with_lockout(0, Duration::from_secs(60));
assert_eq!(
run(relocked.verify("u", &secret, &wrong)),
Ok(TotpVerification::Invalid)
);
assert!(matches!(
run(relocked.verify("u", &secret, &wrong)),
Err(TotpError::Locked { .. })
));
}
#[test]
fn step_store_failures_surface() {
let (totp, steps, _) = fixture();
let secret = TotpSecret::generate().unwrap();
let code = totp.code_at(&secret, at(1_800_000_000)).unwrap();
steps.fail_with(Some("down"));
assert_eq!(
run(totp.verify("u", &secret, &code)),
Err(TotpError::Storage("down".into()))
);
steps.fail_with(None);
assert_eq!(
run(totp.verify("u", &secret, &code)),
Ok(TotpVerification::Valid)
);
}
#[test]
fn errors_display() {
let cases = [
(TotpError::Config("x".into()), "configuration: x"),
(TotpError::Random("x".into()), "generation: x"),
(TotpError::Storage("x".into()), "storage: x"),
(
TotpError::from(PasswordError::Blocking),
"recovery code hashing",
),
];
for (e, text) in cases {
assert!(e.to_string().contains(text), "{e}");
}
}
#[test]
fn before_the_epoch_counts_as_zero() {
assert_eq!(unix_secs(UtcDateTime::unsafe_make(-5_000)), 0);
}
#[test]
fn mfa_marks_and_claims() {
let mut s = session();
assert_eq!(mfa_verified_at(&s), None);
let p = AuthPrincipal::new("u");
assert_eq!(with_mfa_claims(p.clone(), &s), p);
mark_mfa(&mut s, at(1_000));
assert_eq!(mfa_verified_at(&s), Some(at(1_000)));
assert!(is_mfa_fresh(&s, at(1_300), Duration::from_secs(300)));
assert!(!is_mfa_fresh(&s, at(1_301), Duration::from_secs(300)));
assert!(!is_mfa_fresh(&s, at(999), Duration::from_secs(300)));
let p = with_mfa_claims(AuthPrincipal::new("u").with_claim(AMR_CLAIM, "pwd"), &s);
assert_eq!(
p.claim(AMR_CLAIM),
Some(&serde_json::json!(["pwd", "otp", "mfa"]))
);
assert_eq!(p.claim(MFA_AT_CLAIM), Some(&serde_json::json!(1_000)));
let twice = with_mfa_claims(p.clone(), &s);
assert_eq!(twice.claim(AMR_CLAIM), p.claim(AMR_CLAIM));
s.set_attr(MFA_VERIFIED_AT_ATTR, "garbage");
assert_eq!(mfa_verified_at(&s), None);
}
#[test]
fn step_up_marks_and_rotates_the_session() {
let (totp, _, clock) = fixture();
let sessions =
Sessions::new(MemorySessionStore::new(), SessionConfig::default()).with_clock(clock.clone());
let s = run(sessions.login("u", None)).unwrap();
clock.jump(Duration::from_secs(5));
let up = run(totp.step_up(&sessions, s.clone()))
.unwrap()
.expect("live");
assert_ne!(up.id(), s.id());
assert_eq!(mfa_verified_at(&up), Some(at(1_800_000_005)));
assert_eq!(run(sessions.load(s.id())).unwrap(), None);
assert!(format!("{totp:?}").contains("Acme"));
assert_eq!(totp.config().digits, 6);
}
#[test]
fn recovery_codes_have_the_expected_shape() {
let code = RecoveryCode::generate().unwrap();
let text = code.expose();
assert_eq!(text.len(), 11);
assert_eq!(&text[5..6], "-");
assert!(
normalize(text)
.bytes()
.all(|b| RECOVERY_ALPHABET.contains(&b))
);
assert_eq!(format!("{code:?}"), "RecoveryCode(<redacted>)");
assert_eq!(normalize(" abcde-fghij "), "ABCDEFGHIJ");
}
#[test]
fn h6_dangerous_without_lockout_is_recorded() {
let (totp, _, _) = fixture();
#[allow(deprecated)]
let shim = totp.clone().without_lockout_dangerous();
assert!(shim.lockout.is_none());
let open = totp.dangerous_without_lockout();
assert!(open.lockout.is_none());
let found = id_effect::observability::posture::dangerous_opt_outs();
let (_, reopens) = found
.iter()
.find(|(id, _)| *id == DANGEROUS_WITHOUT_LOCKOUT)
.expect("recorded");
assert!(reopens.starts_with("H6:"));
}
#[test]
fn m18_memory_step_store_is_reported_as_memory_only() {
let _ = MemoryTotpStepStore::default();
let found = id_effect::observability::posture::dangerous_opt_outs();
let (_, reopens) = found
.iter()
.find(|(id, _)| *id == MEMORY_TOTP_STEP_STORE_POSTURE)
.expect("recorded");
assert!(reopens.starts_with("M18: TOTP replay state is memory-only"));
}
#[cfg(feature = "pg")]
#[test]
fn the_pg_step_store_consumes_with_one_compare_and_set() {
use id_effect_sql::{SqlClient, SqlValue, TestSqlClient};
let client = TestSqlClient::new();
let store = PgTotpStepStore::new(client.clone());
assert!(matches!(
run(store.consume("u", 7)),
Err(TotpError::Storage(_))
));
assert!(matches!(
run(store.ensure_schema()),
Err(TotpError::Storage(_))
));
run(client.connect()).unwrap();
assert_eq!(run(store.consume("u", 7)), Ok(false), "no row affected");
client.script_execute(pg::CONSUME, 1);
assert_eq!(run(store.consume("u", 8)), Ok(true));
let (sql, params) = &client.recorded_executes()[1];
assert_eq!(sql, pg::CONSUME);
assert_eq!(params, &[SqlValue::Text("u".into()), SqlValue::I64(8)]);
assert!(matches!(
run(store.consume("u", u64::MAX)),
Err(TotpError::Storage(m)) if m.contains("out of range")
));
run(store.ensure_schema()).unwrap();
let ddl: Vec<String> = client
.recorded_executes()
.into_iter()
.skip(2)
.map(|(sql, _)| sql)
.collect();
assert_eq!(ddl.len(), 3, "{ddl:?}");
assert!(ddl[0].starts_with("CREATE TABLE IF NOT EXISTS id_effect_totp_steps"));
assert!(format!("{store:?}").contains("id_effect_totp_steps"));
}
}