From 57a5752fe79fc11c74c0e232c544362d9a6db7f6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 14 May 2021 15:16:01 +0200 Subject: [PATCH] Can create reset token from the API --- .../admin/admin_res_create_reset_token.rs | 21 +++++++++++++++++++ src/api_data/admin/mod.rs | 3 ++- .../admin/admin_account_controller.rs | 15 +++++++++++++ src/routes.rs | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/api_data/admin/admin_res_create_reset_token.rs diff --git a/src/api_data/admin/admin_res_create_reset_token.rs b/src/api_data/admin/admin_res_create_reset_token.rs new file mode 100644 index 0000000..13fba04 --- /dev/null +++ b/src/api_data/admin/admin_res_create_reset_token.rs @@ -0,0 +1,21 @@ +//! # Result of creation of a reset token +//! +//! @author Pierre Hubert + +use crate::data::admin::AdminResetToken; +use serde::Serialize; + +#[derive(Serialize)] +pub struct AdminResCreateResetToken { + token: String, + expire: u64, +} + +impl AdminResCreateResetToken { + pub fn new(token: AdminResetToken) -> Self { + Self { + token: token.token, + expire: token.expire, + } + } +} \ No newline at end of file diff --git a/src/api_data/admin/mod.rs b/src/api_data/admin/mod.rs index b12dfd3..133100b 100644 --- a/src/api_data/admin/mod.rs +++ b/src/api_data/admin/mod.rs @@ -6,4 +6,5 @@ pub mod admin_auth_options; pub mod admin_auth_success; pub mod admin_id_api; pub mod admin_info_api; -pub mod admin_keys_api; \ No newline at end of file +pub mod admin_keys_api; +pub mod admin_res_create_reset_token; \ 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 a49b86c..ec9f9dc 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -9,6 +9,7 @@ use crate::api_data::admin::admin_auth_success::AdminAuthSuccess; use crate::api_data::admin::admin_id_api::AdminIDAPI; use crate::api_data::admin::admin_info_api::AdminInfoAPI; use crate::api_data::admin::admin_keys_api::AdminKeyAPI; +use crate::api_data::admin::admin_res_create_reset_token::AdminResCreateResetToken; use crate::data::admin::{AdminKey, NewAdminGeneralSettings}; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::error::Res; @@ -124,6 +125,20 @@ pub fn update_general_settings(r: &mut HttpRequestHandler) -> RequestResult { r.ok() } +/// Generate access reset token +pub fn generate_reset_token(r: &mut HttpRequestHandler) -> RequestResult { + let admin_id = r.post_admin_id("id")?; + + if admin_id != r.admin_id()? { + // TODO + unimplemented!(); + } + + let token = admin_account_helper::create_new_reset_token(admin_id)?; + + r.set_response(AdminResCreateResetToken::new(token)) +} + /// Generate a challenge to register a new key pub fn challenge_register_key(r: &mut HttpRequestHandler) -> RequestResult { let mut wan = get_wan(); diff --git a/src/routes.rs b/src/routes.rs index 935a6fa..8e49c19 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -354,6 +354,7 @@ pub fn get_routes() -> Vec { Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)), Route::admin_post("/admin/accounts/keys", Box::new(admin_account_controller::get_keys_list)), Route::admin_post("/admin/accounts/update_general_settings", Box::new(admin_account_controller::update_general_settings)), + Route::admin_post("/admin/accounts/generate_reset_token", Box::new(admin_account_controller::generate_reset_token)), 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)), Route::admin_post("/admin/accounts/delete_auth_key", Box::new(admin_account_controller::delete_auth_key)),