2019-08-11 19:48:02 +00:00
|
|
|
use hashbrown::HashMap;
|
2019-08-08 22:24:14 +00:00
|
|
|
use log::{info, warn};
|
|
|
|
|
|
|
|
pub struct AuthProvider {
|
|
|
|
accounts: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AuthProvider {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
AuthProvider {
|
|
|
|
accounts: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn query(&mut self, username: String, password: String) -> bool {
|
2019-08-09 01:28:25 +00:00
|
|
|
let pwd = password.clone();
|
|
|
|
if self.accounts.entry(username.clone()).or_insert_with(|| {
|
|
|
|
info!("Registered new user '{}'", &username);
|
|
|
|
pwd
|
|
|
|
}) == &password
|
|
|
|
{
|
2019-08-08 22:24:14 +00:00
|
|
|
info!("User '{}' successfully authenticated", username);
|
2019-08-09 01:28:25 +00:00
|
|
|
true
|
2019-08-08 22:24:14 +00:00
|
|
|
} else {
|
2019-08-09 01:28:25 +00:00
|
|
|
warn!(
|
|
|
|
"User '{}' attempted to log in with invalid password '{}'!",
|
|
|
|
username, password
|
|
|
|
);
|
|
|
|
false
|
2019-08-08 22:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|