Working on DAV router

This commit is contained in:
Quentin 2024-02-27 19:30:51 +01:00
parent 7f35e68bfe
commit 239df7bd14
Signed by: quentin
GPG Key ID: E9602264D639FF68
1 changed files with 19 additions and 11 deletions

View File

@ -13,6 +13,7 @@ use tokio::sync::watch;
use crate::config::DavUnsecureConfig; use crate::config::DavUnsecureConfig;
use crate::login::ArcLoginProvider; use crate::login::ArcLoginProvider;
use crate::user::User;
pub struct Server { pub struct Server {
bind_addr: SocketAddr, bind_addr: SocketAddr,
@ -45,7 +46,7 @@ impl Server {
_ = wait_conn_finished => continue, _ = wait_conn_finished => continue,
_ = must_exit.changed() => continue, _ = must_exit.changed() => continue,
}; };
tracing::info!("DAV: accepted connection from {}", remote_addr); tracing::info!("Accepted connection from {}", remote_addr);
let stream = TokioIo::new(socket); let stream = TokioIo::new(socket);
let login = self.login_provider.clone(); let login = self.login_provider.clone();
let conn = tokio::spawn(async move { let conn = tokio::spawn(async move {
@ -66,13 +67,14 @@ impl Server {
} }
drop(tcp); drop(tcp);
tracing::info!("DAV server shutting down, draining remaining connections..."); tracing::info!("Server shutting down, draining remaining connections...");
while connections.next().await.is_some() {} while connections.next().await.is_some() {}
Ok(()) Ok(())
} }
} }
//@FIXME We should not support only BasicAuth
async fn auth( async fn auth(
login: ArcLoginProvider, login: ArcLoginProvider,
req: Request<impl hyper::body::Body>, req: Request<impl hyper::body::Body>,
@ -113,21 +115,27 @@ async fn auth(
.body(Full::new(Bytes::from("Wrong credentials")))?), .body(Full::new(Bytes::from("Wrong credentials")))?),
}; };
// Build a user
let user = User::new(username.into(), creds).await?;
// Call router with user // Call router with user
router(user, req).await
unimplemented!();
} }
async fn router(req: Request<impl hyper::body::Body>) -> Result<Response<Full<Bytes>>> { async fn router(user: std::sync::Arc<User>, req: Request<impl hyper::body::Body>) -> Result<Response<Full<Bytes>>> {
let path_segments: Vec<_> = req.uri().path().split("/").filter(|s| *s != "").collect(); let path_segments: Vec<_> = req.uri().path().split("/").filter(|s| *s != "").collect();
match path_segments.as_slice() { match path_segments.as_slice() {
[] => tracing::info!("root"), [] => tracing::info!("root"),
[ user ] => tracing::info!(user=user, "user home"), [ username, ..] if *username != user.username => return Ok(Response::builder()
[ user, coltype ] => tracing::info!(user=user, cat=coltype, "user cat of coll"), .status(403)
[ user, coltype, colname ] => tracing::info!(user=user, cat=coltype, name=colname, "user coll"), .body(Full::new(Bytes::from("Accessing other user ressources is not allowed")))?),
[ user, coltype, colname, member ] => tracing::info!(user=user, cat=coltype, name=colname, obj=member, "accessing file"), [ _ ] => tracing::info!(user=username, "user home"),
_ => unimplemented!(), [ _, "calendar" ] => tracing::info!(user=username, cat=coltype, "user cat of coll"),
[ _, "calendar", colname ] => tracing::info!(user=username, cat=coltype, name=colname, "user coll"),
[ _, "calendar", colname, member ] => tracing::info!(user=username, cat=coltype, name=colname, obj=member, "accessing file"),
_ => return Ok(Response::builder()
.status(404)
.body(Full::new(Bytes::from("Resource not found")))?),
} }
Ok(Response::new(Full::new(Bytes::from("Hello World!")))) Ok(Response::new(Full::new(Bytes::from("Hello World!"))))
} }