use crate::errors::*;
use crate::protocol::{check, AuthResult, Pending, Response};
use crate::random::challenge;
use crate::types::*;
use core::fmt;
#[derive(Clone, Copy)]
#[repr(C)]
pub struct TimedPending {
pub id: AliceId,
pub c: [u8; 32],
pub created_ms: u64,
}
impl fmt::Debug for TimedPending {
#[inline(always)]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
pub const CHALLENGE_TTL_MS: u64 = 30_000;
#[inline]
pub fn make_timed_challenge(id: AliceId, now_ms: u64) -> Result<TimedPending> {
Ok(TimedPending {
id,
c: challenge()?,
created_ms: now_ms,
})
}
#[inline]
#[must_use]
pub fn check_timed(p: &TimedPending, r: &Response, now_ms: u64, ttl_ms: u64) -> AuthResult {
if now_ms.saturating_sub(p.created_ms) > ttl_ms {
return AuthResult::Fail;
}
let pending = Pending { id: p.id, c: p.c };
check(&pending, r)
}