use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use std::io::{Error, ErrorKind, Result};
use pasetoken_lib::ConfigPaSeToken;
use pasetors::footer::Footer;
use crate::{
CFG_FILE_EXTENSION,
FILE_SCHEME,
defs::{
Local, TotpAlgorithm, TotpMode,
deserialize_totp_algorithm,
deserialize_totp_mode,
},
};
use std::path::Path;
fn default_config_resource() -> String {
"/".to_string()
}
fn default_config_array_resource() -> Vec<String> {
Vec::new()
}
fn default_config_serv_paths()-> Vec<ServPath> {
Vec::new()
}
fn default_config_dflt_lang() -> String {
"en".to_string()
}
fn default_config_org() -> String {
"".to_string()
}
fn default_config_empty() -> String {
"".to_string()
}
fn default_config_locales() -> HashMap<String,Local> {
HashMap::new()
}
fn default_config_tpls() -> HashMap<String,String> {
HashMap::new()
}
fn default_is_restricted() -> bool {
false
}
fn default_config_use_mail() -> bool {
false
}
fn default_config_use_random() -> bool {
false
}
fn default_config_invite_expire() -> u64 {
300
}
fn default_config_web_menu_items() -> Vec<WebMenuItem> {
Vec::new()
}
fn default_web_menu_item_roles() -> Vec<String> {
Vec::new()
}
fn default_sub_menu_items() -> Vec<SubMenuItem> {
Vec::new()
}
fn default_config_totp_digits() -> usize {
6
}
fn default_config_totp_algorithm() -> TotpAlgorithm {
TotpAlgorithm::default()
}
fn default_config_totp_mode() -> TotpMode {
TotpMode::default()
}
fn default_config_password_score() -> u8 {
0
}
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct SubMenuItem {
#[serde(default = "default_config_empty")]
pub typ: String,
#[serde(default = "default_config_empty")]
pub srctyp: String,
#[serde(default = "default_config_empty")]
pub text: String,
#[serde(default = "default_config_empty")]
pub url: String,
#[serde(default = "default_web_menu_item_roles")]
pub roles: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct WebMenuItem {
#[serde(default = "default_config_empty")]
pub typ: String,
#[serde(default = "default_config_empty")]
pub srctyp: String,
#[serde(default = "default_config_empty")]
pub text: String,
#[serde(default = "default_config_empty")]
pub url: String,
#[serde(default = "default_web_menu_item_roles")]
pub roles: Vec<String>,
#[serde(default = "default_sub_menu_items")]
pub items: Vec<SubMenuItem>,
}
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct UiConfig {
#[serde(default = "default_config_empty")]
pub css_link: String,
#[serde(default = "default_config_empty")]
pub js_link: String,
#[serde(default = "default_config_empty")]
pub other_css_link: String,
#[serde(default = "default_config_empty")]
pub other_js_link: String,
#[serde(default = "default_config_empty")]
pub main_js_link: String,
#[serde(default = "default_config_empty")]
pub utils_js_link: String,
#[serde(default = "default_config_web_menu_items")]
pub web_menu_items: Vec<WebMenuItem>,
}
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct ServPath {
#[serde(default = "default_config_empty")]
pub src_path: String,
#[serde(default = "default_config_empty")]
pub url_path: String,
#[serde(default = "default_config_empty")]
pub not_found: String,
#[serde(default = "default_config_empty")]
pub not_auth: String,
#[serde(default = "default_is_restricted")]
pub is_restricted: bool,
#[serde(default = "default_config_empty")]
pub redirect_to: String,
}
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct Config {
pub hostport: String,
pub bind: String,
pub port: u16,
pub protocol: String,
#[serde(default = "default_config_org")]
pub org: String,
pub name: String,
pub verbose: u8,
pub prefix: String,
pub resources_path: String,
pub cert_file: String,
pub key_file: String,
#[serde(default = "default_config_array_resource")]
pub allow_origin: Vec<String>,
#[serde(default = "default_config_array_resource")]
pub langs: Vec<String>,
#[serde(default = "default_config_dflt_lang")]
pub dflt_lang: String,
#[serde(default = "default_config_empty")]
pub path_locales_config: String,
#[serde(default = "default_config_locales")]
pub locales: HashMap<String,Local>,
#[serde(default = "default_config_resource")]
pub root_path: String,
#[serde(default = "default_config_resource")]
pub defaults_path: String,
#[serde(default = "default_config_serv_paths")]
pub serv_paths: Vec<ServPath>,
#[serde(default = "default_config_resource")]
pub docs_index: String,
#[serde(default = "default_config_resource")]
pub templates_path: String,
#[serde(default = "default_config_resource")]
pub html_url: String,
#[serde(default = "default_config_resource")]
pub assets_url: String,
#[serde(default = "default_config_resource")]
pub users_store_uri: String,
#[serde(default = "default_config_empty")]
pub user_store_access: String,
#[serde(default = "default_config_empty")]
pub signup_mode: String,
#[serde(default = "default_config_invite_expire")]
pub invite_expire: u64,
#[serde(default = "default_config_use_random")]
pub use_random: bool,
#[serde(default = "default_config_totp_digits")]
pub totp_digits: usize,
#[serde(default = "default_config_totp_mode", deserialize_with = "deserialize_totp_mode")]
pub totp_mode: TotpMode,
#[serde(default = "default_config_totp_algorithm", deserialize_with = "deserialize_totp_algorithm")]
pub totp_algorithm: TotpAlgorithm,
#[serde(default = "default_config_password_score")]
pub password_score: u8,
#[serde(default = "default_config_empty")]
pub admin_fields: String,
#[serde(default = "default_config_empty")]
pub mail_from: String,
#[serde(default = "default_config_empty")]
pub mail_reply_to: String,
#[serde(default = "default_config_use_mail")]
pub use_mail: bool,
#[serde(default = "default_config_empty")]
pub smtp: String,
#[serde(default = "default_config_empty")]
pub smtp_auth: String,
#[cfg(feature = "authstore")]
#[serde(default = "default_config_resource")]
pub authz_store_uri: String,
#[cfg(feature = "casbin")]
#[serde(default = "default_config_empty")]
pub authz_model_path: String,
#[cfg(feature = "casbin")]
#[serde(default = "default_config_empty")]
pub authz_policy_path: String,
#[serde(default = "default_config_empty")]
pub session_store_uri: String,
#[serde(default = "default_config_empty")]
pub session_store_file: String,
pub session_expire: u64,
pub paseto: ConfigPaSeToken,
pub ui: UiConfig,
#[serde(default = "default_config_tpls")]
pub tpls: HashMap<String,String>,
}
impl Config {
fn fix_root_path<Config>(&mut self, root_path: String ) {
if root_path != self.root_path {
self.root_path = root_path.to_owned();
}
if self.root_path.is_empty() || ! Path::new(&self.root_path).exists() {
return;
}
if self.users_store_uri.starts_with(FILE_SCHEME) {
self.users_store_uri = format!("{}{}",
FILE_SCHEME, self.fix_item_path(self.users_store_uri.replace(FILE_SCHEME,"")));
}
if self.session_store_uri.starts_with(FILE_SCHEME) {
self.session_store_uri = format!("{}{}",
FILE_SCHEME, self.fix_item_path(self.session_store_uri.replace(FILE_SCHEME,"")));
}
self.cert_file = self.fix_item_path(self.cert_file.to_owned());
self.key_file = self.fix_item_path(self.key_file.to_owned());
self.templates_path = self.fix_item_path(self.templates_path.to_owned());
self.defaults_path = self.fix_item_path(self.defaults_path.to_owned());
self.paseto.public_path = self.fix_item_path(self.paseto.public_path.to_owned());
self.paseto.secret_path = self.fix_item_path(self.paseto.secret_path.to_owned());
#[cfg(feature = "authstore")]
if self.authz_store_uri.starts_with(FILE_SCHEME) {
self.authz_store_uri = format!("{}{}",
FILE_SCHEME, self.fix_item_path(self.authz_store_uri.replace(FILE_SCHEME,"")));
}
#[cfg(feature = "casbin")]
{
self.authz_model_path = self.fix_item_path(self.authz_model_path.to_owned());
self.authz_policy_path = self.fix_item_path(self.authz_policy_path.to_owned());
}
(self.paseto.public_data, self.paseto.secret_data) = self.paseto.load_data();
self.paseto.footer = ConfigPaSeToken::make_footer(
self.paseto.map_footer.to_owned()
).unwrap_or(Footer::new());
}
fn fix_item_path(&mut self, item: String ) -> String {
if !item.is_empty() && ! Path::new(&item).exists() {
format!("{}/{}",&self.root_path,&item)
} else {
item
}
}
pub fn load_items(&mut self ) {
self.fix_root_path::<Config>(self.root_path.to_owned());
}
#[allow(dead_code)]
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|e|{
println!("Error to convert Config to json: {}",e);
String::from("")
})
}
#[allow(dead_code)]
pub fn full_html_url(&self, url: &str) -> String {
format!("{}://{}:{}/{}",
&self.protocol,&self.bind,&self.port,
url
)
}
pub fn load_from_file<'a>(file_cfg: &str, name: &str) -> Result<Self> {
let item_cfg: Self;
let file_path: String;
if file_cfg.contains(CFG_FILE_EXTENSION) {
file_path = file_cfg.to_string();
} else {
file_path = format!("{}{}",file_cfg.to_string(),CFG_FILE_EXTENSION);
}
let config_content: &'a str;
match std::fs::read_to_string(&file_path) {
Ok(cfgcontent) => config_content = Box::leak(cfgcontent.into_boxed_str()),
Err(e) =>
return Err(Error::new(
ErrorKind::InvalidInput,
format!("Error read {}: {}",&file_path,e)
)),
};
match toml::from_str::<Self>(&config_content) {
Ok(cfg) => item_cfg = cfg,
Err(e) => return Err(Error::new(
ErrorKind::InvalidInput,
format!("Error loading config {}: {}",&file_path,e)
)),
};
log::info!("Loaded {} config from: {}", &name, &file_path);
Ok(item_cfg)
}
}