aboutsummaryrefslogtreecommitdiff
path: root/src/login/static_provider.rs
blob: d013c6f349185e535a61601387485cc87891e77c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::collections::HashMap;
use std::sync::Arc;

use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;

use crate::config::*;
use crate::cryptoblob::{Key, SecretKey};
use crate::login::*;
use crate::storage;

pub struct StaticLoginProvider {
    users: HashMap<String, Arc<LoginStaticUser>>,
    users_by_email: HashMap<String, Arc<LoginStaticUser>>,
}

impl StaticLoginProvider {
    pub fn new(config: LoginStaticConfig) -> Result<Self> {
        let users = config
            .into_iter()
            .map(|(k, v)| (k, Arc::new(v)))
            .collect::<HashMap<_, _>>();
        let mut users_by_email = HashMap::new();
        for (_, u) in users.iter() {
            for m in u.email_addresses.iter() {
                if users_by_email.contains_key(m) {
                    bail!("Several users have same email address: {}", m);
                }
                users_by_email.insert(m.clone(), u.clone());
            }
        }

        Ok(Self {
            users,
            users_by_email,
        })
    }
}

#[async_trait]
impl LoginProvider for StaticLoginProvider {
    async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
        tracing::debug!(user=%username, "login");
        let user = match self.users.get(username) {
            None => bail!("User {} does not exist", username),
            Some(u) => u,
        };

        tracing::debug!(user=%username, "verify password");
        if !verify_password(password, &user.password)? {
            bail!("Wrong password");
        }

        /*
        tracing::debug!(user=%username, "fetch bucket");
        let bucket = user
            .bucket
            .clone()
            .or_else(|| self.default_bucket.clone())
            .ok_or(anyhow!(
                "No bucket configured and no default bucket specified"
            ))?;*/

        tracing::debug!(user=%username, "fetch keys");
        let storage: storage::Builders = match user.storage {
            StaticStorage::InMemory => Box::new(storage::in_memory::FullMem {}),
            StaticStorage::Garage(c) => Box::new(storage::garage::GrgCreds {}),
        };

        /*
        StorageCredentials {
            k2v_region: self.k2v_region.clone(),
            s3_region: self.s3_region.clone(),
            aws_access_key_id: user.aws_access_key_id.clone(),
            aws_secret_access_key: user.aws_secret_access_key.clone(),
            bucket,
        };*/

        let keys = match (&user.master_key, &user.secret_key) {
            (Some(m), Some(s)) => {
                let master_key =
                    Key::from_slice(&base64::decode(m)?).ok_or(anyhow!("Invalid master key"))?;
                let secret_key = SecretKey::from_slice(&base64::decode(s)?)
                    .ok_or(anyhow!("Invalid secret key"))?;
                CryptoKeys::open_without_password(&storage, &master_key, &secret_key).await?
            }
            (None, None) => {
                let user_secrets = UserSecrets {
                    user_secret: user.user_secret.clone(),
                    alternate_user_secrets: user.alternate_user_secrets.clone(),
                };
                CryptoKeys::open(&storage, &user_secrets, password).await?
            }
            _ => bail!(
                "Either both master and secret key or none of them must be specified for user"
            ),
        };

        tracing::debug!(user=%username, "logged");
        Ok(Credentials { storage, keys })
    }

    async fn public_login(&self, email: &str) -> Result<PublicCredentials> {
        let user = match self.users_by_email.get(email) {
            None => bail!("No user for email address {}", email),
            Some(u) => u,
        };

        /*
        let bucket = user
            .bucket
            .clone()
            .or_else(|| self.default_bucket.clone())
            .ok_or(anyhow!(
                "No bucket configured and no default bucket specieid"
            ))?;

        let storage = StorageCredentials {
            k2v_region: self.k2v_region.clone(),
            s3_region: self.s3_region.clone(),
            aws_access_key_id: user.aws_access_key_id.clone(),
            aws_secret_access_key: user.aws_secret_access_key.clone(),
            bucket,
        };*/
        let storage: storage::Builders = match user.storage {
            StaticStorage::InMemory => X,
            StaticStorage::Garage => Y,
        };

        let k2v_client = storage.k2v_client()?;
        let (_, public_key) = CryptoKeys::load_salt_and_public(&k2v_client).await?;

        Ok(PublicCredentials {
            storage,
            public_key,
        })
    }
}

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())
}

pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
    use argon2::{
        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())
}