Skip to main content

ReplayCache

Trait ReplayCache 

Source
pub trait ReplayCache {
    // Required method
    fn check_and_store(
        &mut self,
        key: ReplayKey,
        expires_at: SystemTime,
    ) -> Result<(), SamlError>;
}
Expand description

Caller-owned replay cache.

Implementations should atomically reject duplicate keys and return SamlError::ReplayDetected for duplicate SAML messages.

§Examples

This is a minimal in-memory example for a single process. Applications should use shared durable storage when multiple processes handle SAML responses.

use std::{collections::HashMap, time::{Duration, SystemTime}};

use saml_rs::{
    ReplayCache, ReplayKey, ReplayPolicy, SamlError, SamlValidationContext,
};

#[derive(Default)]
struct MinimalReplayCache {
    seen: HashMap<String, SystemTime>,
}

impl ReplayCache for MinimalReplayCache {
    fn check_and_store(
        &mut self,
        key: ReplayKey,
        expires_at: SystemTime,
    ) -> Result<(), SamlError> {
        let cache_key = key.cache_key();
        if self.seen.contains_key(&cache_key) {
            return Err(SamlError::ReplayDetected { key: cache_key });
        }
        self.seen.insert(cache_key, expires_at);
        Ok(())
    }
}

let now = SystemTime::now();
let mut cache = MinimalReplayCache::default();
let validation = SamlValidationContext::new(
    now,
    ReplayPolicy::RequireCache(&mut cache),
)
.with_replay_retention(Duration::from_secs(5 * 60));

Required Methods§

Source

fn check_and_store( &mut self, key: ReplayKey, expires_at: SystemTime, ) -> Result<(), SamlError>

Check whether key has already been seen, then store it until expires_at if it is new.

§Errors

Implementations should return SamlError::ReplayDetected for duplicate keys. They may also return storage-specific failures mapped to SamlError if cache access fails.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§