2024-11-03 10:28:14 -08:00
|
|
|
use common::Request;
|
2024-11-02 11:05:16 -07:00
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::{BufRead, BufReader};
|
2024-11-03 10:28:14 -08:00
|
|
|
use std::path::PathBuf;
|
2024-11-02 11:05:16 -07:00
|
|
|
|
2024-11-02 13:49:07 -07:00
|
|
|
#[derive(Clone)]
|
2024-11-03 10:28:14 -08:00
|
|
|
pub struct PasswordDirectory {
|
|
|
|
|
dir_path: PathBuf,
|
|
|
|
|
}
|
2024-11-02 11:05:16 -07:00
|
|
|
|
2024-11-03 10:28:14 -08:00
|
|
|
impl PasswordDirectory {
|
|
|
|
|
pub fn new(path: &str) -> PasswordDirectory {
|
|
|
|
|
PasswordDirectory {
|
|
|
|
|
dir_path: PathBuf::from(path),
|
|
|
|
|
}
|
2024-11-02 13:49:07 -07:00
|
|
|
}
|
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-03 10:28:14 -08:00
|
|
|
// Don't allow empty userids or test_passwords
|
2024-11-02 11:05:16 -07:00
|
|
|
//
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 10:28:14 -08:00
|
|
|
let mut file_name = self.dir_path.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 {
|
2024-11-03 10:28:14 -08:00
|
|
|
use crate::repository::PasswordDirectory;
|
2024-11-02 15:58:35 -07:00
|
|
|
use common::Request;
|
2024-11-02 13:49:07 -07:00
|
|
|
|
2024-11-03 10:28:14 -08:00
|
|
|
const TEST_PATH: &str = "./test_passwords";
|
|
|
|
|
|
2024-11-02 13:49:07 -07:00
|
|
|
#[test]
|
|
|
|
|
fn it_checks_good_password() {
|
2024-11-03 10:28:14 -08:00
|
|
|
let repository = PasswordDirectory::new(TEST_PATH);
|
2024-11-02 13:49:07 -07:00
|
|
|
let request = Request {
|
|
|
|
|
userid: "bp".to_string(),
|
|
|
|
|
password: "some-rad-password".to_string(),
|
|
|
|
|
service: "ignore-this".to_string(),
|
|
|
|
|
realm: "also-ignore-this".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-03 10:28:14 -08:00
|
|
|
assert_eq!(repository.check_auth(&request), true);
|
2024-11-02 13:49:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_fails_bad_password() {
|
2024-11-03 10:28:14 -08:00
|
|
|
let repository = PasswordDirectory::new(TEST_PATH);
|
2024-11-02 13:49:07 -07:00
|
|
|
let request = Request {
|
|
|
|
|
userid: "bp".to_string(),
|
|
|
|
|
password: "not-correct".to_string(),
|
|
|
|
|
service: "ignore-this".to_string(),
|
|
|
|
|
realm: "also-ignore-this".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-03 10:28:14 -08:00
|
|
|
assert_eq!(repository.check_auth(&request), false);
|
2024-11-02 13:49:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_fails_bad_userid() {
|
2024-11-03 10:28:14 -08:00
|
|
|
let repository = PasswordDirectory::new(TEST_PATH);
|
2024-11-02 13:49:07 -07:00
|
|
|
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(),
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-03 10:28:14 -08:00
|
|
|
assert_eq!(repository.check_auth(&request), false);
|
2024-11-02 13:49:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_ignores_comments() {
|
2024-11-03 10:28:14 -08:00
|
|
|
let repository = PasswordDirectory::new(TEST_PATH);
|
2024-11-02 13:49:07 -07:00
|
|
|
let request = Request {
|
|
|
|
|
userid: "bp".to_string(),
|
|
|
|
|
password: "commented-out".to_string(),
|
|
|
|
|
service: "ignore-this".to_string(),
|
|
|
|
|
realm: "also-ignore-this".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-03 10:28:14 -08:00
|
|
|
assert_eq!(repository.check_auth(&request), false);
|
2024-11-02 13:49:07 -07:00
|
|
|
}
|
|
|
|
|
}
|