working in memory storage
This commit is contained in:
parent
3a1f68c6bf
commit
2830e62df9
2 changed files with 30 additions and 11 deletions
|
@ -24,6 +24,7 @@ pub struct UserDatabase {
|
||||||
|
|
||||||
pub struct StaticLoginProvider {
|
pub struct StaticLoginProvider {
|
||||||
user_db: watch::Receiver<UserDatabase>,
|
user_db: watch::Receiver<UserDatabase>,
|
||||||
|
in_memory_store: tokio::sync::Mutex<HashMap<String, Arc<storage::in_memory::MemBuilder>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_user_list(config: PathBuf, up: watch::Sender<UserDatabase>) -> Result<()> {
|
pub async fn update_user_list(config: PathBuf, up: watch::Sender<UserDatabase>) -> Result<()> {
|
||||||
|
@ -68,7 +69,7 @@ impl StaticLoginProvider {
|
||||||
tokio::spawn(update_user_list(config.user_list, tx));
|
tokio::spawn(update_user_list(config.user_list, tx));
|
||||||
rx.changed().await?;
|
rx.changed().await?;
|
||||||
|
|
||||||
Ok(Self { user_db: rx })
|
Ok(Self { user_db: rx, in_memory_store: tokio::sync::Mutex::new(HashMap::new()) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,10 +77,12 @@ impl StaticLoginProvider {
|
||||||
impl LoginProvider for StaticLoginProvider {
|
impl LoginProvider for StaticLoginProvider {
|
||||||
async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
|
async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
|
||||||
tracing::debug!(user=%username, "login");
|
tracing::debug!(user=%username, "login");
|
||||||
let user_db = self.user_db.borrow();
|
let user = {
|
||||||
let user = match user_db.users.get(username) {
|
let user_db = self.user_db.borrow();
|
||||||
None => bail!("User {} does not exist", username),
|
match user_db.users.get(username) {
|
||||||
Some(u) => u,
|
None => bail!("User {} does not exist", username),
|
||||||
|
Some(u) => u.clone(),
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
tracing::debug!(user=%username, "verify password");
|
tracing::debug!(user=%username, "verify password");
|
||||||
|
@ -89,7 +92,13 @@ impl LoginProvider for StaticLoginProvider {
|
||||||
|
|
||||||
tracing::debug!(user=%username, "fetch keys");
|
tracing::debug!(user=%username, "fetch keys");
|
||||||
let storage: storage::Builder = match &user.config.storage {
|
let storage: storage::Builder = match &user.config.storage {
|
||||||
StaticStorage::InMemory => storage::in_memory::MemBuilder::new(username),
|
StaticStorage::InMemory => {
|
||||||
|
let mut global_storage = self.in_memory_store.lock().await;
|
||||||
|
global_storage
|
||||||
|
.entry(username.to_string())
|
||||||
|
.or_insert(storage::in_memory::MemBuilder::new(username))
|
||||||
|
.clone()
|
||||||
|
},
|
||||||
StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf {
|
StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf {
|
||||||
region: grgconf.aws_region.clone(),
|
region: grgconf.aws_region.clone(),
|
||||||
k2v_endpoint: grgconf.k2v_endpoint.clone(),
|
k2v_endpoint: grgconf.k2v_endpoint.clone(),
|
||||||
|
@ -108,14 +117,23 @@ impl LoginProvider for StaticLoginProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn public_login(&self, email: &str) -> Result<PublicCredentials> {
|
async fn public_login(&self, email: &str) -> Result<PublicCredentials> {
|
||||||
let user_db = self.user_db.borrow();
|
let user = {
|
||||||
let user = match user_db.users_by_email.get(email) {
|
let user_db = self.user_db.borrow();
|
||||||
None => bail!("No user for email address {}", email),
|
match user_db.users_by_email.get(email) {
|
||||||
Some(u) => u,
|
None => bail!("Email {} does not exist", email),
|
||||||
|
Some(u) => u.clone(),
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
tracing::debug!(user=%user.username, "public_login");
|
||||||
|
|
||||||
let storage: storage::Builder = match &user.config.storage {
|
let storage: storage::Builder = match &user.config.storage {
|
||||||
StaticStorage::InMemory => storage::in_memory::MemBuilder::new(&user.username),
|
StaticStorage::InMemory => {
|
||||||
|
let mut global_storage = self.in_memory_store.lock().await;
|
||||||
|
global_storage
|
||||||
|
.entry(user.username.to_string())
|
||||||
|
.or_insert(storage::in_memory::MemBuilder::new(&user.username))
|
||||||
|
.clone()
|
||||||
|
},
|
||||||
StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf {
|
StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf {
|
||||||
region: grgconf.aws_region.clone(),
|
region: grgconf.aws_region.clone(),
|
||||||
k2v_endpoint: grgconf.k2v_endpoint.clone(),
|
k2v_endpoint: grgconf.k2v_endpoint.clone(),
|
||||||
|
|
|
@ -78,6 +78,7 @@ pub struct MemBuilder {
|
||||||
|
|
||||||
impl MemBuilder {
|
impl MemBuilder {
|
||||||
pub fn new(user: &str) -> Arc<Self> {
|
pub fn new(user: &str) -> Arc<Self> {
|
||||||
|
tracing::debug!("initialize membuilder for {}", user);
|
||||||
let mut unicity: Vec<u8> = vec![];
|
let mut unicity: Vec<u8> = vec![];
|
||||||
unicity.extend_from_slice(file!().as_bytes());
|
unicity.extend_from_slice(file!().as_bytes());
|
||||||
unicity.extend_from_slice(user.as_bytes());
|
unicity.extend_from_slice(user.as_bytes());
|
||||||
|
|
Loading…
Reference in a new issue