1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-26 05:28:50 +00:00

Return keys listing in auth options

This commit is contained in:
Pierre HUBERT 2021-05-14 11:12:41 +02:00
parent 3838cf3e03
commit 210dcb9597
6 changed files with 37 additions and 9 deletions

View File

@ -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`));

View File

@ -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`));

View File

@ -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<AuthKey>,
}
impl AdminAuthOptions {
pub fn new(admin: &Admin) -> Self {
pub fn new(admin: &Admin, keys: &Vec<AdminKey>) -> 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(),
}
}
}

View File

@ -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

View File

@ -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

View File

@ -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<Vec<AdminKey>> {
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<AdminKey> {
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")?)?,
})
}