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:
@@ -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<Listener, Error> {
|
||||
pub fn new(handler: PasswordDirectory) -> Result<Listener, Error> {
|
||||
if OPTIONS.socket_name.exists() {
|
||||
std::fs::remove_file(&OPTIONS.socket_name)?;
|
||||
}
|
||||
|
||||
+4
-3
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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<Opt> = LazyLock::new(Opt::parse);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<Server, Error> {
|
||||
pub fn new(stream: UnixStream, handler: PasswordDirectory) -> Result<Server, Error> {
|
||||
Ok(Server { stream, handler })
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user