Skip to main content

SessionStore

Trait SessionStore 

Source
pub trait SessionStore:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &str;
    fn read<'a>(
        &'a self,
        id: &'a str,
    ) -> BoxFuture<'a, Result<Option<SessionData>>>;
    fn write<'a>(
        &'a self,
        id: &'a str,
        data: &'a SessionData,
    ) -> BoxFuture<'a, Result<()>>;
    fn destroy<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<()>>;

    // Provided methods
    fn gc(&self) -> BoxFuture<'_, Result<u64>> { ... }
    fn is_client_side(&self) -> bool { ... }
    fn encode(&self, id: &str, data: &SessionData) -> Result<String> { ... }
    fn decode(&self, value: &str) -> Result<(String, Option<SessionData>)> { ... }
}
Expand description

Where session data is kept between requests.

The four required methods are the server-side shape: an id in the cookie, the data somewhere else. encode and decode exist so a store can instead put the whole session in the cookie — see CookieSessionStore — without the middleware needing to know which kind it has.

Required Methods§

Source

fn name(&self) -> &str

A label for diagnostics — "memory", "database", "cache", "cookie".

Source

fn read<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<Option<SessionData>>>

The data for this id, or None if it is unknown or expired.

Source

fn write<'a>( &'a self, id: &'a str, data: &'a SessionData, ) -> BoxFuture<'a, Result<()>>

Write the data back, refreshing its expiry.

Source

fn destroy<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<()>>

Remove a session.

Provided Methods§

Source

fn gc(&self) -> BoxFuture<'_, Result<u64>>

Discard every expired session. Called periodically, not per request.

Source

fn is_client_side(&self) -> bool

Whether the session travels in the cookie rather than being stored here.

A client-side store’s read and write are never called by the middleware.

Source

fn encode(&self, id: &str, data: &SessionData) -> Result<String>

The cookie value carrying this session.

Server-side stores return the id, which is the default.

Source

fn decode(&self, value: &str) -> Result<(String, Option<SessionData>)>

Recover a session id — and, for a client-side store, its data — from a cookie value.

The default rejects anything that is not the shape generate_session_id produces. A cookie is client-supplied, so an id we did not mint is not worth a round trip, and letting a client choose its own id is how session fixation starts.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§