use super::ApplicationName;
use super::Authenticate;
use parsec_interface::requests::request::RequestAuth;
use parsec_interface::requests::{ResponseStatus, Result};
use std::str;
#[derive(Copy, Clone, Debug)]
pub struct SimpleAuthenticator;
impl Authenticate for SimpleAuthenticator {
fn authenticate(&self, auth: &RequestAuth) -> Result<ApplicationName> {
if auth.is_empty() {
Ok(ApplicationName(String::from("root")))
} else {
match str::from_utf8(auth.bytes()) {
Ok(str) => Ok(ApplicationName(String::from(str))),
Err(_) => Err(ResponseStatus::AuthenticationError),
}
}
}
}
#[cfg(test)]
mod test {
use super::super::Authenticate;
use super::SimpleAuthenticator;
use parsec_interface::requests::request::RequestAuth;
#[test]
fn successful_authentication() {
let authenticator = SimpleAuthenticator {};
let app_name = "app_name".to_string();
let req_auth = RequestAuth::from_bytes(app_name.clone().into_bytes());
let auth_name = authenticator
.authenticate(&req_auth)
.expect("Failed to authenticate");
assert_eq!(auth_name.get_name(), app_name);
}
#[test]
#[should_panic(expected = "Failed to authenticate")]
fn failed_authentication() {
let authenticator = SimpleAuthenticator {};
let _ = authenticator
.authenticate(&RequestAuth::from_bytes(vec![0xff; 5]))
.expect("Failed to authenticate");
}
#[test]
fn auth_root() {
let authenticator = SimpleAuthenticator {};
let auth_name = authenticator
.authenticate(&RequestAuth::from_bytes(Vec::new()))
.expect("Failed to authenticate");
assert_eq!(auth_name.get_name(), "root");
}
}