From 9dc06c21efbec6c14f388078761c3a2f7cd9093e Mon Sep 17 00:00:00 2001 From: curly Date: Tue, 27 Aug 2024 15:59:41 -0600 Subject: UseRegistrationKeys feature --- src/db.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 7 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index d4a24d7..c758e32 100644 --- a/src/db.rs +++ b/src/db.rs @@ -8,12 +8,18 @@ use std::{fs::File, io::Write}; use crate::uid::{self, UID}; +#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] +pub enum Features { + UseRegistrationKeys +} + #[derive(Serialize, Deserialize, Clone)] pub struct Config { dbsave: PathBuf, address: IpAddr, port: u16, base_tokens: Vec, + features: Vec, } impl Config { pub fn new() -> Config { @@ -28,6 +34,9 @@ impl Config { Token::new(Color::Blue, 5), Token::new(Color::Yellow, 5), ], + features: vec![ + Features::UseRegistrationKeys + ] } } @@ -61,6 +70,10 @@ impl Config { pub fn address(&self) -> IpAddr { self.address } + + pub fn useregistrationkeys(&self) -> bool { + self.features.contains(&Features::UseRegistrationKeys) + } } #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] @@ -331,6 +344,10 @@ impl DB { } pub async fn use_key(&mut self, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let mut result = Err("Could not find key".into()); let new_vec = self.registration_keys.clone().into_iter().filter_map(|k| { if *key == k { @@ -346,6 +363,10 @@ impl DB { } fn add_key(&mut self, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + if self.registration_keys.contains(key) { Err("Key already exists".into()) } else { @@ -355,6 +376,10 @@ impl DB { } pub async fn new_registration_key(&mut self, id: UID, session: &String, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let u = self.get_user_authenticated(id, session).await?; if u.is_admin() { self.add_key(key)?; @@ -365,6 +390,10 @@ impl DB { } pub async fn del_registration_key(&mut self, id: UID, session: &String, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let u = self.get_user_authenticated(id, session).await?; if u.is_admin() { self.use_key(key).await?; @@ -375,6 +404,10 @@ impl DB { } pub async fn list_registration_keys(&self, id: UID, session: &String) -> Result, String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let u = self.get_user_authenticated(id, session).await?; if u.is_admin() { Ok(self.registration_keys.clone()) @@ -383,26 +416,46 @@ impl DB { } } - pub async fn new_user(&mut self, name: String, password: String, id: UID, key: &String) -> Result { - if self.use_key(key).await.is_ok() { - let mut is_admin = false; - if key == "ADMIN" { + pub async fn new_user(&mut self, name: String, password: String, id: UID, key: &Option) -> Result { + let ok: bool; + let mut is_admin = false; + + if self.config.useregistrationkeys() { + let key = match key.clone() { + Some(n) => n, + None => return Err("Key is required".into()), + }; + + if self.use_key(&key).await.is_ok() { + ok = true; + if key == "ADMIN" { + is_admin = true; + } + } else { + return Err("Invalid key".into()) + } + + } else { + ok = true; + if self.users.len() == 0 { is_admin = true; } + } + + if ok { let user = User::new(name, password, id, is_admin, self.config.base_tokens.clone()); self.users.push(user.clone()); match self.save().await { Ok(_) => Ok(user), Err(n) => { - self.add_key(key)?; + self.add_key(&key.clone().unwrap())?; Err(n) }, } } else { - Err("Invalid key".into()) + Err("Could not make new user".into()) } - } pub async fn login(&mut self, id: UID, password: &String, clientid: &String) -> Result { -- cgit v1.2.3