71 lines
1.7 KiB
Rust
71 lines
1.7 KiB
Rust
use std::error::Error;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use tokio::{fs::File, io::AsyncReadExt};
|
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
|
pub struct Proxy {
|
|
pub listen: String,
|
|
pub port: u16,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
|
pub struct Db {
|
|
pub host: String,
|
|
pub user: String,
|
|
pub port: Option<u16>,
|
|
pub password: Option<String>,
|
|
pub database: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
|
pub struct Api {
|
|
pub listen: String,
|
|
pub port: u16,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
|
pub struct Client {
|
|
pub name: String,
|
|
pub hashed_secret: String,
|
|
}
|
|
|
|
impl Client {
|
|
pub async fn verify(us: String, config: Config) -> bool {
|
|
// us stands for user:secret btw
|
|
let us_split: Vec<&str> = us.split(':').collect();
|
|
|
|
let name = us_split.first().unwrap();
|
|
let secret = us_split.last().unwrap();
|
|
|
|
let client: Client = config
|
|
.clients
|
|
.into_iter()
|
|
.filter(|x| x.name.eq(name))
|
|
.nth(0)
|
|
.unwrap();
|
|
|
|
bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
|
pub struct Config {
|
|
pub database: String,
|
|
pub proxy: Proxy,
|
|
pub api: Api,
|
|
pub clients: Vec<Client>,
|
|
}
|
|
|
|
impl Config {
|
|
pub async fn get() -> Result<Self, Box<dyn Error>> {
|
|
let mut file = File::open("./config.yaml").await?;
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents).await?;
|
|
|
|
let config: Self = serde_yaml_bw::from_str::<Self>(&contents)?;
|
|
|
|
Ok(config)
|
|
}
|
|
}
|