Expand description
Cookie sessions and CSRF tokens for siderite applications.
This crate is an adapter: it owns cookie mechanics, id generation,
rotation, expiry and CSRF tokens, and nothing about identity. Users,
login flows and password storage stay in the application, which calls
Session::login once it has authenticated someone. Nothing in
siderite-core depends on it.
SessionLayerloads the session named by the cookie, exposes it as theSessionextractor and writes changes back.SessionConfigsets cookie flags (__Host-name,HttpOnly,Secure,Path=/,SameSite=Lax), lifetimes and the CSRF key.SessionStoreis the storage trait. Stores only ever see the SHA-256 hex of a session id (id_hash).MemoryStoreis included;CacheStore(featurecache) uses anysiderite_cache::Cache, Redis included.
The requirements this crate implements are in
docs/plans/SESSIONS_CSRF.md. For cross-site request forgery pair it
with siderite_core::middleware::CsrfOrigin; add a CSRF token for
forms and for SameSite=None.
use siderite_core::{App, get, post};
use siderite_sessions::{
MemoryStore, Session, SessionConfig, SessionLayer,
};
async fn login(session: Session) -> &'static str {
// ...authenticate the user first...
session.login("ada");
"welcome"
}
async fn me(session: Session) -> String {
session.user().unwrap_or_else(|| "anonymous".into())
}
let store = MemoryStore::default();
let layer = SessionLayer::new(store, SessionConfig::new())?;
App::new()
.route("/login", post(login))
.route("/me", get(me))
.layer(layer)
.run("0.0.0.0:8000")
.await?;Structs§
- Cache
Store - Sessions stored as JSON in a
Cache, with the store TTL. - Memory
Store - A session store in process memory, for tests and single-process apps.
- Session
- The request’s session, available once a
SessionLayeris installed. - Session
Config - How sessions are issued, kept and expired.
- Session
Config Error - An invalid
SessionConfig, reported bySessionLayer::new. - Session
Layer - Loads the session named by the request cookie, exposes it to handlers
as
Session, and writes changes back when the handler returns. - Session
Record - One stored session.
- Store
Error - A store failure.
Enums§
Traits§
- Session
Store - Server-side storage of sessions, keyed by the SHA-256 hex of the id.
Functions§
- id_hash
- The SHA-256 of a session id as lowercase hex: the only form of an id
a
SessionStoreever sees.