pub struct SessionConfig { /* private fields */ }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 totower_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
impl SessionConfig
Sourcepub fn new(signing_key: &[u8]) -> Result<Self, SessionConfigError>
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.
Sourcepub fn dev(signing_key: &[u8]) -> Result<Self, SessionConfigError>
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.
Sourcepub fn with_same_site(self, same_site: SameSite) -> Self
pub fn with_same_site(self, same_site: SameSite) -> Self
Override the SameSite attribute. Default SameSite::Strict.
Sourcepub fn with_secure(self, secure: bool) -> Self
pub fn with_secure(self, secure: bool) -> Self
Override the Secure attribute (default true).
Sourcepub fn with_http_only(self, http_only: bool) -> Self
pub fn with_http_only(self, http_only: bool) -> Self
Override the HttpOnly attribute (default true).
Sourcepub fn with_path(self, path: impl Into<String>) -> Self
pub fn with_path(self, path: impl Into<String>) -> Self
Override the cookie Path attribute (default "/").
Sourcepub fn with_domain(self, domain: impl Into<String>) -> Self
pub fn with_domain(self, domain: impl Into<String>) -> Self
Override the cookie Domain attribute (default: none).
Sourcepub fn with_max_age(self, max_age: Duration) -> Self
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.
Sourcepub fn with_absolute_max_age(self, absolute_max_age: Duration) -> Self
pub fn with_absolute_max_age(self, absolute_max_age: Duration) -> Self
Override the absolute authenticated session lifetime. Default 30 days.
Sourcepub fn absolute_max_age(&self) -> Duration
pub fn absolute_max_age(&self) -> Duration
The configured absolute authenticated session lifetime.
Sourcepub fn into_layer<Store>(
self,
store: Store,
) -> Result<SessionLayer<Store>, SessionBuildError>where
Store: SessionStore,
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
impl Clone for SessionConfig
Source§fn clone(&self) -> SessionConfig
fn clone(&self) -> SessionConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SessionConfig
impl RefUnwindSafe for SessionConfig
impl Send for SessionConfig
impl Sync for SessionConfig
impl Unpin for SessionConfig
impl UnsafeUnpin for SessionConfig
impl UnwindSafe for SessionConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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