pub struct Session { /* private fields */ }Expand description
A request’s session.
Cheap to clone — every clone is the same underlying bag, which is what lets the middleware hold one while the handler writes through another.
Reached from a handler with
request.session(), which is Some
exactly when the StartSession middleware ran.
Implementations§
Source§impl Session
impl Session
Sourcepub fn restore(id: impl Into<String>, data: SessionData) -> Session
pub fn restore(id: impl Into<String>, data: SessionData) -> Session
A session loaded from a store.
Sourcepub fn is_dirty(&self) -> bool
pub fn is_dirty(&self) -> bool
Whether anything has changed and the session needs writing back.
Sourcepub fn get<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
pub fn get<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
A value, deserialised.
Sourcepub fn put(
&self,
key: impl Into<String>,
value: impl Serialize,
) -> Result<(), Error>
pub fn put( &self, key: impl Into<String>, value: impl Serialize, ) -> Result<(), Error>
Store a value.
Sourcepub fn pull<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
pub fn pull<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
Read a value and remove it in one step.
Sourcepub fn flash(
&self,
key: impl Into<String>,
value: impl Serialize,
) -> Result<(), Error>
pub fn flash( &self, key: impl Into<String>, value: impl Serialize, ) -> Result<(), Error>
Store a value for the next request only.
The redirect-then-show-a-message pattern: the value survives exactly one further request and is then deleted, without anything having to remember to clean it up.
Sourcepub fn keep(&self, keys: &[&str])
pub fn keep(&self, keys: &[&str])
Keep the named flashed values for one further request.
What a redirect chain needs: without it, a value flashed before a redirect that itself redirects is gone before anything renders it.
Sourcepub fn regenerate(&self)
pub fn regenerate(&self)
Give the session a new id, keeping its contents.
Call this on login. Without it, an attacker who can set a victim’s session cookie before they authenticate ends up holding a cookie for their authenticated session — session fixation, and it is invisible in testing because everything works.
Sourcepub fn invalidate(&self)
pub fn invalidate(&self)
Give the session a new id and throw its contents away. Logging out.
Sourcepub fn superseded_ids(&self) -> Vec<String>
pub fn superseded_ids(&self) -> Vec<String>
Ids this session has left behind, for the store to delete.
Sourcepub fn age_and_take(&self) -> SessionData
pub fn age_and_take(&self) -> SessionData
Age the flash data and produce what should be written back.
Called once by the middleware, after the handler has run: values flashed by a previous request are dropped unless they were kept, and values flashed by this one become the next request’s.