11//! Configuration options to tune the behaviour of [`SessionMiddleware`].
22
33use actix_web:: cookie:: { time:: Duration , Key , SameSite } ;
4+ use derive_more:: From ;
45
56use crate :: { storage:: SessionStore , SessionMiddleware } ;
67
78/// Determines what type of session cookie should be used and how its lifecycle should be managed.
89///
910/// Used by [`SessionMiddlewareBuilder::session_lifecycle`].
10- #[ derive( Debug , Clone ) ]
11+ #[ derive( Debug , Clone , From ) ]
1112#[ non_exhaustive]
1213pub enum SessionLifecycle {
1314 /// The session cookie will expire when the current browser session ends.
@@ -27,25 +28,16 @@ pub enum SessionLifecycle {
2728 PersistentSession ( PersistentSession ) ,
2829}
2930
30- impl From < BrowserSession > for SessionLifecycle {
31- fn from ( session : BrowserSession ) -> Self {
32- Self :: BrowserSession ( session)
33- }
34- }
35-
36- impl From < PersistentSession > for SessionLifecycle {
37- fn from ( session : PersistentSession ) -> Self {
38- Self :: PersistentSession ( session)
39- }
40- }
41-
4231/// A [session lifecycle](SessionLifecycle) strategy where the session cookie expires when the
4332/// browser's current session ends.
4433///
4534/// When does a browser session end? It depends on the browser. Chrome, for example, will often
4635/// continue running in the background when the browser is closed—session cookies are not deleted
4736/// and they will still be available when the browser is opened again. Check the documentation of
4837/// the browsers you are targeting for up-to-date information.
38+ ///
39+ /// Due to its `Into<SessionLifecycle>` implementation, a `BrowserSession` can be passed directly
40+ /// to [`SessionMiddlewareBuilder::session_lifecycle()`].
4941#[ derive( Debug , Clone ) ]
5042pub struct BrowserSession {
5143 state_ttl : Duration ,
@@ -103,6 +95,26 @@ impl Default for BrowserSession {
10395/// Persistent cookies have a pre-determined expiration, specified via the `Max-Age` or `Expires`
10496/// attribute. They do not disappear when the current browser session ends.
10597///
98+ /// Due to its `Into<SessionLifecycle>` implementation, a `PersistentSession` can be passed directly
99+ /// to [`SessionMiddlewareBuilder::session_lifecycle()`].
100+ ///
101+ /// # Examples
102+ /// ```
103+ /// use actix_web::cookie::time::Duration;
104+ /// use actix_session::SessionMiddleware;
105+ /// use actix_session::config::{PersistentSession, TtlExtensionPolicy};
106+ ///
107+ /// const SECS_IN_WEEK: i64 = 60 * 60 * 24 * 7;
108+ ///
109+ /// // a session lifecycle with a time-to-live (expiry) of 1 week and default extension policy
110+ /// PersistentSession::default().session_ttl(Duration::seconds(SECS_IN_WEEK));
111+ ///
112+ /// // a session lifecycle with the default time-to-live (expiry) and a custom extension policy
113+ /// PersistentSession::default()
114+ /// // this policy causes the session state's TTL to be refreshed on every request
115+ /// .session_ttl_extension_policy(TtlExtensionPolicy::OnEveryRequest);
116+ /// ```
117+ ///
106118/// [persistent]: https://www.whitehatsec.com/glossary/content/persistent-session-cookie
107119#[ derive( Debug , Clone ) ]
108120pub struct PersistentSession {
@@ -113,10 +125,10 @@ pub struct PersistentSession {
113125impl PersistentSession {
114126 /// Specifies how long the session cookie should live.
115127 ///
116- /// Defaults to 1 day if left unspecified.
117- ///
118128 /// The session TTL is also used as the TTL for the session state in the storage backend.
119129 ///
130+ /// Defaults to 1 day.
131+ ///
120132 /// A persistent session can live more than the specified TTL if the TTL is extended.
121133 /// See [`session_ttl_extension_policy`](Self::session_ttl_extension_policy) for more details.
122134 pub fn session_ttl ( mut self , session_ttl : Duration ) -> Self {
@@ -127,7 +139,7 @@ impl PersistentSession {
127139 /// Determines under what circumstances the TTL of your session should be extended.
128140 /// See [`TtlExtensionPolicy`] for more details.
129141 ///
130- /// Defaults to [`TtlExtensionPolicy::OnStateChanges`] if left unspecified .
142+ /// Defaults to [`TtlExtensionPolicy::OnStateChanges`].
131143 pub fn session_ttl_extension_policy (
132144 mut self ,
133145 ttl_extension_policy : TtlExtensionPolicy ,
@@ -148,23 +160,23 @@ impl Default for PersistentSession {
148160
149161/// Configuration for which events should trigger an extension of the time-to-live for your session.
150162///
151- /// If you are using a [`BrowserSession`], `TtlExtensionPolicy` controls how often the TTL of
152- /// the session state should be refreshed. The browser is in control of the lifecycle of the
153- /// session cookie.
163+ /// If you are using a [`BrowserSession`], `TtlExtensionPolicy` controls how often the TTL of the
164+ /// session state should be refreshed. The browser is in control of the lifecycle of the session
165+ /// cookie.
154166///
155- /// If you are using a [`PersistentSession`], `TtlExtensionPolicy` controls both the expiration
156- /// of the session cookie and the TTL of the session state.
167+ /// If you are using a [`PersistentSession`], `TtlExtensionPolicy` controls both the expiration of
168+ /// the session cookie and the TTL of the session state on the storage backend .
157169#[ derive( Debug , Clone ) ]
158170#[ non_exhaustive]
159171pub enum TtlExtensionPolicy {
160172 /// The TTL is refreshed every time the server receives a request associated with a session.
161173 ///
162174 /// # Performance impact
163- /// Refreshing the TTL on every request is not free.
164- /// It implies a refresh of the TTL on the session state. This translates into a request over
165- /// the network if you are using a remote system as storage backend (e.g. Redis).
166- /// This impacts both the total load on your storage backend (i.e. number of
167- /// queries it has to handle) and the latency of the requests served by your server.
175+ /// Refreshing the TTL on every request is not free. It implies a refresh of the TTL on the
176+ /// session state. This translates into a request over the network if you are using a remote
177+ /// system as storage backend (e.g. Redis). This impacts both the total load on your storage
178+ /// backend (i.e. number of queries it has to handle) and the latency of the requests served by
179+ /// your server.
168180 OnEveryRequest ,
169181
170182 /// The TTL is refreshed every time the session state changes or the session key is renewed.
@@ -197,8 +209,7 @@ pub(crate) const fn default_ttl_extension_policy() -> TtlExtensionPolicy {
197209 TtlExtensionPolicy :: OnStateChanges
198210}
199211
200- /// A fluent builder to construct a [`SessionMiddleware`] instance with custom configuration
201- /// parameters.
212+ /// A fluent, customized [`SessionMiddleware`] builder.
202213#[ must_use]
203214pub struct SessionMiddlewareBuilder < Store : SessionStore > {
204215 storage_backend : Store ,
@@ -236,6 +247,22 @@ impl<Store: SessionStore> SessionMiddlewareBuilder<Store> {
236247 /// Check out [`SessionLifecycle`]'s documentation for more details on the available options.
237248 ///
238249 /// Default is [`SessionLifecycle::BrowserSession`].
250+ ///
251+ /// # Examples
252+ /// ```
253+ /// use actix_web::cookie::{Key, time::Duration};
254+ /// use actix_session::{SessionMiddleware, config::PersistentSession};
255+ /// use actix_session::storage::CookieSessionStore;
256+ ///
257+ /// const SECS_IN_WEEK: i64 = 60 * 60 * 24 * 7;
258+ ///
259+ /// // creates a session middleware with a time-to-live (expiry) of 1 week
260+ /// SessionMiddleware::builder(CookieSessionStore::default(), Key::from(&[0; 64]))
261+ /// .session_lifecycle(
262+ /// PersistentSession::default().session_ttl(Duration::seconds(SECS_IN_WEEK))
263+ /// )
264+ /// .build();
265+ /// ```
239266 pub fn session_lifecycle < S : Into < SessionLifecycle > > ( mut self , session_lifecycle : S ) -> Self {
240267 match session_lifecycle. into ( ) {
241268 SessionLifecycle :: BrowserSession ( BrowserSession {
0 commit comments