WIP: trying to refactor to separate password-checking from network-communication

This commit is contained in:
Barry Pederson
2024-11-02 11:05:16 -07:00
parent 15daa7d7a1
commit ac9743dc8a
5 changed files with 140 additions and 75 deletions
+35
View File
@@ -0,0 +1,35 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use crate::options::OPTIONS;
use crate::server::{Handler, Request};
pub struct FileHandler;
impl FileHandler {
pub fn new() -> FileHandler {}
}
impl Handler for FileHandler {
pub fn check_auth(&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().flatten() {
if !line.starts_with('#') && line.trim() == request.password {
return true;
}
}
}
false
}
}