diff --git a/docs/db_struct.sql b/docs/db_struct.sql index 409ab2a..8c068cf 100644 --- a/docs/db_struct.sql +++ b/docs/db_struct.sql @@ -287,5 +287,5 @@ CREATE TABLE `comunic_admin_key` ( `admin_id` INT NULL, `name` VARCHAR(45) NULL, `time_add` INT NULL, - `security_key` TEXT NULL, + `credential` TEXT NULL, PRIMARY KEY (`id`)); \ No newline at end of file diff --git a/docs/migration.sql b/docs/migration.sql index 406316e..6b84161 100644 --- a/docs/migration.sql +++ b/docs/migration.sql @@ -13,5 +13,5 @@ CREATE TABLE `comunic_admin_key` ( `admin_id` INT NULL, `name` VARCHAR(45) NULL, `time_add` INT NULL, - `security_key` TEXT NULL, + `credential` TEXT NULL, PRIMARY KEY (`id`)); diff --git a/src/api_data/admin/admin_auth_options.rs b/src/api_data/admin/admin_auth_options.rs index 5ab52aa..d1eeec3 100644 --- a/src/api_data/admin/admin_auth_options.rs +++ b/src/api_data/admin/admin_auth_options.rs @@ -4,17 +4,25 @@ use serde::Serialize; -use crate::data::admin::Admin; +use crate::data::admin::{Admin, AdminKey}; + +#[derive(Serialize)] +struct AuthKey { + name: String, + id: u64, +} #[derive(Serialize)] pub struct AdminAuthOptions { reset_token: bool, + keys: Vec, } impl AdminAuthOptions { - pub fn new(admin: &Admin) -> Self { + pub fn new(admin: &Admin, keys: &Vec) -> Self { Self { - reset_token: admin.reset_token.is_some() + reset_token: admin.reset_token.is_some(), + keys: keys.iter().map(|k| AuthKey { id: k.id, name: k.name.to_string() }).collect(), } } } \ No newline at end of file diff --git a/src/controllers/admin/admin_account_controller.rs b/src/controllers/admin/admin_account_controller.rs index f4ffbb0..493b370 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -20,8 +20,9 @@ use crate::utils::date_utils::time; pub fn get_auth_options(r: &mut HttpRequestHandler) -> RequestResult { let mail = r.post_email("mail")?; let admin = admin_account_helper::find_admin_by_email(&mail)?; + let keys = admin_account_key_helper::get_admin_keys(admin.id)?; - r.set_response(AdminAuthOptions::new(&admin)) + r.set_response(AdminAuthOptions::new(&admin, &keys)) } /// Login admin using a reset token diff --git a/src/data/admin.rs b/src/data/admin.rs index 22f8230..ebcc3d7 100644 --- a/src/data/admin.rs +++ b/src/data/admin.rs @@ -2,6 +2,8 @@ //! //! @author Pierre Hubert +use webauthn_rs::proto::Credential; + #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] pub struct AdminID(u64); @@ -41,7 +43,7 @@ pub struct AdminKey { pub id: u64, pub admin_id: AdminID, pub name: String, - pub key: String, + pub key: Credential, } /// Admin access token diff --git a/src/helpers/admin_account_key_helper.rs b/src/helpers/admin_account_key_helper.rs index 761bf55..d3198c8 100644 --- a/src/helpers/admin_account_key_helper.rs +++ b/src/helpers/admin_account_key_helper.rs @@ -5,7 +5,7 @@ use webauthn_rs::proto::Credential; use crate::constants::database_tables_names::ADMIN_KEYS_TABLE; -use crate::data::admin::AdminID; +use crate::data::admin::{AdminID, AdminKey}; use crate::data::error::Res; use crate::helpers::database; use crate::utils::date_utils::time; @@ -16,6 +16,23 @@ pub fn add_key(id: AdminID, name: &str, key: Credential) -> Res { .add_admin_id("admin_id", id) .add_str("name", name) .add_u64("time_add", time()) - .add_str("security_key", &serde_json::to_string(&key)?) + .add_str("credential", &serde_json::to_string(&key)?) .insert_drop_result() +} + +/// Get the list of keys of a given admin +pub fn get_admin_keys(id: AdminID) -> Res> { + database::QueryInfo::new(ADMIN_KEYS_TABLE) + .cond_admin_id("admin_id", id) + .exec(db_to_admin_key) +} + +/// Turn database entry into an AdminKey structure +fn db_to_admin_key(row: &database::RowResult) -> Res { + Ok(AdminKey { + id: row.get_u64("id")?, + admin_id: row.get_admin_id("admin_id")?, + name: row.get_str("name")?, + key: serde_json::from_str(&row.get_str("credential")?)?, + }) } \ No newline at end of file