From 1dc9fa993d42ff5041de9c93c4053d003003c7e0 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Tue, 28 Jan 2025 00:19:03 +0100 Subject: Generate authorized_keys from user keys --- server/src/main.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'server/src/main.rs') diff --git a/server/src/main.rs b/server/src/main.rs index 298a418..3d6d0e6 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -19,6 +19,7 @@ mod tests; mod api_model; mod auth; +mod authorized_keys; mod db_utils; mod fs_utils; mod git; @@ -669,7 +670,7 @@ async fn users( #[utoipa::path( responses( (status = 200, description = "Key added to current user", body = api_model::UserKey), - (status = 400, description = "Key too large"), + (status = 400, description = "Key too large or invalid"), ), security( ("session" = []), @@ -678,12 +679,17 @@ async fn users( #[post("/user/keys/add", data = "")] async fn user_key_add( mut db: Connection, + authorized_keys_config: &State>, + authorized_keys_state: &State, session: auth::Session, data: Json>, ) -> Result, Custom<&'static str>> { if data.data.len() > 8192 { return Err(Custom(Status::BadRequest, "Key is too large")); } + if data.kind.contains(' ') || data.data.contains(' ') { + return Err(Custom(Status::BadRequest, "Invalid kind or data")); + } let comment = data.comment.unwrap_or(""); let result = sqlx::query!( @@ -697,12 +703,24 @@ async fn user_key_add( .await .unwrap(); - Ok(Json(api_model::UserKey { + let key = api_model::UserKey { id: result.last_insert_id(), kind: data.kind.to_string(), data: data.data.to_string(), comment: comment.to_string(), - })) + }; + + authorized_keys_state + .new_user_key( + authorized_keys_config, + key.id, + session.user_id.as_str(), + key.kind.as_str(), + key.data.as_str(), + ) + .await; + + Ok(Json(key)) } #[utoipa::path( @@ -750,6 +768,8 @@ async fn user_key_get( #[delete("/user/keys/")] async fn user_key_del( mut db: Connection, + authorized_keys_config: &State>, + authorized_keys_state: &State, session: auth::Session, id: u64, ) -> Result<&'static str, Custom<&'static str>> { @@ -765,6 +785,10 @@ async fn user_key_del( return Err(Custom(Status::NotFound, "No such key for current user")); } + authorized_keys_state + .del_user_key(authorized_keys_config, id, session.user_id.as_str()) + .await; + Ok("") } @@ -863,6 +887,7 @@ fn rocket_from_config(figment: Figment) -> Rocket { ) .attach(auth::stage(basepath)) .attach(git_root::stage()) + .attach(authorized_keys::stage()) } #[rocket::main] -- cgit v1.2.3-70-g09d2