Got session control into it's own layer.
This commit is contained in:
68
src/main.rs
68
src/main.rs
@ -6,12 +6,11 @@ use axum::{
|
||||
routing::get,
|
||||
RequestPartsExt, Router,
|
||||
};
|
||||
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
||||
use clap::Parser;
|
||||
use morethantext::MoreThanText;
|
||||
use std::convert::Infallible;
|
||||
use tokio::{spawn, sync::mpsc::channel};
|
||||
use tower_cookies::{CookieManagerLayer, Cookies};
|
||||
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};
|
||||
use uuid::Uuid;
|
||||
|
||||
const LOCALHOST: &str = "127.0.0.1";
|
||||
@ -64,34 +63,27 @@ where
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let Extension(cookies) = parts.extract::<Extension<Cookies>>().await.unwrap();
|
||||
let Extension(mut state) = parts.extract::<Extension<MoreThanText>>().await.unwrap();
|
||||
let sess_id: Option<String> = None;
|
||||
let id = Uuid::nil();
|
||||
//let id = state.validate_session(sess_id);
|
||||
cookies.add(tower_cookies::Cookie::new(SESSION_KEY, id.to_string()));
|
||||
let req_id = match cookies.get(SESSION_KEY) {
|
||||
Some(cookie) => Some(cookie.value().to_string()),
|
||||
None => None,
|
||||
};
|
||||
let requested = req_id.clone();
|
||||
let (tx, mut rx) = channel(1);
|
||||
spawn(async move {
|
||||
tx.send(state.validate_session(requested)).await.unwrap();
|
||||
});
|
||||
let id = rx.recv().await.unwrap();
|
||||
if !req_id.is_some_and(|x| x == id.to_string()) {
|
||||
cookies.add(Cookie::new(SESSION_KEY, id.to_string()));
|
||||
}
|
||||
Ok(SessionID(id))
|
||||
}
|
||||
}
|
||||
|
||||
async fn mtt_conn(
|
||||
jar: CookieJar,
|
||||
sess_id: SessionID,
|
||||
state: State<MoreThanText>,
|
||||
) -> impl IntoResponse {
|
||||
/*
|
||||
let sid = match jar.get(SESSION_KEY) {
|
||||
Some(cookie) => Some(cookie.value().to_string()),
|
||||
None => None,
|
||||
};
|
||||
let sess_info = sid.clone();
|
||||
let (tx, mut rx) = channel(5);
|
||||
spawn(async move {
|
||||
tx.send(state.clone().request(sess_info)).await.unwrap();
|
||||
});
|
||||
let reply = rx.recv().await.unwrap();
|
||||
let cookie = Cookie::build((SESSION_KEY, reply.get_data("sess_id").unwrap().to_string()));
|
||||
let cookies = jar.add(cookie);
|
||||
(cookies, reply.get_data("dov").unwrap().to_string())
|
||||
*/
|
||||
("something".to_string(),)
|
||||
}
|
||||
|
||||
@ -100,7 +92,10 @@ mod servers {
|
||||
use super::*;
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode},
|
||||
http::{
|
||||
header::{COOKIE, SET_COOKIE},
|
||||
Request, StatusCode,
|
||||
},
|
||||
};
|
||||
use tower::ServiceExt;
|
||||
|
||||
@ -112,7 +107,7 @@ mod servers {
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let sessid = format!("{:?}", response.headers().get("set-cookie").unwrap());
|
||||
let sessid = format!("{:?}", response.headers().get(SET_COOKIE).unwrap());
|
||||
assert!(sessid.contains(SESSION_KEY), "did not set session id");
|
||||
}
|
||||
|
||||
@ -126,7 +121,7 @@ mod servers {
|
||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let sessid = format!("{:?}", response.headers().get("set-cookie").unwrap());
|
||||
let sessid = format!("{:?}", response.headers().get(SET_COOKIE).unwrap());
|
||||
assert!(
|
||||
!holder.contains(&sessid),
|
||||
"found duplicate entry: {:?}",
|
||||
@ -136,6 +131,29 @@ mod servers {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cookie_only_issued_once() {
|
||||
let app = create_app(MoreThanText::new()).await;
|
||||
let initial = app
|
||||
.clone()
|
||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(initial.status(), StatusCode::OK);
|
||||
let sessid = initial.headers().get(SET_COOKIE).unwrap();
|
||||
let mut request = Request::builder()
|
||||
.uri("/")
|
||||
.header(COOKIE, sessid.clone())
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
let response = app.clone().oneshot(request).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
match response.headers().get(SET_COOKIE) {
|
||||
Some(info) => assert!(false, "first pass: {:?}, second pass: {:?}", sessid, info),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn receive_file_not_found() {
|
||||
let app = create_app(MoreThanText::new()).await;
|
||||
|
Reference in New Issue
Block a user