1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can register a new key

This commit is contained in:
Pierre HUBERT 2021-05-14 10:58:11 +02:00
parent 70860ab184
commit 3838cf3e03
9 changed files with 67 additions and 6 deletions

View File

@ -283,9 +283,9 @@ CREATE TABLE `comunic_admin` (
CREATE TABLE `comunic_admin_key` ( CREATE TABLE `comunic_admin_key` (
`id` INT NOT NULL, `id` INT NOT NULL AUTO_INCREMENT,
`admin_id` INT NULL, `admin_id` INT NULL,
`name` VARCHAR(45) NULL, `name` VARCHAR(45) NULL,
`time_add` INT NULL, `time_add` INT NULL,
`key` TEXT NULL, `security_key` TEXT NULL,
PRIMARY KEY (`id`)); PRIMARY KEY (`id`));

View File

@ -9,9 +9,9 @@ CREATE TABLE `comunic_admin` (
PRIMARY KEY (`id`)); PRIMARY KEY (`id`));
CREATE TABLE `comunic_admin_key` ( CREATE TABLE `comunic_admin_key` (
`id` INT NOT NULL, `id` INT NOT NULL AUTO_INCREMENT,
`admin_id` INT NULL, `admin_id` INT NULL,
`name` VARCHAR(45) NULL, `name` VARCHAR(45) NULL,
`time_add` INT NULL, `time_add` INT NULL,
`key` TEXT NULL, `security_key` TEXT NULL,
PRIMARY KEY (`id`)); PRIMARY KEY (`id`));

View File

@ -12,7 +12,7 @@ use crate::data::admin::NewAdminGeneralSettings;
use crate::data::base_request_handler::BaseRequestHandler; use crate::data::base_request_handler::BaseRequestHandler;
use crate::data::http_request_handler::HttpRequestHandler; use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::webauthn_config::get_wan; 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::routes::RequestResult;
use crate::utils::date_utils::time; use crate::utils::date_utils::time;
@ -104,3 +104,21 @@ pub fn challenge_register_key(r: &mut HttpRequestHandler) -> RequestResult {
r.set_response(res) 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()
}

View File

@ -551,6 +551,13 @@ pub trait BaseRequestHandler {
Ok(list) 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<webauthn_rs::proto::RegisterPublicKeyCredential> {
let str = self.post_string(name)?;
Ok(serde_json::from_str(&str)?)
}
/// Get the ID of a user included in a POST request /// Get the ID of a user included in a POST request
fn post_user_id(&mut self, name: &str) -> ResultBoxError<UserID> { fn post_user_id(&mut self, name: &str) -> ResultBoxError<UserID> {
let user_id = UserID::new(self.post_u64(name)?); let user_id = UserID::new(self.post_u64(name)?);

View File

@ -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()
}

View File

@ -32,3 +32,11 @@ pub fn set(admin: AdminID, key: RegistrationState) -> Res {
Ok(()) Ok(())
} }
pub fn get(admin: AdminID) -> Res<Option<RegistrationState>> {
let cache = unsafe {
CACHE.as_ref().unwrap().lock()
};
Ok(cache?.remove(&admin))
}

View File

@ -692,6 +692,11 @@ impl InsertQuery {
self 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 { pub fn add_group_id(mut self, key: &str, value: &GroupID) -> InsertQuery {
self.values.insert(key.to_string(), Value::from(value.id())); self.values.insert(key.to_string(), Value::from(value.id()));
self self

View File

@ -23,5 +23,6 @@ pub mod independent_push_notifications_service_helper;
pub mod firebase_notifications_helper; pub mod firebase_notifications_helper;
pub mod forez_presence_helper; pub mod forez_presence_helper;
pub mod admin_account_helper; pub mod admin_account_helper;
pub mod admin_account_key_helper;
pub mod admin_access_token_helper; pub mod admin_access_token_helper;
pub mod admin_key_registration_challenges_helper; pub mod admin_key_registration_challenges_helper;

View File

@ -353,6 +353,7 @@ pub fn get_routes() -> Vec<Route> {
Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)), 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/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/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)),
] ]
} }