2024-11-02 11:05:16 -07:00
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::{BufRead, BufReader};
|
|
|
|
|
|
|
|
|
|
use crate::options::OPTIONS;
|
2024-11-02 15:58:35 -07:00
|
|
|
use common::Request;
|
2024-11-02 11:05:16 -07:00
|
|
|
|
2024-11-02 13:49:07 -07:00
|
|
|
#[derive(Clone)]
|
2024-11-02 11:05:16 -07:00
|
|
|
pub struct FileHandler;
|
|
|
|
|
|
|
|
|
|
impl FileHandler {
|
2024-11-02 13:49:07 -07:00
|
|
|
pub fn new() -> FileHandler {
|
|
|
|
|
FileHandler {}
|
|
|
|
|
}
|
2024-11-02 11:05:16 -07:00
|
|
|
|
2024-11-02 13:49:07 -07:00
|
|
|
pub fn check_auth(&self, request: &Request) -> bool {
|
2024-11-02 11:05:16 -07:00
|
|
|
// Don't allow empty userids or passwords
|
|
|
|
|
//
|
2024-11-02 13:49:07 -07:00
|
|
|
if request.userid.is_empty() || request.userid.contains('/') || request.password.is_empty()
|
|
|
|
|
{
|
2024-11-02 11:05:16 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut file_name = OPTIONS.password_dir.clone();
|
2024-11-02 13:49:07 -07:00
|
|
|
file_name.push(&request.userid);
|
2024-11-02 11:05:16 -07:00
|
|
|
|
|
|
|
|
if let Ok(file) = File::open(file_name) {
|
2024-11-02 15:58:35 -07:00
|
|
|
for line in BufReader::new(file).lines().map_while(Result::ok) {
|
2024-11-02 11:05:16 -07:00
|
|
|
if !line.starts_with('#') && line.trim() == request.password {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-02 13:49:07 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use crate::handler::FileHandler;
|
2024-11-02 15:58:35 -07:00
|
|
|
use common::Request;
|
2024-11-02 13:49:07 -07:00
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
|
}
|
|
|
|
|
}
|