2021-12-17 16:43:31 +01:00
|
|
|
/*
|
2022-03-07 18:27:54 +01:00
|
|
|
* Copyright © 2022 RLKM UG (haftungsbeschränkt).
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2021-12-17 16:43:31 +01:00
|
|
|
*/
|
|
|
|
|
2022-03-15 16:28:11 +01:00
|
|
|
use anyhow::Context;
|
|
|
|
use lmdb::Environment;
|
|
|
|
use once_cell::sync::OnceCell;
|
2022-03-13 17:29:21 +01:00
|
|
|
use rkyv::{Archive, Deserialize, Infallible, Serialize};
|
2022-03-15 16:28:11 +01:00
|
|
|
use std::collections::HashMap;
|
2022-03-10 20:52:34 +01:00
|
|
|
use std::ops::Deref;
|
2022-03-13 22:50:37 +01:00
|
|
|
use std::path::Path;
|
2022-03-10 20:52:34 +01:00
|
|
|
use std::sync::Arc;
|
2021-11-26 22:11:24 +01:00
|
|
|
|
2022-03-13 17:29:21 +01:00
|
|
|
pub mod db;
|
2021-12-06 21:53:42 +01:00
|
|
|
|
2022-03-08 18:56:03 +01:00
|
|
|
pub use crate::authentication::db::PassDB;
|
2022-03-15 17:52:47 +01:00
|
|
|
use crate::authorization::roles::Role;
|
2022-03-15 16:28:11 +01:00
|
|
|
use crate::db::LMDBorrow;
|
2022-03-13 22:50:37 +01:00
|
|
|
use crate::users::db::UserData;
|
2022-03-15 16:28:11 +01:00
|
|
|
use crate::UserDB;
|
2021-11-26 22:11:24 +01:00
|
|
|
|
2022-03-13 17:29:21 +01:00
|
|
|
#[derive(
|
|
|
|
Clone,
|
|
|
|
PartialEq,
|
|
|
|
Eq,
|
|
|
|
Debug,
|
|
|
|
Archive,
|
|
|
|
Serialize,
|
|
|
|
Deserialize,
|
|
|
|
serde::Serialize,
|
|
|
|
serde::Deserialize,
|
|
|
|
)]
|
2022-03-13 22:50:37 +01:00
|
|
|
#[archive_attr(derive(Debug, PartialEq))]
|
2022-03-15 17:52:47 +01:00
|
|
|
pub struct UserRef {
|
2022-03-13 22:50:37 +01:00
|
|
|
id: String,
|
2021-11-26 22:11:24 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 17:52:47 +01:00
|
|
|
impl UserRef {
|
2022-03-13 22:50:37 +01:00
|
|
|
pub fn new(id: String) -> Self {
|
2022-03-15 17:52:47 +01:00
|
|
|
UserRef { id }
|
2021-12-17 16:43:31 +01:00
|
|
|
}
|
2022-03-13 17:29:21 +01:00
|
|
|
|
|
|
|
pub fn get_username(&self) -> &str {
|
2022-03-13 22:50:37 +01:00
|
|
|
self.id.as_str()
|
2022-03-13 17:29:21 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-13 22:50:37 +01:00
|
|
|
|
2022-03-15 16:28:11 +01:00
|
|
|
static USERDB: OnceCell<UserDB> = OnceCell::new();
|
2022-03-13 22:50:37 +01:00
|
|
|
|
2022-03-15 16:28:11 +01:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[repr(transparent)]
|
2022-03-13 22:50:37 +01:00
|
|
|
pub struct Users {
|
2022-03-15 16:28:11 +01:00
|
|
|
userdb: &'static UserDB,
|
2022-03-13 22:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Users {
|
|
|
|
pub fn new(env: Arc<Environment>) -> anyhow::Result<Self> {
|
2022-03-15 16:28:11 +01:00
|
|
|
let span = tracing::debug_span!("users", ?env, "Creating Users handle");
|
|
|
|
let _guard = span.enter();
|
|
|
|
|
|
|
|
let userdb = USERDB
|
|
|
|
.get_or_try_init(|| {
|
|
|
|
tracing::debug!("Global resource not yet initialized, initializing…");
|
2022-03-15 17:52:47 +01:00
|
|
|
unsafe { UserDB::create(env) }
|
2022-03-15 16:28:11 +01:00
|
|
|
})
|
|
|
|
.context("Failed to open userdb")?;
|
|
|
|
|
|
|
|
Ok(Self { userdb })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_user(&self, uid: &str) -> Option<db::User> {
|
|
|
|
tracing::trace!(uid, "Looking up user");
|
|
|
|
self.userdb
|
|
|
|
.get(uid)
|
|
|
|
.unwrap()
|
|
|
|
.map(|user| Deserialize::<db::User, _>::deserialize(user.deref(), &mut Infallible).unwrap())
|
2022-03-13 22:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_file<P: AsRef<Path>>(&self, path: P) -> anyhow::Result<()> {
|
|
|
|
let f = std::fs::read(path)?;
|
|
|
|
let mut map: HashMap<String, UserData> = toml::from_slice(&f)?;
|
|
|
|
|
|
|
|
for (uid, mut userdata) in map {
|
2022-03-15 16:28:11 +01:00
|
|
|
userdata.passwd = userdata.passwd.map(|pw| {
|
|
|
|
if !pw.starts_with("$argon2") {
|
|
|
|
let config = argon2::Config::default();
|
|
|
|
let salt: [u8; 16] = rand::random();
|
|
|
|
let hash = argon2::hash_encoded(pw.as_bytes(), &salt, &config)
|
|
|
|
.expect(&format!("Failed to hash password for {}: ", uid));
|
|
|
|
tracing::debug!("Hashed pw for {} to {}", uid, hash);
|
|
|
|
|
|
|
|
hash
|
|
|
|
} else {
|
|
|
|
pw
|
|
|
|
}
|
2022-03-13 22:50:37 +01:00
|
|
|
});
|
2022-03-15 16:28:11 +01:00
|
|
|
let user = db::User {
|
|
|
|
id: uid.clone(),
|
|
|
|
userdata,
|
|
|
|
};
|
2022-03-13 22:50:37 +01:00
|
|
|
tracing::trace!(%uid, ?user, "Storing user object");
|
2022-03-15 16:28:11 +01:00
|
|
|
self.userdb.put(uid.as_str(), &user);
|
2022-03-13 22:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-03-15 16:28:11 +01:00
|
|
|
}
|