From 9cc779744384d0827a4a4a6dfec73d9a25b13c31 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sun, 17 Oct 2021 13:51:05 +0200 Subject: [PATCH] Can register new API clients from command line --- src/helpers/api_helper.rs | 25 +++++++++++++++-- src/main.rs | 59 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/helpers/api_helper.rs b/src/helpers/api_helper.rs index 8fb56af..0d9bc08 100644 --- a/src/helpers/api_helper.rs +++ b/src/helpers/api_helper.rs @@ -2,12 +2,34 @@ use crate::constants::database_tables_names::CLIENTS_TABLE; use crate::data::api_client::APIClient; use crate::data::error::{Res, ResultBoxError}; use crate::helpers::database; -use crate::helpers::database::QueryInfo; +use crate::helpers::database::{QueryInfo, InsertQuery}; /// API helper /// /// @author Pierre Hubert +/// Get the entire list of clients +pub fn get_clients() -> Res> { + QueryInfo::new(CLIENTS_TABLE) + .exec(db_to_client) +} + +/// Register a new client +pub fn register_client(client: &APIClient) -> Res { + let mut query = InsertQuery::new(CLIENTS_TABLE) + .add_str("name", &client.name) + .add_opt_str("domain", client.domain.as_ref()) + .add_opt_str("comment", client.comment.as_ref()) + .add_opt_str("firebase_project_name", client.firebase_project_name.as_ref()) + .add_opt_str("firebase_service_account_file", client.firebase_service_account_file.as_ref()); + + if client.default_expiration_time > 0 { + query = query.add_u64("default_expiration_time", client.default_expiration_time); + } + + query.insert_expect_result() +} + /// Get information about a client, based on its token pub fn get_client(name: &str) -> ResultBoxError { QueryInfo::new(CLIENTS_TABLE) @@ -15,7 +37,6 @@ pub fn get_client(name: &str) -> ResultBoxError { .query_row(db_to_client) } - /// Get information about a client, based on its origin pub fn get_by_origin(name: &str) -> ResultBoxError { QueryInfo::new(CLIENTS_TABLE) diff --git a/src/main.rs b/src/main.rs index 9ae28f4..b53cca1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ use comunic_server::{cleanup_thread, server}; use comunic_server::constants::admin::{ADMIN_ROLES_LIST, AdminRole}; use comunic_server::data::admin::NewAdmin; +use comunic_server::data::api_client::APIClient; use comunic_server::data::config::{conf, Config}; use comunic_server::data::error::Res; use comunic_server::data::user::UserID; -use comunic_server::helpers::{account_helper, admin_account_helper, admin_roles_helper, database}; +use comunic_server::helpers::{account_helper, admin_account_helper, admin_roles_helper, api_helper, database}; use comunic_server::utils::date_utils::current_year; type MainActionFunction = Res; @@ -34,7 +35,23 @@ fn get_actions() -> Vec { function: Box::new(help), }, - // Reset password + // Get the list of registered clients + Action { + name: "clients_list", + description: "Get the list of registered clients", + arguments: vec![], + function: Box::new(list_clients), + }, + + // Register a new API client + Action { + name: "register_client", + description: "Register a new API client", + arguments: vec!["name", "origin", "comment"], + function: Box::new(register_client), + }, + + // Reset a user password Action { name: "reset_password", description: "Create a password reset URL for a user", @@ -170,6 +187,44 @@ fn help(_a: Vec) -> Res { Ok(()) } +fn list_clients(_args: Vec) -> Res { + for client in api_helper::get_clients()? { + println!( + "Client {}\n* Name: {}\n* Domain: {}\n* Comment: {}\n* Default tokens expiration time: {}\n* Firebase project: {}", + client.id, + client.name, + client.domain.unwrap_or("None".to_string()), + client.comment.unwrap_or("None".to_string()), + client.default_expiration_time, + client.firebase_project_name.unwrap_or("None".to_string()) + ); + } + Ok(()) +} + +fn register_client(args: Vec) -> Res { + let client = APIClient { + id: 0, + name: args[0].to_string(), + domain: match args[1].is_empty() { + true => None, + false => Some(args[1].to_string()) + }, + comment: match args[2].is_empty() { + true => None, + false => Some(args[2].to_string()) + }, + default_expiration_time: 0, + firebase_project_name: None, + firebase_service_account_file: None, + }; + + let id = api_helper::register_client(&client)?; + println!("Registered client #{}", id); + + Ok(()) +} + fn reset_password(args: Vec) -> Res { let user_id = UserID::new(args[0].parse::()?); let token = account_helper::generate_password_reset_token(&user_id)?;