Skip to main content

hyperlane_plugin_session/
struct.rs

1use super::*;
2
3/// Configuration controlling cookie attributes, signing, TTL, and capacity for [`SessionPlugin`].
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct SessionConfig {
6    /// Cookie name carrying the session token (default `"hyperlane_sid"`).
7    pub(crate) cookie_name: String,
8    /// Cookie `Path` attribute (default `"/"`).
9    pub(crate) cookie_path: String,
10    /// Optional cookie `Domain` attribute.
11    #[get(type(clone))]
12    pub(crate) cookie_domain: Option<String>,
13    /// Cookie `SameSite` policy (`"Lax"`, `"Strict"`, `"None"`).
14    pub(crate) same_site: String,
15    /// Sets `HttpOnly` flag on session cookies when `true`.
16    #[get(type(copy))]
17    pub(crate) http_only: bool,
18    /// Sets `Secure` flag on session cookies when `true`.
19    #[get(type(copy))]
20    pub(crate) secure: bool,
21    /// Session idle TTL in seconds (also emitted as cookie `Max-Age`).
22    #[get(type(copy))]
23    pub(crate) ttl_secs: u64,
24    /// Maximum active sessions allowed in the built-in [`InMemorySessionStore`].
25    #[get(type(copy))]
26    pub(crate) max_sessions: usize,
27    /// Optional HMAC-SHA256 secret signing cookie values (`<sid>.<mac>`).
28    #[get(type(clone))]
29    pub(crate) signing_secret: Option<Vec<u8>>,
30}
31
32/// Mutable per-request session state tracking dirty writes, regeneration, and destruction.
33#[derive(Clone, Debug)]
34pub(crate) struct SessionState {
35    /// Current session identifier.
36    pub(crate) id: String,
37    /// Previous session identifier deleted from the store after [`SessionHandle::regenerate_id`].
38    pub(crate) previous_id: Option<String>,
39    /// Key-value session attributes.
40    pub(crate) values: HashMap<String, String>,
41    /// `true` when loaded from an existing persisted session.
42    pub(crate) existed: bool,
43    /// `true` when modified during this request (drives zero-write read optimization).
44    pub(crate) dirty: bool,
45    /// `true` when [`SessionHandle::destroy`] was called.
46    pub(crate) destroyed: bool,
47}
48
49/// Thread-safe handle stored in Hyperlane [`Context`] allowing handlers to read and mutate the active session.
50#[derive(Clone, Debug)]
51pub struct SessionHandle {
52    /// Protected per-request session state.
53    pub(crate) state: Arc<Mutex<SessionState>>,
54}
55
56/// Internal record stored inside each shard of [`InMemorySessionStore`].
57#[derive(Clone)]
58pub(crate) struct StoredSession {
59    /// Persisted key-value map.
60    pub(crate) values: HashMap<String, String>,
61    /// Expiration Unix timestamp in seconds.
62    pub(crate) expires_at_secs: u64,
63}
64
65/// Shared interior state of [`InMemorySessionStore`].
66pub(crate) struct InMemorySessionStoreInner {
67    /// Maximum tracked concurrent sessions across all shards.
68    pub(crate) max_sessions: usize,
69    /// Lock-free active session counter.
70    pub(crate) count: AtomicUsize,
71    /// 64-way lock-striped session tables.
72    pub(crate) shards: SessionShardArray,
73}
74
75/// High-concurrency 64-way sharded bounded in-memory [`SessionStore`].
76#[derive(Clone)]
77pub struct InMemorySessionStore {
78    /// Shared sharded store state.
79    pub(crate) inner: Arc<InMemorySessionStoreInner>,
80}
81
82/// Process-wide [`SessionPlugin`] singleton storing [`SessionConfig`] and a pluggable [`SessionStore`].
83#[derive(Clone)]
84pub struct SessionPlugin {
85    /// Cookie, signing, and TTL configuration.
86    pub(crate) config: SessionConfig,
87    /// Backing session store.
88    pub(crate) store: SharedSessionStore,
89}
90
91/// Opt-in request middleware loading the incoming session cookie into [`SessionHandle`].
92#[derive(Clone, Copy, Debug, Default)]
93#[hyperlane::request_middleware(-85)]
94pub struct SessionRequestHook;
95
96/// Opt-in response middleware persisting modified/regenerated/destroyed sessions and emitting `Set-Cookie`.
97#[derive(Clone, Copy, Debug, Default)]
98#[hyperlane::response_middleware(-70)]
99pub struct SessionResponseHook;