Add a separate client binary, with some common code to the server.

The cargo workspace thing was a PITA to figure out, but it seems to be working.  Could maybe be simplified?
This commit is contained in:
Barry Pederson
2024-11-02 15:58:35 -07:00
parent 46ad77f19e
commit 076e48f457
16 changed files with 380 additions and 255 deletions
+94
View File
@@ -0,0 +1,94 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use crate::options::OPTIONS;
use common::Request;
#[derive(Clone)]
pub struct FileHandler;
impl FileHandler {
pub fn new() -> FileHandler {
FileHandler {}
}
pub fn check_auth(&self, request: &Request) -> bool {
// Don't allow empty userids or passwords
//
if request.userid.is_empty() || request.userid.contains('/') || request.password.is_empty()
{
return false;
}
let mut file_name = OPTIONS.password_dir.clone();
file_name.push(&request.userid);
if let Ok(file) = File::open(file_name) {
for line in BufReader::new(file).lines().map_while(Result::ok) {
if !line.starts_with('#') && line.trim() == request.password {
return true;
}
}
}
false
}
}
#[cfg(test)]
mod tests {
use crate::handler::FileHandler;
use common::Request;
#[test]
fn it_checks_good_password() {
let handler = FileHandler::new();
let request = Request {
userid: "bp".to_string(),
password: "some-rad-password".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(handler.check_auth(&request), true);
}
#[test]
fn it_fails_bad_password() {
let handler = FileHandler::new();
let request = Request {
userid: "bp".to_string(),
password: "not-correct".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(handler.check_auth(&request), false);
}
#[test]
fn it_fails_bad_userid() {
let handler = FileHandler::new();
let request = Request {
userid: "bp-xxx".to_string(),
password: "some-rad-password".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(handler.check_auth(&request), false);
}
#[test]
fn it_ignores_comments() {
let handler = FileHandler::new();
let request = Request {
userid: "bp".to_string(),
password: "commented-out".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(handler.check_auth(&request), false);
}
}