pub struct SessionLayer<Store> {
    store: Store,
    cookie_path: String,
    cookie_name: String,
    cookie_domain: Option<String>,
    persistence_policy: PersistencePolicy,
    session_ttl: Option<Duration>,
    same_site_policy: SameSite,
    http_only: bool,
    secure: bool,
    key: Key,
}
Expand description

Layer that provides cookie-based sessions.

Fields§

§store: Store§cookie_path: String§cookie_name: String§cookie_domain: Option<String>§persistence_policy: PersistencePolicy§session_ttl: Option<Duration>§same_site_policy: SameSite§http_only: bool§secure: bool§key: Key

Implementations§

source§

impl<Store> SessionLayer<Store>where Store: SessionStore,

source

pub fn new(store: Store, secret: &[u8]) -> SessionLayer<Store>

Creates a layer which will attach a SessionHandle to requests via an extension. This session is derived from a cryptographically signed cookie. When the client sends a valid, known cookie then the session is hydrated from this. Otherwise a new cookie is created and returned in the response.

The default behaviour is to enable “guest” sessions with PersistencePolicy::Always.

Panics

SessionLayer::new will panic if the secret is less than 64 bytes.

Customization

The configuration of the session may be adjusted according to the needs of your application:

SessionLayer::new(
    MemoryStore::new(),
    b"please do not hardcode your secret; instead use a
    cryptographically secure value",
)
.with_cookie_name("your.cookie.name")
.with_cookie_path("/some/path")
.with_cookie_domain("www.example.com")
.with_same_site_policy(SameSite::Lax)
.with_session_ttl(Some(Duration::from_secs(60 * 5)))
.with_persistence_policy(PersistencePolicy::Always)
.with_http_only(true)
.with_secure(true);
source

pub fn with_persistence_policy( self, policy: PersistencePolicy ) -> SessionLayer<Store>

When true, a session cookie will always be set. When false the session data must be modified in order for it to be set. Defaults to true.

Sets a cookie for the session. Defaults to "/".

Sets a cookie name for the session. Defaults to "sid".

Sets a cookie domain for the session. Defaults to None.

source

pub fn with_same_site_policy(self, policy: SameSite) -> SessionLayer<Store>

Sets a cookie same site policy for the session. Defaults to SameSite::Strict.

source

pub fn with_session_ttl( self, session_ttl: Option<Duration> ) -> SessionLayer<Store>

Sets a cookie time-to-live (ttl) for the session. Defaults to Duration::from_secs(60 * 60 * 24); one day.

source

pub fn with_http_only(self, http_only: bool) -> SessionLayer<Store>

Sets a cookie HttpOnly attribute for the session. Defaults to true.

source

pub fn with_secure(self, secure: bool) -> SessionLayer<Store>

Sets a cookie secure attribute for the session. Defaults to true.

Trait Implementations§

source§

impl<Store> Clone for SessionLayer<Store>where Store: Clone,

source§

fn clone(&self) -> SessionLayer<Store>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<Inner, Store> Layer<Inner> for SessionLayer<Store>where Store: SessionStore,

§

type Service = Session<Inner, Store>

The wrapped service
source§

fn layer(&self, inner: Inner) -> <SessionLayer<Store> as Layer<Inner>>::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

Auto Trait Implementations§

§

impl<Store> RefUnwindSafe for SessionLayer<Store>where Store: RefUnwindSafe,

§

impl<Store> Send for SessionLayer<Store>where Store: Send,

§

impl<Store> Sync for SessionLayer<Store>where Store: Sync,

§

impl<Store> Unpin for SessionLayer<Store>where Store: Unpin,

§

impl<Store> UnwindSafe for SessionLayer<Store>where Store: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for Twhere T: Clone,

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for Twhere T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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 Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

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

§

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