🗝
summary refs log tree commit diff
path: root/src/server/store.rs
blob: 98c1bccd343d1d06f1e6fa882251a99acd3d9311 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::{
    collections::HashMap,
    path::PathBuf,
    sync::Arc,
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use argon2::{
    password_hash::{rand_core::OsRng, Encoding, PasswordHashString, SaltString},
    Argon2, PasswordHash, PasswordHasher,
};
use nanoid::nanoid;
use serde::{de::Unexpected, Deserialize, Serialize};
use serde_json::{Map, Value};
use tap::{Pipe, Tap};
use tokio::sync::{RwLock, RwLockReadGuard};

#[derive(Clone)]
pub struct Store(Arc<RwLock<StoreInner>>);

impl Store {
    pub async fn load(path: PathBuf) -> eyre::Result<Self> {
        let mut inner = StoreInner::new(path.clone());
        if path.try_exists()? {
            let mut map: Map<String, Value> =
                { std::fs::File::open(&path)?.pipe(serde_json::from_reader)? };
            let accounts: Vec<Account> =
                serde_json::from_value(map.remove("accounts").unwrap_or_default())?;
            let apps: Vec<App> = serde_json::from_value(map.remove("apps").unwrap_or_default())?;
            for mut account in accounts {
                account.tokens = account
                    .tokens
                    .into_iter()
                    .filter(|token| token.expires > SystemTime::now())
                    .collect();
                for token in &account.tokens {
                    inner.token_map.insert(
                        token.value.clone(),
                        (account.name.clone(), token.expires.clone()),
                    );
                }
                inner.accounts.insert(account.name.clone(), account);
            }
            for app in apps {
                inner.apps.insert(app.name.clone(), app);
            }
            inner.invites = serde_json::from_value(map.remove("invites").unwrap_or_default())?;
            inner.invites = inner
                .invites
                .into_iter()
                .filter(|invite| invite.expires > SystemTime::now())
                .collect();
        } else {
            Self::save(&mut inner).await?;
        }
        Ok(Store(Arc::new(RwLock::new(inner))))
    }

    async fn save(inner: &mut StoreInner) -> std::io::Result<()> {
        let mut map: Map<String, Value> = Map::new();
        map.insert(
            "accounts".into(),
            serde_json::to_value(inner.accounts.values().collect::<Vec<_>>()).unwrap(),
        );
        map.insert(
            "apps".into(),
            serde_json::to_value(inner.apps.values().collect::<Vec<_>>()).unwrap(),
        );
        map.insert(
            "invites".into(),
            serde_json::to_value(&inner.invites).unwrap(),
        );
        let data = serde_json::to_vec_pretty(&map).unwrap();
        let mut temp = inner.path.clone();
        temp.set_file_name(
            temp.file_name()
                .unwrap_or_default()
                .to_os_string()
                .tap_mut(|name| name.push(".tmp")),
        );
        tokio::fs::write(&temp, data).await?;
        tokio::fs::rename(temp, &inner.path).await?;
        Ok(())
    }

    pub async fn get_account(&self, name: &str) -> Option<RwLockReadGuard<'_, Account>> {
        let guard = self.0.read().await;
        RwLockReadGuard::try_map(guard, |guard| guard.accounts.get(name)).ok()
    }

    pub async fn create_account(&self, name: &str, password: &str) -> std::io::Result<bool> {
        let hash = Argon2::default()
            .hash_password(password.as_bytes(), &SaltString::generate(&mut OsRng))
            .unwrap()
            .pipe(|hash| hash.serialize())
            .pipe(OwnedPasswordHash::from);
        let mut guard = self.0.write().await;
        if guard.accounts.get(name).is_some() {
            return Ok(false);
        }
        guard.accounts.insert(
            name.to_string(),
            Account {
                name: name.to_string(),
                password: hash,
                tokens: Default::default(),
                scopes: Default::default(),
            },
        );
        Self::save(&mut guard).await?;
        Ok(true)
    }

    pub async fn delete_account(&self, name: &str) -> std::io::Result<()> {
        let mut guard = self.0.write().await;
        if let Some(account) = guard.accounts.remove(name) {
            for token in account.tokens {
                guard.token_map.remove(&token.value);
            }
            Self::save(&mut guard).await
        } else {
            Ok(())
        }
    }

    pub async fn update_account(
        &self,
        name: &str,
        with: impl FnOnce(&mut Account),
    ) -> std::io::Result<bool> {
        let mut guard = self.0.write().await;
        if let Some(account) = guard.accounts.get_mut(name) {
            with(account);
            Self::save(&mut guard).await.map(|_| true)
        } else {
            Ok(false)
        }
    }

    pub async fn list_accounts(&self) -> Vec<String> {
        self.0
            .read()
            .await
            .accounts
            .values()
            .map(|account| account.name.clone())
            .collect()
    }

    pub async fn create_token(&self, name: &str) -> std::io::Result<Option<String>> {
        let mut guard = self.0.write().await;
        let token = nanoid!(32);
        if let Some(account) = guard.accounts.get_mut(name) {
            let expires = SystemTime::now() + Duration::from_secs(60 * 60 * 24 * 30);
            account.tokens.push(ExpiringValue {
                value: token.clone(),
                expires: expires.clone(),
            });
            guard
                .token_map
                .insert(token.clone(), (name.to_string(), expires));
            Self::save(&mut guard).await?;
            Ok(Some(token))
        } else {
            Ok(None)
        }
    }

    pub async fn check_token(&self, token: &str) -> Option<String> {
        let guard = self.0.read().await;
        let Some((name, expires)) = guard.token_map.get(token) else {
            return None;
        };
        if *expires < SystemTime::now() {
            return None;
        }
        Some(name.clone())
    }

    pub async fn create_invite(&self) -> std::io::Result<String> {
        let mut guard = self.0.write().await;
        let invite = nanoid!(32);
        let expires = SystemTime::now() + Duration::from_secs(60 * 60 * 24 * 14);
        guard.invites.push(ExpiringValue {
            value: invite.clone(),
            expires,
        });
        Self::save(&mut guard).await?;
        Ok(invite)
    }

    pub async fn check_invite(&self, invite: &str) -> bool {
        let guard = self.0.read().await;
        let now = SystemTime::now();
        guard
            .invites
            .iter()
            .any(|check| check.expires > now && check.value == invite)
    }

    pub async fn use_invite(&self, invite: &str) -> std::io::Result<bool> {
        let mut guard = self.0.write().await;
        let now = SystemTime::now();
        if let Some((index, _)) = guard
            .invites
            .iter()
            .enumerate()
            .find(|(_, check)| check.expires > now && check.value == invite)
        {
            guard.invites.swap_remove(index);
            Self::save(&mut guard).await?;
            Ok(true)
        } else {
            Ok(false)
        }
    }
}

pub struct StoreInner {
    path: PathBuf,
    accounts: HashMap<String, Account>,
    apps: HashMap<String, App>,
    invites: Vec<ExpiringValue>,
    token_map: HashMap<String, (String, SystemTime)>,
}

impl StoreInner {
    fn new(at: PathBuf) -> Self {
        Self {
            path: at,
            accounts: Default::default(),
            apps: Default::default(),
            invites: Default::default(),
            token_map: Default::default(),
        }
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Account {
    pub name: String,
    pub password: OwnedPasswordHash,
    pub tokens: Vec<ExpiringValue>,
    pub scopes: Vec<String>,
}

#[ouroboros::self_referencing]
pub struct OwnedPasswordHash {
    owned: PasswordHashString,
    #[borrows(owned)]
    #[not_covariant]
    parsed: PasswordHash<'this>,
}

impl OwnedPasswordHash {
    pub fn from(inner: PasswordHashString) -> Self {
        OwnedPasswordHash::new(inner, |inner| inner.password_hash())
    }

    pub fn parsed(&self) -> PasswordHash {
        self.with_parsed(|x| x.clone())
    }
}

impl Serialize for OwnedPasswordHash {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.with_owned(|owned| owned.as_str().serialize(serializer))
    }
}

impl<'de> Deserialize<'de> for OwnedPasswordHash {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Ok(String::deserialize(deserializer)?
            .pipe(|hash| {
                PasswordHashString::parse(&hash, Encoding::B64).map_err(|_| {
                    <D::Error as serde::de::Error>::invalid_value(
                        Unexpected::Str(&hash),
                        &"valid password hash",
                    )
                })
            })?
            .pipe(OwnedPasswordHash::from))
    }
}

pub struct ExpiringValue {
    pub value: String,
    pub expires: SystemTime,
}

impl Serialize for ExpiringValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        <(&str, u64)>::serialize(
            &(
                &self.value,
                (self.expires.duration_since(SystemTime::UNIX_EPOCH))
                    .unwrap()
                    .as_secs(),
            ),
            serializer,
        )
    }
}

impl<'de> Deserialize<'de> for ExpiringValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        <(String, u64)>::deserialize(deserializer).map(|(token, unix)| ExpiringValue {
            value: token,
            expires: UNIX_EPOCH + Duration::from_secs(unix),
        })
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct App {
    name: String,
    secret: String,
}