2020-01-01 20:17:43 +00:00
|
|
|
use authc::{AuthClient, AuthToken, Uuid};
|
2020-01-07 06:27:18 +00:00
|
|
|
use common::msg::RegisterError;
|
2019-08-11 19:48:02 +00:00
|
|
|
use hashbrown::HashMap;
|
2020-01-11 21:04:49 +00:00
|
|
|
use log::error;
|
2019-12-21 17:02:39 +00:00
|
|
|
use std::str::FromStr;
|
2019-08-08 22:24:14 +00:00
|
|
|
|
2020-01-01 20:17:43 +00:00
|
|
|
fn derive_uuid(username: &str) -> Uuid {
|
2020-05-12 23:08:33 +00:00
|
|
|
let mut state = 144066263297769815596495629667062367629;
|
|
|
|
|
|
|
|
for byte in username.as_bytes() {
|
|
|
|
state ^= *byte as u128;
|
|
|
|
state = state.wrapping_mul(309485009821345068724781371);
|
2020-01-01 20:17:43 +00:00
|
|
|
}
|
2020-05-12 23:08:33 +00:00
|
|
|
|
|
|
|
Uuid::from_slice(&state.to_be_bytes()).unwrap()
|
2020-01-01 20:17:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 22:24:14 +00:00
|
|
|
pub struct AuthProvider {
|
2020-01-02 08:43:45 +00:00
|
|
|
accounts: HashMap<Uuid, String>,
|
2019-12-21 17:02:39 +00:00
|
|
|
auth_server: Option<AuthClient>,
|
2019-08-08 22:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AuthProvider {
|
2019-12-21 17:02:39 +00:00
|
|
|
pub fn new(auth_addr: Option<String>) -> Self {
|
|
|
|
let auth_server = match auth_addr {
|
|
|
|
Some(addr) => Some(AuthClient::new(addr)),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2019-08-08 22:24:14 +00:00
|
|
|
AuthProvider {
|
|
|
|
accounts: HashMap::new(),
|
2019-12-21 17:02:39 +00:00
|
|
|
auth_server,
|
2019-08-08 22:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:04:49 +00:00
|
|
|
pub fn logout(&mut self, uuid: Uuid) {
|
|
|
|
if self.accounts.remove(&uuid).is_none() {
|
|
|
|
error!("Attempted to logout user that is not logged in.");
|
|
|
|
};
|
|
|
|
}
|
2020-01-11 19:50:35 +00:00
|
|
|
|
2020-01-11 21:04:49 +00:00
|
|
|
pub fn query(&mut self, username_or_token: String) -> Result<(String, Uuid), RegisterError> {
|
2019-12-21 17:02:39 +00:00
|
|
|
// Based on whether auth server is provided or not we expect an username or
|
|
|
|
// token
|
|
|
|
match &self.auth_server {
|
|
|
|
// Token from auth server expected
|
|
|
|
Some(srv) => {
|
|
|
|
log::info!("Validating '{}' token.", &username_or_token);
|
2020-01-07 06:27:18 +00:00
|
|
|
// Parse token
|
|
|
|
let token = AuthToken::from_str(&username_or_token)
|
|
|
|
.map_err(|e| RegisterError::AuthError(e.to_string()))?;
|
|
|
|
// Validate token
|
|
|
|
let uuid = srv.validate(token)?;
|
|
|
|
// Check if already logged in
|
|
|
|
if self.accounts.contains_key(&uuid) {
|
|
|
|
return Err(RegisterError::AlreadyLoggedIn);
|
2019-12-21 17:02:39 +00:00
|
|
|
}
|
2020-01-07 06:27:18 +00:00
|
|
|
// Log in
|
2020-01-11 21:04:49 +00:00
|
|
|
let username = srv.uuid_to_username(uuid)?;
|
|
|
|
self.accounts.insert(uuid, username.clone());
|
|
|
|
Ok((username, uuid))
|
2019-12-21 17:02:39 +00:00
|
|
|
},
|
|
|
|
// Username is expected
|
|
|
|
None => {
|
2020-01-02 08:43:45 +00:00
|
|
|
// Assume username was provided
|
|
|
|
let username = username_or_token;
|
|
|
|
let uuid = derive_uuid(&username);
|
|
|
|
if !self.accounts.contains_key(&uuid) {
|
|
|
|
log::info!("New User '{}'", username);
|
2020-01-11 21:04:49 +00:00
|
|
|
self.accounts.insert(uuid, username.clone());
|
|
|
|
Ok((username, uuid))
|
2019-12-21 17:02:39 +00:00
|
|
|
} else {
|
2020-01-07 06:27:18 +00:00
|
|
|
Err(RegisterError::AlreadyLoggedIn)
|
2019-12-21 17:02:39 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-08 22:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|