From 5abd4979a3887be6b5cd36430e1a7f57a0d3e2ed Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 14 May 2021 15:08:39 +0200 Subject: [PATCH] Can remove a key from the list of keys of an admin --- .../admin/admin_account_controller.rs | 20 +++++++++++++++++++ src/helpers/admin_account_key_helper.rs | 7 +++++++ src/routes.rs | 1 + 3 files changed, 28 insertions(+) diff --git a/src/controllers/admin/admin_account_controller.rs b/src/controllers/admin/admin_account_controller.rs index a6d255f..a49b86c 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -153,6 +153,26 @@ pub fn register_key(r: &mut HttpRequestHandler) -> RequestResult { r.ok() } +/// Delete an admin auth key +pub fn delete_auth_key(r: &mut HttpRequestHandler) -> RequestResult { + let admin_id = r.post_admin_id("adminID")?; + let key_id = r.post_u64("keyID")?; + + if admin_id != r.admin_id()? { + unimplemented!(); // TODO + } + + for key in admin_account_key_helper::get_admin_keys(admin_id)? { + if key.id == key_id { + admin_account_key_helper::delete_key(key)?; + + return r.ok(); + } + } + + r.not_found("Requested key was not found!".to_string()) +} + /// Generate a challenge to authenticate with a security key pub fn challenge_auth_with_key(r: &mut HttpRequestHandler) -> RequestResult { let key = r.post_admin_auth_key("mail", "key_id")?; diff --git a/src/helpers/admin_account_key_helper.rs b/src/helpers/admin_account_key_helper.rs index 99f53d6..3f90e5f 100644 --- a/src/helpers/admin_account_key_helper.rs +++ b/src/helpers/admin_account_key_helper.rs @@ -27,6 +27,13 @@ pub fn get_admin_keys(id: AdminID) -> Res> { .exec(db_to_admin_key) } +/// Remove a key from the database +pub fn delete_key(key: AdminKey) -> Res { + database::DeleteQuery::new(ADMIN_KEYS_TABLE) + .cond_u64("id", key.id) + .exec() +} + /// Turn database entry into an AdminKey structure fn db_to_admin_key(row: &database::RowResult) -> Res { Ok(AdminKey { diff --git a/src/routes.rs b/src/routes.rs index c999597..935a6fa 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -356,6 +356,7 @@ pub fn get_routes() -> Vec { Route::admin_post("/admin/accounts/update_general_settings", Box::new(admin_account_controller::update_general_settings)), Route::admin_post("/admin/accounts/challenge_register_key", Box::new(admin_account_controller::challenge_register_key)), Route::admin_post("/admin/accounts/register_key", Box::new(admin_account_controller::register_key)), + Route::admin_post("/admin/accounts/delete_auth_key", Box::new(admin_account_controller::delete_auth_key)), Route::limited_admin_post_without_login("/admin/accounts/challenge_auth_with_key", Box::new(admin_account_controller::challenge_auth_with_key), LimitPolicy::ANY(10)), Route::limited_admin_post_without_login("/admin/accounts/auth_with_key", Box::new(admin_account_controller::auth_with_key), LimitPolicy::ANY(10)), ]