Struct mailpot_web::SessionLayer
source · 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
§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,
impl<Store> SessionLayer<Store>where Store: SessionStore,
sourcepub fn new(store: Store, secret: &[u8]) -> SessionLayer<Store>
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);
sourcepub fn with_persistence_policy(
self,
policy: PersistencePolicy
) -> SessionLayer<Store>
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
.
sourcepub fn with_same_site_policy(self, policy: SameSite) -> SessionLayer<Store>
pub fn with_same_site_policy(self, policy: SameSite) -> SessionLayer<Store>
Sets a cookie same site policy for the session. Defaults to
SameSite::Strict
.
sourcepub fn with_session_ttl(
self,
session_ttl: Option<Duration>
) -> SessionLayer<Store>
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.
sourcepub fn with_http_only(self, http_only: bool) -> SessionLayer<Store>
pub fn with_http_only(self, http_only: bool) -> SessionLayer<Store>
Sets a cookie HttpOnly
attribute for the session. Defaults to true
.
sourcepub fn with_secure(self, secure: bool) -> SessionLayer<Store>
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,
impl<Store> Clone for SessionLayer<Store>where Store: Clone,
source§fn clone(&self) -> SessionLayer<Store>
fn clone(&self) -> SessionLayer<Store>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more