Skip to main content

SessionConfig

Struct SessionConfig 

Source
pub struct SessionConfig { /* private fields */ }
Available on crate feature auth only.
Expand description

Resolved session configuration.

Construct with SessionConfig::new (a signed-cookie layer), then pass to SessionConfig::into_layer with a session store to build a SessionLayer for Axum. Configuration is explicit and resolved; the library never reads environment variables inside layer construction or request handling.

§Signing key

The cookie signing key must be exactly 64 bytes, the master-key length required by the certified cookie crate’s signed jar. Use SessionKey::generate to produce a cryptographically random key. The key is held in a secrecy::SecretSlice; its Debug output never exposes the bytes.

§Two lifetimes

A session has two independent expiry bounds:

  • Idle/inactivity (max_age, Self::with_max_age) – a sliding window mapped to tower_sessions::Expiry::OnInactivity: each request that saves the session resets it.
  • Absolute (absolute_max_age, Self::with_absolute_max_age) – the maximum authenticated lifetime measured from the authentication timestamp stored in the session at login, enforced at the auth boundary.

Implementations§

Source§

impl SessionConfig

Source

pub fn new(signing_key: &[u8]) -> Result<Self, SessionConfigError>

Build session configuration with a signed-cookie key.

signing_key must be exactly 64 bytes. The cookie attributes default to secure values: name "__Host-id", SameSite=Strict, Secure=true, HttpOnly=true, path "/", no domain, idle Max-Age 14 days, absolute lifetime 30 days. Override any with the with_* builder methods.

§The __Host- prefix

The default cookie name is __Host-id. A __Host- prefix mandates Secure, no Domain, and path / (RFC 6265bis), which the default attributes already satisfy, so the rename is strictly tighter. It defeats session-fixation/cookie-tossing from a sibling subdomain. For development over plain HTTP use SessionConfig::dev (a __Host- cookie is silently dropped by the browser when it is not Secure).

§Errors

Returns SessionConfigError::InvalidSigningKey if the key is not exactly 64 bytes.

Source

pub fn dev(signing_key: &[u8]) -> Result<Self, SessionConfigError>

Build session configuration with the development defaults: cookie name arcature-id (no __Host- prefix), SameSite=Strict, Secure = false, HttpOnly=true, path "/", no domain, idle Max-Age 14 days, absolute lifetime 30 days.

A development server on plain HTTP cannot use the __Host- prefix (the browser drops a non-Secure __Host- cookie); this policy uses a plain cookie name so the session cookie reaches the browser over HTTP. Production keeps SessionConfig::new (__Host-id, Secure = true).

§Errors

Returns SessionConfigError::InvalidSigningKey if the key is not exactly 64 bytes.

Override the session cookie name.

Source

pub fn with_same_site(self, same_site: SameSite) -> Self

Override the SameSite attribute. Default SameSite::Strict.

Source

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

Override the Secure attribute (default true).

Source

pub fn with_http_only(self, http_only: bool) -> Self

Override the HttpOnly attribute (default true).

Source

pub fn with_path(self, path: impl Into<String>) -> Self

Override the cookie Path attribute (default "/").

Source

pub fn with_domain(self, domain: impl Into<String>) -> Self

Override the cookie Domain attribute (default: none).

Source

pub fn with_max_age(self, max_age: Duration) -> Self

Override the session idle/inactivity timeout. Default 14 days.

This is a sliding window: it is mapped to tower_sessions::Expiry::OnInactivity, so each request that saves the session resets the clock. The maximum authenticated lifetime is a separate bound – see Self::with_absolute_max_age.

Source

pub fn with_absolute_max_age(self, absolute_max_age: Duration) -> Self

Override the absolute authenticated session lifetime. Default 30 days.

Source

pub fn absolute_max_age(&self) -> Duration

The configured absolute authenticated session lifetime.

Source

pub fn into_layer<Store>( self, store: Store, ) -> Result<SessionLayer<Store>, SessionBuildError>
where Store: SessionStore,

Build a SessionLayer over store. Validates the configuration before constructing the tower-sessions layer.

§Errors

Returns SessionBuildError if the configuration is internally inconsistent (empty name/path, zero max-age, wrong key length, a __Host- cookie combined with Secure = false).

Trait Implementations§

Source§

impl Clone for SessionConfig

Source§

fn clone(&self) -> SessionConfig

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 SessionConfig

Manual Debug – the signing key is never exposed.

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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<T> MaybeSend for T
where T: Send,

Source§

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

Source§

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

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 = 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 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