From 7bc8e8b7262a3f3abe3222b3b434838e85cdb2bb Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 29 Dec 2024 20:37:26 +0100 Subject: Rework auth to include session The actual authentication is still fake. --- server/src/main.rs | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) (limited to 'server/src/main.rs') diff --git a/server/src/main.rs b/server/src/main.rs index f4fec18..223d861 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -4,39 +4,18 @@ extern crate rocket; use futures::{future::TryFutureExt, stream::TryStreamExt}; use rocket::fairing::{self, AdHoc}; -use rocket::request::{self, FromRequest, Outcome, Request}; use rocket::response::status::NotFound; use rocket::serde::json::Json; use rocket::{futures, Build, Rocket}; use rocket_db_pools::{sqlx, Connection, Database}; mod api_model; +mod auth; #[derive(Database)] #[database("eyeballs")] struct Db(sqlx::MySqlPool); -struct User { - username: String, -} - -#[derive(Debug)] -enum UserError { - Missing, - Invalid, -} - -#[rocket::async_trait] -impl<'r> FromRequest<'r> for User { - type Error = UserError; - - async fn from_request(_req: &'r Request<'_>) -> request::Outcome { - Outcome::Success(User { - username: String::from("foo"), - }) - } -} - enum Role { Reviewer, Watcher, @@ -76,7 +55,7 @@ impl TryFrom for api_model::ReviewState { #[get("/projects?&")] async fn projects<'r>( mut db: Connection, - _user: User, + _session: auth::Session, limit: Option, offset: Option, ) -> Json { @@ -117,7 +96,7 @@ async fn projects<'r>( #[get("/project/")] async fn project<'r>( mut db: Connection, - _user: User, + _session: auth::Session, projectid: u64, ) -> Result, NotFound<&'static str>> { let members = sqlx::query!( @@ -154,7 +133,7 @@ async fn project<'r>( #[get("/project//reviews?&")] async fn reviews<'r>( mut db: Connection, - _user: User, + _session: auth::Session, projectid: u64, limit: Option, offset: Option, @@ -204,7 +183,7 @@ async fn reviews<'r>( #[get("/review/")] async fn review<'r>( mut db: Connection, - _user: User, + _session: auth::Session, reviewid: u64, ) -> Result, NotFound<&'static str>> { let mut users = sqlx::query!( @@ -277,10 +256,13 @@ async fn run_migrations(rocket: Rocket) -> fairing::Result { #[rocket::main] async fn main() -> Result<(), rocket::Error> { + let basepath = "/api/v1"; + let _rocket = rocket::build() .attach(Db::init()) .attach(AdHoc::try_on_ignite("Database Migrations", run_migrations)) - .mount("/api/v1", routes![projects, project, reviews, review]) + .mount(basepath, routes![projects, project, reviews, review]) + .attach(auth::stage(basepath.to_string())) .launch() .await?; -- cgit v1.2.3-70-g09d2