aerogramme/src/login/static_provider.rs

108 lines
3.9 KiB
Rust
Raw Normal View History

2022-05-19 10:10:48 +00:00
use std::collections::HashMap;
use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;
use rusoto_signature::Region;
use crate::config::*;
2022-05-19 12:33:49 +00:00
use crate::cryptoblob::{Key, SecretKey};
2022-05-19 10:10:48 +00:00
use crate::login::*;
pub struct StaticLoginProvider {
default_bucket: Option<String>,
users: HashMap<String, LoginStaticUser>,
k2v_region: Region,
2022-05-19 12:33:49 +00:00
s3_region: Region,
2022-05-19 10:10:48 +00:00
}
impl StaticLoginProvider {
2022-05-19 12:33:49 +00:00
pub fn new(config: LoginStaticConfig, k2v_region: Region, s3_region: Region) -> Result<Self> {
2022-05-19 10:10:48 +00:00
Ok(Self {
default_bucket: config.default_bucket,
users: config.users,
k2v_region,
2022-05-19 12:33:49 +00:00
s3_region,
2022-05-19 10:10:48 +00:00
})
}
}
#[async_trait]
impl LoginProvider for StaticLoginProvider {
async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
2022-06-03 12:00:19 +00:00
tracing::debug!(user=%username, "login");
2022-05-19 10:10:48 +00:00
match self.users.get(username) {
None => bail!("User {} does not exist", username),
Some(u) => {
2022-06-03 12:00:19 +00:00
tracing::debug!(user=%username, "verify password");
if !verify_password(password, &u.password)? {
2022-05-19 10:10:48 +00:00
bail!("Wrong password");
}
2022-06-03 12:00:19 +00:00
tracing::debug!(user=%username, "fetch bucket");
2022-05-19 10:10:48 +00:00
let bucket = u
.bucket
.clone()
.or_else(|| self.default_bucket.clone())
.ok_or(anyhow!(
"No bucket configured and no default bucket specieid"
))?;
2022-06-03 12:00:19 +00:00
tracing::debug!(user=%username, "fetch configuration");
2022-05-19 12:33:49 +00:00
let storage = StorageCredentials {
k2v_region: self.k2v_region.clone(),
s3_region: self.s3_region.clone(),
2022-05-19 10:10:48 +00:00
aws_access_key_id: u.aws_access_key_id.clone(),
aws_secret_access_key: u.aws_secret_access_key.clone(),
bucket,
2022-05-19 12:33:49 +00:00
};
2022-06-03 12:00:19 +00:00
tracing::debug!(user=%username, "fetch keys");
2022-05-19 12:33:49 +00:00
let keys = match (&u.master_key, &u.secret_key) {
(Some(m), Some(s)) => {
let master_key = Key::from_slice(&base64::decode(m)?)
.ok_or(anyhow!("Invalid master key"))?;
2022-05-19 13:17:58 +00:00
let secret_key = SecretKey::from_slice(&base64::decode(s)?)
2022-05-19 12:33:49 +00:00
.ok_or(anyhow!("Invalid secret key"))?;
2022-05-19 13:14:36 +00:00
CryptoKeys::open_without_password(&storage, &master_key, &secret_key).await?
2022-05-19 12:33:49 +00:00
}
(None, None) => {
let user_secrets = UserSecrets {
user_secret: u.user_secret.clone(),
alternate_user_secrets: u.alternate_user_secrets.clone(),
};
CryptoKeys::open(&storage, &user_secrets, password).await?
2022-05-19 12:33:49 +00:00
}
_ => bail!("Either both master and secret key or none of them must be specified for user"),
};
2022-06-03 12:00:19 +00:00
tracing::debug!(user=%username, "logged");
2022-05-19 13:14:36 +00:00
Ok(Credentials { storage, keys })
2022-05-19 10:10:48 +00:00
}
}
}
}
2022-05-19 13:14:36 +00:00
pub fn hash_password(password: &str) -> Result<String> {
use argon2::{
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
Argon2,
};
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
Ok(argon2
.hash_password(password.as_bytes(), &salt)
.map_err(|e| anyhow!("Argon2 error: {}", e))?
.to_string())
2022-05-19 13:14:36 +00:00
}
pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
use argon2::{
2022-05-20 10:49:53 +00:00
password_hash::{PasswordHash, PasswordVerifier},
Argon2,
};
let parsed_hash =
PasswordHash::new(&hash).map_err(|e| anyhow!("Invalid hashed password: {}", e))?;
Ok(Argon2::default()
.verify_password(password.as_bytes(), &parsed_hash)
.is_ok())
2022-05-19 13:14:36 +00:00
}