diff --git a/server/src/listener.rs b/server/src/listener.rs index 52d69d1..86de97f 100644 --- a/server/src/listener.rs +++ b/server/src/listener.rs @@ -1,5 +1,5 @@ -use crate::handler::FileHandler; use crate::options::OPTIONS; +use crate::repository::PasswordDirectory; use crate::server::Server; use std::fs::{metadata, set_permissions}; use std::io::Error; @@ -9,11 +9,11 @@ use std::thread; pub struct Listener { listener: UnixListener, - handler: FileHandler, + handler: PasswordDirectory, } impl Listener { - pub fn new(handler: FileHandler) -> Result { + pub fn new(handler: PasswordDirectory) -> Result { if OPTIONS.socket_name.exists() { std::fs::remove_file(&OPTIONS.socket_name)?; } diff --git a/server/src/main.rs b/server/src/main.rs index a552a10..c7e785c 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,12 +1,13 @@ -mod handler; mod listener; pub mod options; +mod repository; mod server; -use crate::handler::FileHandler; use crate::listener::Listener; +use crate::options::OPTIONS; +use crate::repository::PasswordDirectory; fn main() -> std::io::Result<()> { - let mut listener = Listener::new(FileHandler::new())?; + let mut listener = Listener::new(PasswordDirectory::new(&OPTIONS.password_dir))?; listener.run() } diff --git a/server/src/options.rs b/server/src/options.rs index 99d1afa..efd5e89 100644 --- a/server/src/options.rs +++ b/server/src/options.rs @@ -13,8 +13,8 @@ pub struct Opt { )] pub socket_name: PathBuf, - #[arg(short = 'p', long = "password-dir", default_value = "./passwords")] - pub password_dir: PathBuf, + #[arg(short = 'p', long = "password-dir", default_value = "./test_passwords")] + pub password_dir: String, } pub static OPTIONS: LazyLock = LazyLock::new(Opt::parse); diff --git a/server/src/handler.rs b/server/src/repository.rs similarity index 67% rename from server/src/handler.rs rename to server/src/repository.rs index c9967b7..032854f 100644 --- a/server/src/handler.rs +++ b/server/src/repository.rs @@ -1,26 +1,29 @@ +use common::Request; use std::fs::File; use std::io::{BufRead, BufReader}; - -use crate::options::OPTIONS; -use common::Request; +use std::path::PathBuf; #[derive(Clone)] -pub struct FileHandler; +pub struct PasswordDirectory { + dir_path: PathBuf, +} -impl FileHandler { - pub fn new() -> FileHandler { - FileHandler {} +impl PasswordDirectory { + pub fn new(path: &str) -> PasswordDirectory { + PasswordDirectory { + dir_path: PathBuf::from(path), + } } pub fn check_auth(&self, request: &Request) -> bool { - // Don't allow empty userids or passwords + // Don't allow empty userids or test_passwords // if request.userid.is_empty() || request.userid.contains('/') || request.password.is_empty() { return false; } - let mut file_name = OPTIONS.password_dir.clone(); + let mut file_name = self.dir_path.clone(); file_name.push(&request.userid); if let Ok(file) = File::open(file_name) { @@ -37,12 +40,14 @@ impl FileHandler { #[cfg(test)] mod tests { - use crate::handler::FileHandler; + use crate::repository::PasswordDirectory; use common::Request; + const TEST_PATH: &str = "./test_passwords"; + #[test] fn it_checks_good_password() { - let handler = FileHandler::new(); + let repository = PasswordDirectory::new(TEST_PATH); let request = Request { userid: "bp".to_string(), password: "some-rad-password".to_string(), @@ -50,12 +55,12 @@ mod tests { realm: "also-ignore-this".to_string(), }; - assert_eq!(handler.check_auth(&request), true); + assert_eq!(repository.check_auth(&request), true); } #[test] fn it_fails_bad_password() { - let handler = FileHandler::new(); + let repository = PasswordDirectory::new(TEST_PATH); let request = Request { userid: "bp".to_string(), password: "not-correct".to_string(), @@ -63,12 +68,12 @@ mod tests { realm: "also-ignore-this".to_string(), }; - assert_eq!(handler.check_auth(&request), false); + assert_eq!(repository.check_auth(&request), false); } #[test] fn it_fails_bad_userid() { - let handler = FileHandler::new(); + let repository = PasswordDirectory::new(TEST_PATH); let request = Request { userid: "bp-xxx".to_string(), password: "some-rad-password".to_string(), @@ -76,12 +81,12 @@ mod tests { realm: "also-ignore-this".to_string(), }; - assert_eq!(handler.check_auth(&request), false); + assert_eq!(repository.check_auth(&request), false); } #[test] fn it_ignores_comments() { - let handler = FileHandler::new(); + let repository = PasswordDirectory::new(TEST_PATH); let request = Request { userid: "bp".to_string(), password: "commented-out".to_string(), @@ -89,6 +94,6 @@ mod tests { realm: "also-ignore-this".to_string(), }; - assert_eq!(handler.check_auth(&request), false); + assert_eq!(repository.check_auth(&request), false); } } diff --git a/server/src/server.rs b/server/src/server.rs index f44f8c2..3884d14 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -1,15 +1,15 @@ -use crate::handler::FileHandler; +use crate::repository::PasswordDirectory; use common::{Request, RESPONSE_NO, RESPONSE_OK}; use std::io::{Error, Write}; use std::os::unix::net::UnixStream; pub struct Server { stream: UnixStream, - handler: FileHandler, + handler: PasswordDirectory, } impl Server { - pub fn new(stream: UnixStream, handler: FileHandler) -> Result { + pub fn new(stream: UnixStream, handler: PasswordDirectory) -> Result { Ok(Server { stream, handler }) } diff --git a/passwords/bp b/test_passwords/bp similarity index 100% rename from passwords/bp rename to test_passwords/bp