summaryrefslogtreecommitdiff
path: root/server/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/main.rs')
-rw-r--r--server/src/main.rs31
1 files changed, 28 insertions, 3 deletions
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 = "<data>")]
async fn user_key_add(
mut db: Connection<Db>,
+ authorized_keys_config: &State<authorized_keys::Config<'_>>,
+ authorized_keys_state: &State<authorized_keys::AuthorizedKeys>,
session: auth::Session,
data: Json<api_model::UserKeyData<'_>>,
) -> Result<Json<api_model::UserKey>, 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/<id>")]
async fn user_key_del(
mut db: Connection<Db>,
+ authorized_keys_config: &State<authorized_keys::Config<'_>>,
+ authorized_keys_state: &State<authorized_keys::AuthorizedKeys>,
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<Build> {
)
.attach(auth::stage(basepath))
.attach(git_root::stage())
+ .attach(authorized_keys::stage())
}
#[rocket::main]