hardcoded session cookie

This commit is contained in:
2020-03-25 15:56:09 -07:00
parent 161a042123
commit cf6f65cc62
2 changed files with 75 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ edition = "2018"
futures = "0.3" futures = "0.3"
log = "0.4" log = "0.4"
pretty_env_logger = "0.4" pretty_env_logger = "0.4"
sessions = { version = "0.0.2", features = ["fs-store", "nanoid", "tokio"] }
tokio = { version = "0.2", features = ["macros"] } tokio = { version = "0.2", features = ["macros"] }
warp = "0.2" warp = "0.2"
webrtc-unreliable = "0.4" webrtc-unreliable = "0.4"

View File

@@ -1,22 +1,30 @@
use std::collections::HashMap; use std::collections::HashMap;
use log::info; use log::{info, trace};
use warp::{Filter, Rejection, Reply}; use warp::{Filter, Rejection, Reply};
use warp::http::StatusCode; use warp::http::{StatusCode, Uri};
const SESSION_HEADER: &'static str = "Session";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
let mut store = sessions::FilesystemStore::new("data/sessions.json".into());
// GET / // GET /
let root = warp::path::end() let root = warp::path::end()
.and(warp::get()) .and(warp::get())
.and(warp::fs::file("./static/index.html")); .and(with_session(&mut store))
.map(|session| format!("Hello user #{}", session));
// POST /name name={name} // GET/POST /user name={name}
let namechange = warp::path("name") let namechange = warp::path!("user")
.and(warp::path::end()) .and(warp::get()
.and(warp::post()) .and(with_session(&mut store))
.and(warp::fs::file("./static/index.html"))
.map(|_session, file| file)
// .recover(|err| todo!("prevent redir loop")) // reply::with_status(..)
.or(warp::post()
.and(warp::body::content_length_limit(1024 * 16)) .and(warp::body::content_length_limit(1024 * 16))
.and(warp::body::form()) .and(warp::body::form())
.and_then(|form: HashMap<String, String>| async move { .and_then(|form: HashMap<String, String>| async move {
@@ -26,19 +34,49 @@ async fn main() {
name.chars().count() > 32 => return Err(warp::reject::custom(BadName)), name.chars().count() > 32 => return Err(warp::reject::custom(BadName)),
Some(name) => name Some(name) => name
}; };
info!("POST /name as \"{}\"", name); info!("POST /user as \"{}\"", name);
Ok(warp::reply::with_header(StatusCode::SEE_OTHER, Ok(warp::reply::with_header(StatusCode::SEE_OTHER,
warp::http::header::LOCATION, warp::http::header::LOCATION,
"/")) "/"))
}) })
.recover(handle_reject); .recover(handle_reject)
)
);
let routes = root let routes = root
.or(namechange); .or(namechange)
.recover(handle_no_session);
warp::serve(routes).run(([127, 0, 0, 1], 8060)).await; warp::serve(routes).run(([127, 0, 0, 1], 8060)).await;
} }
// TODO: change `T` to `!` when/if `never` stabilizes?
async fn clarify_error<T, From, To>(err: Rejection) -> Result<T, Rejection>
where From: 'static,
To: warp::reject::Reject + Default
{
Err(match err.find::<From>() {
Some(_) => { trace!("no session"); warp::reject::custom(To::default()) },
_ => {
trace!("no clarify to {}: {} ≠ {:?}", std::any::type_name::<From>(),
std::any::type_name::<To>(),
err);
err
}
})
}
fn with_session(_store: &mut impl sessions::Storable) -> impl Filter<Extract = (String,),
Error = Rejection> + Clone {
warp::cookie::cookie(SESSION_HEADER)
.map(|session| {
info!("Found session: {}", session);
session
})
.or_else(clarify_error::<_, warp::reject::MissingCookie, NoSession>) // Has cookies, but not session cookie
.or_else(clarify_error::<_, warp::reject::InvalidHeader, NoSession>) // No cookies at all
}
#[derive(Debug)] #[derive(Debug)]
struct BadName; struct BadName;
impl warp::reject::Reject for BadName {} impl warp::reject::Reject for BadName {}
@@ -49,3 +87,16 @@ async fn handle_reject(err: Rejection) -> Result<impl Reply, Rejection> {
_ => Err(err) _ => Err(err)
} }
} }
#[derive(Debug, Default)]
struct NoSession;
impl warp::reject::Reject for NoSession {}
async fn handle_no_session(err: Rejection) -> Result<impl Reply, Rejection> {
match err.find() {
Some(NoSession) => Ok(warp::reply::with_header(warp::redirect::temporary(Uri::from_static("/user")),
"Set-Cookie",
format!("{}={}; Max-Age=31536000; SameSite=Lax", SESSION_HEADER, 5))),
_ => Err(err)
}
}