//! # 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, AdminKey}; 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("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")?, time_add: row.get_u64("time_add")?, key: serde_json::from_str(&row.get_str("credential")?)?, }) }