Skip to main content

SessionBag

Struct SessionBag 

Source
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

Source

pub fn new() -> Session

A fresh, empty session with a new id.

Source

pub fn restore(id: impl Into<String>, data: SessionData) -> Session

A session loaded from a store.

Source

pub fn id(&self) -> String

The session id, as it appears in the cookie.

Source

pub fn is_dirty(&self) -> bool

Whether anything has changed and the session needs writing back.

Source

pub fn get<T>(&self, key: &str) -> Option<T>

A value, deserialised.

Source

pub fn string(&self, key: &str) -> Option<String>

A value as a string, if it is one.

Source

pub fn value(&self, key: &str) -> Option<Value>

The raw value.

Source

pub fn has(&self, key: &str) -> bool

Whether the key is present and not null.

Source

pub fn all(&self) -> Map<String, Value>

Every value.

Source

pub fn len(&self) -> usize

How many values are held.

Source

pub fn is_empty(&self) -> bool

Whether the session holds nothing.

Source

pub fn put( &self, key: impl Into<String>, value: impl Serialize, ) -> Result<(), Error>

Store a value.

Source

pub fn pull<T>(&self, key: &str) -> Option<T>

Read a value and remove it in one step.

Source

pub fn forget(&self, key: &str)

Remove a value.

Source

pub fn flush(&self)

Remove every value, keeping the id.

Source

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.

Source

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.

Source

pub fn reflash(&self)

Keep every flashed value for one further request.

Source

pub fn token(&self) -> String

The CSRF token, minted on first use.

Source

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.

Source

pub fn invalidate(&self)

Give the session a new id and throw its contents away. Logging out.

Source

pub fn superseded_ids(&self) -> Vec<String>

Ids this session has left behind, for the store to delete.

Source

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§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Session

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Session

Deliberately opaque: a session holds whatever the application put in it, which in practice includes user ids and CSRF tokens.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Session

Source§

fn default() -> Session

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Actor for T
where T: Send + Sync + 'static,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more