Skip to main content

RotatingNonceCache

Struct RotatingNonceCache 

Source
pub struct RotatingNonceCache { /* private fields */ }
Expand description

Two-bucket rotating nonce cache for WS-Security replay detection.

Buckets rotate every half_window seconds (default: 150 s → 300 s total window). A nonce present in either bucket is considered a replay and rejected.

When the current bucket reaches max_entries a forced rotation occurs so the bucket is replaced by an empty one — legitimate traffic continues but further nonces from the previous bucket will no longer be detected as replays. This is preferable to unbounded growth; the trade-off is documented in check_and_insert.

§Thread-safety

check_and_insert takes &mut self, so RotatingNonceCache itself is not thread-safe. Wrap it in a tokio::sync::Mutex (or std::sync::Mutex for sync contexts) before sharing across async tasks:

use tokio::sync::Mutex;
use soap_server::RotatingNonceCache;

let cache = Arc::new(Mutex::new(RotatingNonceCache::new(150)));
// Inside an async handler:
let mut cache = cache.lock().await;
cache.check_and_insert(&nonce)?;

The soap-server [SoapService] handles this internally — consumers using [validate_username_token] via the server builder do not need to manage the cache directly. Consider interior-mutability refactoring in v0.2.

Implementations§

Source§

impl RotatingNonceCache

Source

pub fn new(half_window_secs: u64) -> Self

Create a new cache with the given time-window and default entry cap (100 000).

Source

pub fn with_max_entries(half_window_secs: u64, max_entries: usize) -> Self

Create a new cache with explicit time-window and per-bucket entry cap.

Source

pub fn check_and_insert(&mut self, nonce: &str) -> Result<(), SoapFault>

Check nonce for replay and insert if not seen. Returns Err on replay.

If the current bucket is at capacity (max_entries), a forced rotation is performed before inserting: the current bucket becomes the previous bucket and a fresh empty bucket is started. This bounds memory use at the cost of a narrow window where very recently-rotated nonces are no longer tracked — a DoS flood that exhausts the cap is still bounded, and normal traffic is not disrupted.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more