Identity provider and SSO for Kupolo.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

93 lines
2.8 KiB

use std::collections::HashMap;
use local::store::{User, UserStore, DefaultUserStore, LuaUserStore};
use rocket;
use toml;
use std::path::Path;
use std::fs::File;
use std::io::Read;
#[derive(Debug, Deserialize)]
pub struct Config {
pub url: Option<String>,
pub keyfile: Option<String>,
pub user_store: Option<UserStoreConfig>,
pub web: Option<WebConfig>
}
#[derive(Debug, Deserialize)]
pub struct UserStoreConfig {
#[serde(rename = "type")]
type_name: String,
// Used for in-memory userstore
users: Option<HashMap<String, String>>,
// Used for lua userstore
file: Option<String>
}
#[derive(Debug, Deserialize)]
pub struct WebConfig {
address: Option<String>,
port: Option<u16>,
workers: Option<u16>
}
impl Config {
pub fn load_from_file<P: AsRef<Path>>(config_file_name: P) -> Result<Config, toml::de::Error> {
let config_path = config_file_name.as_ref();
if let Ok(mut file) = File::open(&config_path) {
let mut contents = String::new();
if let Err(e) = file.read_to_string(&mut contents) {
panic!("Failed to open config file {}: {:?}", config_path.display(), e);
} else {
toml::from_str(&contents)
}
} else {
panic!("Failed to open config file {}! Does it exist?", config_path.display());
}
}
pub fn create_user_store (&self) -> Option<Box<UserStore>> {
self.user_store.as_ref().and_then(|store_config| store_config.create_user_store())
}
}
impl UserStoreConfig {
fn create_user_store (&self) -> Option<Box<UserStore>> {
match self.type_name.as_ref() {
"memory" => Some(Box::new(self.create_in_memory_user_store()) as Box<UserStore>),
"lua" => Some(Box::new(LuaUserStore::new(self.file.as_ref().expect("Missing required parameter: file")))),
_ => None
}
}
fn create_in_memory_user_store (&self) -> DefaultUserStore {
let mut user_store = DefaultUserStore::new();
if let Some(ref users) = self.users {
for (username, password) in users.iter() {
user_store.add(User {
username: username.clone()
}, password);
}
}
user_store
}
}
impl<'a> From<&'a WebConfig> for rocket::Config {
fn from(web_config: &'a WebConfig) -> rocket::Config {
let mut rocket_config = rocket::Config::development().unwrap();
if let Some(ref address) = web_config.address {
rocket_config.set_address(address.to_owned());
}
if let Some(port) = web_config.port {
rocket_config.set_port(port);
}
if let Some(workers) = web_config.workers {
rocket_config.set_workers(workers);
}
rocket_config
}
}