Load passdb \o/

This commit is contained in:
Gregor Reitzenstein 2020-12-16 14:25:03 +01:00
parent d568d46212
commit d4da25b0d7
3 changed files with 28 additions and 0 deletions

1
examples/pass.toml Normal file
View File

@ -0,0 +1 @@
Testuser = "secret"

View File

@ -1,4 +1,7 @@
use std::sync::Arc;
use std::path::Path;
use std::fs;
use std::collections::HashMap;
use argon2;
use lmdb::{Environment, Transaction, RwTransaction, Cursor};
@ -53,4 +56,24 @@ impl PassDB {
txn.put(self.db, &authcid.as_bytes(), &hash.as_bytes(), lmdb::WriteFlags::empty())
.map_err(Into::into)
}
pub fn insert_multiple(&self, vec: Vec<(String, String)>) -> Result<()> {
let mut txn = self.env.begin_rw_txn()?;
for (authcid, password) in vec.iter() {
self.store_with_txn(&mut txn, authcid.as_ref(), password.as_bytes())?;
}
txn.commit()?;
let v: Vec<&String> = vec.iter().map(|(a,_)| a).collect();
debug!(self.log, "Loaded passwords for: {:?}", v);
Ok(())
}
pub fn load_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let f = fs::read(path)?;
let mut map: HashMap<String, String> = toml::from_slice(&f)?;
self.insert_multiple(map.drain().collect())
}
}

View File

@ -146,6 +146,10 @@ fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> Result<(), Error> {
db.access.internal.load_roles(&dir)?;
dir.pop();
dir.push("pass.toml");
db.passdb.load_file(&dir);
dir.pop();
Ok(())
} else {
let ex = Executor::new();