diff options
author | curly <curly@infernal.garden> | 2024-08-27 15:59:41 -0600 |
---|---|---|
committer | curly <curly@infernal.garden> | 2024-08-27 15:59:41 -0600 |
commit | 9dc06c21efbec6c14f388078761c3a2f7cd9093e (patch) | |
tree | e6b9190db9ce885166be6f650733ee536fbdf6e9 /src | |
parent | 3d8d55ecc531f6db73fcf86ac44ce28b9bf097c9 (diff) | |
download | poko_server-master.tar.gz poko_server-master.tar.bz2 poko_server-master.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 67 | ||||
-rw-r--r-- | src/main.rs | 2 |
2 files changed, 61 insertions, 8 deletions
@@ -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<Token>, + features: Vec<Features>, } 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<Vec<String>, 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<User, String> { - 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<String>) -> Result<User, String> { + 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<String, String> { diff --git a/src/main.rs b/src/main.rs index 88123df..9d7654b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,7 +79,7 @@ struct RegisterForm { name: String, password: String, id: Option<UID>, - key: String, + key: Option<String>, } #[post("/register", data="<data>", format="json")] async fn new_user(data: Json<RegisterForm>, db: &State<Mutex<DB>>) -> (Status, Result<Json<UserOut>, Json<String>>) { |