1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
//! A middleware that provides [`Session`] as a request extension.
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use http::{Request, Response};
use time::Duration;
use tower_cookies::{cookie::SameSite, Cookie, CookieManager, Cookies};
use tower_layer::Layer;
use tower_service::Service;
use tracing::Instrument;
use crate::{
session::{self, Expiry},
Session, SessionStore,
};
#[derive(Debug, Clone)]
struct SessionConfig {
name: String,
http_only: bool,
same_site: SameSite,
expiry: Option<Expiry>,
secure: bool,
path: String,
domain: Option<String>,
}
impl SessionConfig {
fn build_cookie<'c>(&self, session_id: session::Id, expiry_age: Duration) -> Cookie<'c> {
let mut cookie_builder = Cookie::build((self.name.clone(), session_id.to_string()))
.http_only(self.http_only)
.same_site(self.same_site)
.secure(self.secure)
.path(self.path.clone());
cookie_builder = cookie_builder.max_age(expiry_age);
if let Some(domain) = &self.domain {
cookie_builder = cookie_builder.domain(domain.clone());
}
cookie_builder.build()
}
}
impl Default for SessionConfig {
fn default() -> Self {
Self {
name: String::from("id"), /* See: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-name-fingerprinting */
http_only: true,
same_site: SameSite::Strict,
expiry: None, // TODO: Is `Max-Age: "Session"` the right default?
secure: true,
path: String::from("/"),
domain: None,
}
}
}
/// A middleware that provides [`Session`] as a request extension.
#[derive(Debug, Clone)]
pub struct SessionManager<S, Store: SessionStore> {
inner: S,
session_store: Arc<Store>,
session_config: SessionConfig,
}
impl<S, Store: SessionStore> SessionManager<S, Store> {
/// Create a new [`SessionManager`].
pub fn new(inner: S, session_store: Store) -> Self {
Self {
inner,
session_store: Arc::new(session_store),
session_config: Default::default(),
}
}
}
impl<ReqBody, ResBody, S, Store: SessionStore> Service<Request<ReqBody>>
for SessionManager<S, Store>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + 'static,
S::Error: Into<Box<dyn std::error::Error + Send + Sync>> + 'static,
S::Future: Send,
ReqBody: Send + 'static,
ResBody: Send,
{
type Response = S::Response;
type Error = Box<dyn std::error::Error + Send + Sync>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx).map_err(Into::into)
}
fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
let span = tracing::info_span!("call");
let session_store = self.session_store.clone();
let session_config = self.session_config.clone();
// Because the inner service can panic until ready, we need to ensure we only
// use the ready service.
//
// See: https://docs.rs/tower/latest/tower/trait.Service.html#be-careful-when-cloning-inner-services
let clone = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(
async move {
let Some(cookies) = req.extensions().get::<Cookies>().cloned() else {
return Err(session::Error::MissingCookies.into());
};
let session_cookie = cookies.get(&session_config.name).map(Cookie::into_owned);
let session_id = session_cookie
.clone()
.map(|cookie| cookie.value().to_string())
.and_then(|cookie_value| cookie_value.parse::<session::Id>().ok());
let session = Session::new(session_id, session_store, session_config.expiry);
req.extensions_mut().insert(session.clone());
let res = inner.call(req).await.map_err(Into::into)?;
let modified = session.is_modified();
let empty = session.is_empty().await;
tracing::trace!(modified = modified, empty = empty, "session response state");
match session_cookie {
Some(cookie) if empty => {
tracing::debug!("removing session cookie");
cookies.remove(cookie)
}
// TODO: We can consider an "always save" configuration option:
_ if modified && !empty && !res.status().is_server_error() => {
tracing::debug!("saving session");
session.save().await?;
let session_id = session.id().ok_or(session::Error::MissingId)?;
let expiry_age = session.expiry_age();
let session_cookie = session_config.build_cookie(session_id, expiry_age);
tracing::debug!("adding session cookie");
cookies.add(session_cookie);
}
_ => (),
};
Ok(res)
}
.instrument(span),
)
}
}
/// A layer for providing [`Session`] as a request extension.
#[derive(Debug, Clone)]
pub struct SessionManagerLayer<Store: SessionStore> {
session_store: Arc<Store>,
session_config: SessionConfig,
}
impl<Store: SessionStore> SessionManagerLayer<Store> {
/// Configures the name of the cookie used for the session.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::{MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_service = SessionManagerLayer::new(session_store).with_name("my.sid");
/// ```
pub fn with_name(mut self, name: &str) -> Self {
self.session_config.name = name.to_string();
self
}
/// Configures the `"HttpOnly"` attribute of the cookie used for the
/// session.
///
/// # ⚠️ **Warning: Cross-site scripting risk**
///
/// Applications should generally **not** override the default value of
/// `true`. If you do, you are exposing your application to increased risk
/// of cookie theft via techniques like cross-site scripting.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::{MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_service = SessionManagerLayer::new(session_store).with_http_only(true);
/// ```
pub fn with_http_only(mut self, http_only: bool) -> Self {
self.session_config.http_only = http_only;
self
}
/// Configures the `"SameSite"` attribute of the cookie used for the
/// session.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::{cookie::SameSite, MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_service = SessionManagerLayer::new(session_store).with_same_site(SameSite::Lax);
/// ```
pub fn with_same_site(mut self, same_site: SameSite) -> Self {
self.session_config.same_site = same_site;
self
}
/// Configures the `"Max-Age"` attribute of the cookie used for the session.
///
/// # Examples
///
/// ```rust
/// use time::Duration;
/// use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_expiry = Expiry::OnInactivity(Duration::hours(1));
/// let session_service = SessionManagerLayer::new(session_store).with_expiry(session_expiry);
/// ```
pub fn with_expiry(mut self, expiry: Expiry) -> Self {
self.session_config.expiry = Some(expiry);
self
}
/// Configures the `"Secure"` attribute of the cookie used for the session.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::{MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_service = SessionManagerLayer::new(session_store).with_secure(true);
/// ```
pub fn with_secure(mut self, secure: bool) -> Self {
self.session_config.secure = secure;
self
}
/// Configures the `"Path"` attribute of the cookie used for the session.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::{MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_service =
/// SessionManagerLayer::new(session_store).with_path("/some/path".to_string());
/// ```
pub fn with_path(mut self, path: String) -> Self {
self.session_config.path = path;
self
}
/// Configures the `"Domain"` attribute of the cookie used for the session.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::{MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_service =
/// SessionManagerLayer::new(session_store).with_domain("localhost".to_string());
/// ```
pub fn with_domain(mut self, domain: String) -> Self {
self.session_config.domain = Some(domain);
self
}
}
impl<Store: SessionStore> SessionManagerLayer<Store> {
/// Create a new [`SessionManagerLayer`] with the provided session store
/// and default cookie configuration.
///
/// # Examples
///
/// ```rust
/// use tower_sessions::{MemoryStore, SessionManagerLayer};
///
/// let session_store = MemoryStore::default();
/// let session_service = SessionManagerLayer::new(session_store);
/// ```
pub fn new(session_store: Store) -> Self {
let session_config = SessionConfig::default();
Self {
session_store: Arc::new(session_store),
session_config,
}
}
}
impl<S, Store: SessionStore> Layer<S> for SessionManagerLayer<Store> {
type Service = CookieManager<SessionManager<S, Store>>;
fn layer(&self, inner: S) -> Self::Service {
let session_manager = SessionManager {
inner,
session_store: self.session_store.clone(),
session_config: self.session_config.clone(),
};
CookieManager::new(session_manager)
}
}