Crates.io | tower-surf |
lib.rs | tower-surf |
version | 0.3.0 |
source | src |
created_at | 2024-08-19 02:22:09.847054 |
updated_at | 2024-08-20 16:00:51.189886 |
description | 🌊 A stateless CSRF middleware for tower. |
homepage | |
repository | https://github.com/its-danny/tower-surf |
max_upload_size | |
id | 1343282 |
size | 84,483 |
This crate uses the Double Submit Cookie Pattern to mitigate CSRF.
rand
crate).By default, the cookies are set to HTTPOnly
, SameSite: Strict
, and Secure
.
[dependencies]
tower-surf = "0.3.0"
axum
use std::net::SocketAddr;
use axum::{routing::get, Router};
use http::StatusCode;
use tower_surf::{Surf, Token};
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/login", get(login)).route("/logout", get(logout))
.layer(Surf::new("secret-key").secure(false));
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 login(token: Token) -> Result<StatusCode, StatusCode> {
token.set("unique-session-id").map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(StatusCode::OK)
}
async fn logout(token: Token) -> StatusCode {
token.reset();
StatusCode::OK
}
[!NOTE] See the examples for a full example.