Rename handler.rs to repository.rs for clarity, pass the directory name as a new() parameter rather than have the repository aware of the OPTIONS static

This commit is contained in:
Barry Pederson
2024-11-03 10:28:14 -08:00
parent 52f502f7a6
commit 1a66c8ede5
6 changed files with 35 additions and 29 deletions
+3 -3
View File
@@ -1,5 +1,5 @@
use crate::handler::FileHandler;
use crate::options::OPTIONS; use crate::options::OPTIONS;
use crate::repository::PasswordDirectory;
use crate::server::Server; use crate::server::Server;
use std::fs::{metadata, set_permissions}; use std::fs::{metadata, set_permissions};
use std::io::Error; use std::io::Error;
@@ -9,11 +9,11 @@ use std::thread;
pub struct Listener { pub struct Listener {
listener: UnixListener, listener: UnixListener,
handler: FileHandler, handler: PasswordDirectory,
} }
impl Listener { impl Listener {
pub fn new(handler: FileHandler) -> Result<Listener, Error> { pub fn new(handler: PasswordDirectory) -> Result<Listener, Error> {
if OPTIONS.socket_name.exists() { if OPTIONS.socket_name.exists() {
std::fs::remove_file(&OPTIONS.socket_name)?; std::fs::remove_file(&OPTIONS.socket_name)?;
} }
+4 -3
View File
@@ -1,12 +1,13 @@
mod handler;
mod listener; mod listener;
pub mod options; pub mod options;
mod repository;
mod server; mod server;
use crate::handler::FileHandler;
use crate::listener::Listener; use crate::listener::Listener;
use crate::options::OPTIONS;
use crate::repository::PasswordDirectory;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let mut listener = Listener::new(FileHandler::new())?; let mut listener = Listener::new(PasswordDirectory::new(&OPTIONS.password_dir))?;
listener.run() listener.run()
} }
+2 -2
View File
@@ -13,8 +13,8 @@ pub struct Opt {
)] )]
pub socket_name: PathBuf, pub socket_name: PathBuf,
#[arg(short = 'p', long = "password-dir", default_value = "./passwords")] #[arg(short = 'p', long = "password-dir", default_value = "./test_passwords")]
pub password_dir: PathBuf, pub password_dir: String,
} }
pub static OPTIONS: LazyLock<Opt> = LazyLock::new(Opt::parse); pub static OPTIONS: LazyLock<Opt> = LazyLock::new(Opt::parse);
@@ -1,26 +1,29 @@
use common::Request;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use crate::options::OPTIONS;
use common::Request;
#[derive(Clone)] #[derive(Clone)]
pub struct FileHandler; pub struct PasswordDirectory {
dir_path: PathBuf,
}
impl FileHandler { impl PasswordDirectory {
pub fn new() -> FileHandler { pub fn new(path: &str) -> PasswordDirectory {
FileHandler {} PasswordDirectory {
dir_path: PathBuf::from(path),
}
} }
pub fn check_auth(&self, request: &Request) -> bool { 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() if request.userid.is_empty() || request.userid.contains('/') || request.password.is_empty()
{ {
return false; return false;
} }
let mut file_name = OPTIONS.password_dir.clone(); let mut file_name = self.dir_path.clone();
file_name.push(&request.userid); file_name.push(&request.userid);
if let Ok(file) = File::open(file_name) { if let Ok(file) = File::open(file_name) {
@@ -37,12 +40,14 @@ impl FileHandler {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::handler::FileHandler; use crate::repository::PasswordDirectory;
use common::Request; use common::Request;
const TEST_PATH: &str = "./test_passwords";
#[test] #[test]
fn it_checks_good_password() { fn it_checks_good_password() {
let handler = FileHandler::new(); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request { let request = Request {
userid: "bp".to_string(), userid: "bp".to_string(),
password: "some-rad-password".to_string(), password: "some-rad-password".to_string(),
@@ -50,12 +55,12 @@ mod tests {
realm: "also-ignore-this".to_string(), realm: "also-ignore-this".to_string(),
}; };
assert_eq!(handler.check_auth(&request), true); assert_eq!(repository.check_auth(&request), true);
} }
#[test] #[test]
fn it_fails_bad_password() { fn it_fails_bad_password() {
let handler = FileHandler::new(); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request { let request = Request {
userid: "bp".to_string(), userid: "bp".to_string(),
password: "not-correct".to_string(), password: "not-correct".to_string(),
@@ -63,12 +68,12 @@ mod tests {
realm: "also-ignore-this".to_string(), realm: "also-ignore-this".to_string(),
}; };
assert_eq!(handler.check_auth(&request), false); assert_eq!(repository.check_auth(&request), false);
} }
#[test] #[test]
fn it_fails_bad_userid() { fn it_fails_bad_userid() {
let handler = FileHandler::new(); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request { let request = Request {
userid: "bp-xxx".to_string(), userid: "bp-xxx".to_string(),
password: "some-rad-password".to_string(), password: "some-rad-password".to_string(),
@@ -76,12 +81,12 @@ mod tests {
realm: "also-ignore-this".to_string(), realm: "also-ignore-this".to_string(),
}; };
assert_eq!(handler.check_auth(&request), false); assert_eq!(repository.check_auth(&request), false);
} }
#[test] #[test]
fn it_ignores_comments() { fn it_ignores_comments() {
let handler = FileHandler::new(); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request { let request = Request {
userid: "bp".to_string(), userid: "bp".to_string(),
password: "commented-out".to_string(), password: "commented-out".to_string(),
@@ -89,6 +94,6 @@ mod tests {
realm: "also-ignore-this".to_string(), realm: "also-ignore-this".to_string(),
}; };
assert_eq!(handler.check_auth(&request), false); assert_eq!(repository.check_auth(&request), false);
} }
} }
+3 -3
View File
@@ -1,15 +1,15 @@
use crate::handler::FileHandler; use crate::repository::PasswordDirectory;
use common::{Request, RESPONSE_NO, RESPONSE_OK}; use common::{Request, RESPONSE_NO, RESPONSE_OK};
use std::io::{Error, Write}; use std::io::{Error, Write};
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
pub struct Server { pub struct Server {
stream: UnixStream, stream: UnixStream,
handler: FileHandler, handler: PasswordDirectory,
} }
impl Server { impl Server {
pub fn new(stream: UnixStream, handler: FileHandler) -> Result<Server, Error> { pub fn new(stream: UnixStream, handler: PasswordDirectory) -> Result<Server, Error> {
Ok(Server { stream, handler }) Ok(Server { stream, handler })
} }
View File