Skip to main content

SessionManager

Struct SessionManager 

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

Manages authenticated peer sessions with dual-index tracking.

Sessions are indexed by:

  • Session nonce (primary key, unique per session)
  • Identity key (secondary index, one-to-many)

This allows lookup by either nonce or identity key, with the identity key lookup returning the “best” session (preferring authenticated ones).

Implementations§

Source§

impl SessionManager

Source

pub fn new() -> Self

Create a new empty SessionManager with default TTL / seen-set cap.

Source

pub fn with_config(idle_ttl_ms: u64, seen_nonce_cap: usize) -> Self

Create a new empty SessionManager with an explicit idle TTL and per-session replay seen-set cap. Used by tests and by callers that want a tighter reaping policy.

Source

pub fn idle_ttl_ms(&self) -> u64

The configured idle TTL in milliseconds.

Source

pub fn add_session(&mut self, session: PeerSession)

Add a session to the manager.

Indexes by session_nonce (primary) and peer_identity_key (secondary). Does NOT overwrite existing sessions for the same identity key, allowing multiple concurrent sessions per peer.

Source

pub fn get_session(&self, nonce: &str) -> Option<&PeerSession>

Get a session by nonce (immutable reference).

Source

pub fn get_session_mut(&mut self, nonce: &str) -> Option<&mut PeerSession>

Get a session by nonce (mutable reference).

Source

pub fn get_sessions_for_identity(&self, identity_key: &str) -> Vec<&PeerSession>

Get all sessions for a given identity key.

Source

pub fn get_session_by_identifier( &self, identifier: &str, ) -> Option<&PeerSession>

Get the “best” session for an identity key (prefers authenticated).

Matches TS SDK SessionManager.getSession() behavior: if the identifier is a session nonce, returns that exact session. If it is an identity key, returns the best (authenticated preferred) session.

Source

pub fn has_session(&self, nonce: &str) -> bool

Check if a session exists for a given nonce.

Source

pub fn has_session_by_identifier(&self, identifier: &str) -> bool

Check if any session exists for a given identifier (nonce or identity key).

Source

pub fn update_session(&mut self, nonce: &str, session: PeerSession)

Replace a session at the given nonce.

Source

pub fn remove_session(&mut self, nonce: &str) -> Option<PeerSession>

Remove a session by nonce. Returns the removed session if found.

Also drops the session’s replay/activity metadata, freeing its seen-set.

Source

pub fn touch(&mut self, session_nonce: &str, now_ms: u64)

Record activity on an existing session, creating its metadata if absent and advancing last_used_ms to now_ms. No-op if the session is unknown.

Call this right after a handshake adds/promotes a session so that idle reaping has a baseline, and on every accepted message thereafter.

Source

pub fn is_expired(&self, session_nonce: &str, now_ms: u64) -> bool

True if the session has recorded activity AND has been idle longer than the configured TTL. A session with no recorded activity yet (freshly added, not touched) is treated as fresh (not expired).

Source

pub fn get_active_session( &self, identifier: &str, now_ms: u64, ) -> Option<&PeerSession>

Look up a session by identifier (nonce or identity key) honoring the idle TTL: returns None if the session is missing OR has gone idle past the TTL. The hot verify path uses this so a stale/captured yourNonce stops resolving a live session once the TTL lapses.

Source

pub fn mark_message_seen( &mut self, session_nonce: &str, message_nonce: &str, now_ms: u64, ) -> MarkSeen

Anti-replay check-and-insert for a single per-message nonce, scoped to a session. Atomic under &mut self (the caller’s brief write lock), so concurrent verifies of the SAME captured message resolve to exactly one MarkSeen::Fresh and the rest MarkSeen::Replay.

The effective replay key is (peer_identity_key, session_nonce, message_nonce): session_nonce selects the per-session set (which is itself bound to one peer identity via the handshake), and message_nonce is the fresh 32-byte random nonce an honest sender mints per outbound message — so a legit sender never collides and only a byte-replay repeats.

Advances last_used_ms and enforces the per-session FIFO cap. Only call AFTER the message signature has verified, so the set is never poisoned by unauthenticated input.

Source

pub fn reap_idle(&mut self, now_ms: u64) -> usize

Evict every session idle longer than the configured TTL, freeing each one’s replay seen-set. Returns the number reaped.

Intended to be called on the low-frequency handshake path (not the hot verify path), which bounds memory without serializing message verifies.

Trait Implementations§

Source§

impl Default for SessionManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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<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, 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.