pub struct SessionBag { /* 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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Session
impl RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl UnwindSafe for Session
Blanket Implementations§
impl<T> Actor for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Event for T
impl<T> Event for T
Source§fn event_name() -> &'static strwhere
Self: Sized,
fn event_name() -> &'static strwhere
Self: Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more