use chrono::{DateTime, Utc};
use dashmap::{DashMap, mapref::entry::Entry};
#[derive(Debug, Default)]
pub struct SamlReplayCache {
seen: DashMap<String, DateTime<Utc>>,
}
impl SamlReplayCache {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn check_and_record(
&self,
assertion_id: &str,
expires_at: DateTime<Utc>,
now: DateTime<Utc>,
) -> bool {
self.seen.retain(|_, expiry| *expiry > now);
match self.seen.entry(assertion_id.to_owned()) {
Entry::Occupied(_) => false,
Entry::Vacant(slot) => {
slot.insert(expires_at);
true
},
}
}
#[must_use]
pub fn len(&self) -> usize {
self.seen.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.seen.is_empty()
}
}