Implements a first bit of User management.

This commit is contained in:
Nadja Reitzenstein
2022-04-27 20:19:04 +02:00
parent fe992a9446
commit 46e3552e04
5 changed files with 82 additions and 25 deletions

View File

@ -36,6 +36,22 @@ impl User {
Ok(false)
}
}
pub fn new_with_plain_pw(username: &str, password: impl AsRef<[u8]>) -> Self {
let config = argon2::Config::default();
let salt: [u8; 16] = rand::random();
let hash = argon2::hash_encoded(password.as_ref(), &salt, &config)
.expect(&format!("Failed to hash password for {}: ", username));
tracing::debug!("Hashed pw for {} to {}", username, hash);
User {
id: username.to_string(),
userdata: UserData {
passwd: Some(hash),
.. Default::default()
}
}
}
}
#[derive(
@ -43,6 +59,7 @@ Clone,
PartialEq,
Eq,
Debug,
Default,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
@ -73,6 +90,7 @@ impl UserData {
pub fn new_with_kv(roles: Vec<String>, kv: HashMap<String, String>) -> Self {
Self { roles, kv, passwd: None }
}
}
#[derive(Clone, Debug)]
@ -116,6 +134,13 @@ impl UserDB {
Ok(())
}
pub fn delete(&self, uid: &str) -> Result<(), db::Error> {
let mut txn = self.env.begin_rw_txn()?;
self.db.del(&mut txn, &uid)?;
txn.commit()?;
Ok(())
}
pub fn get_all(&self) -> Result<Vec<(String, User)>, db::Error> {
let txn = self.env.begin_ro_txn()?;
let iter = self.db.get_all(&txn)?;

View File

@ -98,6 +98,11 @@ impl Users {
self.userdb.put(uid, user)
}
pub fn del_user(&self, uid: &str) -> Result<(), lmdb::Error> {
tracing::trace!(uid, "Deleting user");
self.userdb.delete(uid)
}
pub fn load_file<P: AsRef<Path>>(&self, path: P) -> anyhow::Result<()> {
let f = std::fs::read(path)?;
let map: HashMap<String, UserData> = toml::from_slice(&f)?;