pub struct SessionLayer<SessionData, SessionStoreConnection> { /* private fields */ }
Expand description

Layer that provides cookie-based sessions. See SessionLayer::new for more details.

Implementations§

source§

impl<SessionData, SessionStoreConnection: SessionStoreConnector<SessionData>> SessionLayer<SessionData, SessionStoreConnection>

source

pub fn new() -> Self

Creates a layer which will attach a SessionHandle to requests via an extension. This session is derived from a cookie. When the client sends a valid, known cookie then the session is loaded using the cookie as key. Otherwise, the SessionHandle will contain a default session which is only persisted if it was mutably accessed.

The layer expects the SessionStoreConnection to exist as an extension. It is a type that implements SessionStoreConnector with the correct SessionData. The type is required to implement Send, Sync and Clone, such that the session layer can make its own copy (required due to some details of axum, specifically the extensions do not get automatically attached to a response from the corresponding request).

The SessionStoreConnection can e.g. be a type representing a simple database connection, a connection pool or database transaction. Since sessions cannot be updated concurrently, using transactions may be useful, to be able to roll back all changes in case the session got updated. It is important that the SessionStoreConnection in the extension is ready, because axum does not use backpressure, but explicit readiness checks.

Customization

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

use typed_session::SessionRenewalStrategy;
SessionLayer::<i32, MemoryStore<i32, NoLogger>>::new()
.with_cookie_name("id") // for security reasons, just stick with the default "id" here
.with_cookie_path("/some/path")
.with_cookie_domain("www.example.com")
.with_same_site_policy(SameSite::Strict)
.with_session_renewal_strategy(SessionRenewalStrategy::AutomaticRenewal {
    time_to_live: Duration::hours(24),
    maximum_remaining_time_to_live_for_renewal: Duration::hours(20),
})
.with_secure(true);

Sets the url path for which the session cookie is valid. Defaults to "/".

Sets the name of the session cookie. Defaults to "id". For security reasons, choose a generic name, and ideally just stick with the default.

Sets the domain for which the session cookie is valid. Defaults to None.

source

pub fn with_same_site_policy(self, policy: SameSite) -> Self

Sets the same-site policy of the session cookie. Defaults to SameSite::Strict. For security reasons, do not change this.

source

pub fn with_session_renewal_strategy( self, session_renewal_strategy: SessionRenewalStrategy ) -> Self

Sets the renewal strategy for sessions. See the members of SessionRenewalStrategy for more details. Defaults to AutomaticRenewal with a ttl of 24 hours and an automatic renewal delay of 4 hours.

source

pub fn with_secure(self, secure: bool) -> Self

Sets the secure attribute for the session cookie. Defaults to true. For security reasons, do not set this to false.

Trait Implementations§

source§

impl<SessionData, SessionStoreConnection: Clone> Clone for SessionLayer<SessionData, SessionStoreConnection>

source§

fn clone(&self) -> Self

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<SessionData: Debug, SessionStoreConnection: Debug> Debug for SessionLayer<SessionData, SessionStoreConnection>

source§

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

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

impl<SessionData, SessionStoreConnection: SessionStoreConnector<SessionData>> Default for SessionLayer<SessionData, SessionStoreConnection>

source§

fn default() -> Self

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

impl<Inner, SessionData, SessionStoreConnection: SessionStoreConnector<SessionData> + Clone> Layer<Inner> for SessionLayer<SessionData, SessionStoreConnection>

§

type Service = Session<Inner, SessionData, SessionStoreConnection>

The wrapped service
source§

fn layer(&self, inner: Inner) -> Self::Service

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

Auto Trait Implementations§

§

impl<SessionData, SessionStoreConnection> RefUnwindSafe for SessionLayer<SessionData, SessionStoreConnection>where SessionData: RefUnwindSafe, SessionStoreConnection: RefUnwindSafe,

§

impl<SessionData, SessionStoreConnection> Send for SessionLayer<SessionData, SessionStoreConnection>where SessionData: Send, SessionStoreConnection: Send,

§

impl<SessionData, SessionStoreConnection> Sync for SessionLayer<SessionData, SessionStoreConnection>where SessionData: Sync, SessionStoreConnection: Sync,

§

impl<SessionData, SessionStoreConnection> Unpin for SessionLayer<SessionData, SessionStoreConnection>where SessionData: Unpin, SessionStoreConnection: Unpin,

§

impl<SessionData, SessionStoreConnection> UnwindSafe for SessionLayer<SessionData, SessionStoreConnection>where SessionData: UnwindSafe, SessionStoreConnection: 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> 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