From 3838cf3e031e5d541dad4d1ab7a6a35151af3df8 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 14 May 2021 10:58:11 +0200 Subject: [PATCH] Can register a new key --- docs/db_struct.sql | 4 ++-- docs/migration.sql | 4 ++-- .../admin/admin_account_controller.rs | 20 +++++++++++++++++- src/data/base_request_handler.rs | 7 +++++++ src/helpers/admin_account_key_helper.rs | 21 +++++++++++++++++++ ...dmin_key_registration_challenges_helper.rs | 8 +++++++ src/helpers/database.rs | 5 +++++ src/helpers/mod.rs | 1 + src/routes.rs | 3 ++- 9 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 src/helpers/admin_account_key_helper.rs diff --git a/docs/db_struct.sql b/docs/db_struct.sql index 37781a0..409ab2a 100644 --- a/docs/db_struct.sql +++ b/docs/db_struct.sql @@ -283,9 +283,9 @@ CREATE TABLE `comunic_admin` ( CREATE TABLE `comunic_admin_key` ( - `id` INT NOT NULL, + `id` INT NOT NULL AUTO_INCREMENT, `admin_id` INT NULL, `name` VARCHAR(45) NULL, `time_add` INT NULL, - `key` TEXT NULL, + `security_key` TEXT NULL, PRIMARY KEY (`id`)); \ No newline at end of file diff --git a/docs/migration.sql b/docs/migration.sql index 6dd7003..406316e 100644 --- a/docs/migration.sql +++ b/docs/migration.sql @@ -9,9 +9,9 @@ CREATE TABLE `comunic_admin` ( PRIMARY KEY (`id`)); CREATE TABLE `comunic_admin_key` ( - `id` INT NOT NULL, + `id` INT NOT NULL AUTO_INCREMENT, `admin_id` INT NULL, `name` VARCHAR(45) NULL, `time_add` INT NULL, - `key` TEXT NULL, + `security_key` TEXT NULL, PRIMARY KEY (`id`)); diff --git a/src/controllers/admin/admin_account_controller.rs b/src/controllers/admin/admin_account_controller.rs index 34541b1..f4ffbb0 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -12,7 +12,7 @@ use crate::data::admin::NewAdminGeneralSettings; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::webauthn_config::get_wan; -use crate::helpers::{admin_access_token_helper, admin_account_helper, admin_key_registration_challenges_helper}; +use crate::helpers::{admin_access_token_helper, admin_account_helper, admin_account_key_helper, admin_key_registration_challenges_helper}; use crate::routes::RequestResult; use crate::utils::date_utils::time; @@ -103,4 +103,22 @@ pub fn challenge_register_key(r: &mut HttpRequestHandler) -> RequestResult { admin_key_registration_challenges_helper::set(r.admin_id()?, state)?; r.set_response(res) +} + +/// Register key +pub fn register_key(r: &mut HttpRequestHandler) -> RequestResult { + let name = r.post_string("name")?; + + let creds = r.post_register_public_key_credential("key")?; + let state = r.some_or_internal_error( + admin_key_registration_challenges_helper::get(r.admin_id()?)?, + "No challenge found!", + )?; + + let wan = get_wan(); + let key = wan.register_credential(creds, state, |_| Ok(false))?; + + admin_account_key_helper::add_key(r.admin_id()?, &name, key)?; + + r.ok() } \ No newline at end of file diff --git a/src/data/base_request_handler.rs b/src/data/base_request_handler.rs index f6b60bf..28048b7 100644 --- a/src/data/base_request_handler.rs +++ b/src/data/base_request_handler.rs @@ -551,6 +551,13 @@ pub trait BaseRequestHandler { Ok(list) } + /// Get the response to a key register credential included in the request + fn post_register_public_key_credential(&mut self, name: &str) -> Res { + let str = self.post_string(name)?; + + Ok(serde_json::from_str(&str)?) + } + /// Get the ID of a user included in a POST request fn post_user_id(&mut self, name: &str) -> ResultBoxError { let user_id = UserID::new(self.post_u64(name)?); diff --git a/src/helpers/admin_account_key_helper.rs b/src/helpers/admin_account_key_helper.rs new file mode 100644 index 0000000..761bf55 --- /dev/null +++ b/src/helpers/admin_account_key_helper.rs @@ -0,0 +1,21 @@ +//! # Admin account key helper +//! +//! @author Pierre Hubert + +use webauthn_rs::proto::Credential; + +use crate::constants::database_tables_names::ADMIN_KEYS_TABLE; +use crate::data::admin::AdminID; +use crate::data::error::Res; +use crate::helpers::database; +use crate::utils::date_utils::time; + +/// Save a new key in the database +pub fn add_key(id: AdminID, name: &str, key: Credential) -> Res { + database::InsertQuery::new(ADMIN_KEYS_TABLE) + .add_admin_id("admin_id", id) + .add_str("name", name) + .add_u64("time_add", time()) + .add_str("security_key", &serde_json::to_string(&key)?) + .insert_drop_result() +} \ No newline at end of file diff --git a/src/helpers/admin_key_registration_challenges_helper.rs b/src/helpers/admin_key_registration_challenges_helper.rs index 6e1443a..46fef83 100644 --- a/src/helpers/admin_key_registration_challenges_helper.rs +++ b/src/helpers/admin_key_registration_challenges_helper.rs @@ -31,4 +31,12 @@ pub fn set(admin: AdminID, key: RegistrationState) -> Res { cache?.insert(admin, key); Ok(()) +} + +pub fn get(admin: AdminID) -> Res> { + let cache = unsafe { + CACHE.as_ref().unwrap().lock() + }; + + Ok(cache?.remove(&admin)) } \ No newline at end of file diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 006a58c..0188301 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -692,6 +692,11 @@ impl InsertQuery { self } + pub fn add_admin_id(mut self, key: &str, value: AdminID) -> InsertQuery { + self.values.insert(key.to_string(), Value::from(value.id())); + self + } + pub fn add_group_id(mut self, key: &str, value: &GroupID) -> InsertQuery { self.values.insert(key.to_string(), Value::from(value.id())); self diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 6edd76d..bf6402e 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -23,5 +23,6 @@ pub mod independent_push_notifications_service_helper; pub mod firebase_notifications_helper; pub mod forez_presence_helper; pub mod admin_account_helper; +pub mod admin_account_key_helper; pub mod admin_access_token_helper; pub mod admin_key_registration_challenges_helper; \ No newline at end of file diff --git a/src/routes.rs b/src/routes.rs index e0c05f2..b44bc3e 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -353,6 +353,7 @@ pub fn get_routes() -> Vec { Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)), Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)), 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/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)), ] } \ No newline at end of file