Expand description
A cookie manager middleware built on top of tower.
§Example
With axum:
use axum::{routing::get, Router};
use std::net::SocketAddr;
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(handler))
.layer(CookieManagerLayer::new());
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
async fn handler(cookies: Cookies) -> &'static str {
cookies.add(Cookie::new("hello_world", "hello_world"));
"Check your cookies."
}
A complete CRUD cookie example in examples/counter.rs
Modules§
Structs§
- Cookie
- Representation of an HTTP cookie.
- Cookie
Manager - Middleware to use
Cookies
. - Cookie
Manager Layer - Layer to apply
CookieManager
middleware. - Cookies
- A parsed on-demand cookie jar.
- Key
- A cryptographic master key for use with
Signed
and/orPrivate
jars. - Private
Cookies - A cookie jar that provides authenticated encryption for its cookies.
- Signed
Cookies - A child cookie jar that authenticates its cookies.
It signs all the cookies added to it and verifies cookies retrieved from it.
Any cookies stored in
SignedCookies
are provided integrity and authenticity. In other words, clients cannot tamper with the contents of a cookie nor can they fabricate cookie values, but the data is visible in plaintext.