aboutsummaryrefslogtreecommitdiff
path: root/aero-user/src/login/demo_provider.rs
blob: 11c7d548a85fae418b32ae39b02ac15177481a6d (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
use crate::login::*;
use crate::storage::*;

pub struct DemoLoginProvider {
    keys: CryptoKeys,
    in_memory_store: in_memory::MemDb,
}

impl DemoLoginProvider {
    pub fn new() -> Self {
        Self {
            keys: CryptoKeys::init(),
            in_memory_store: in_memory::MemDb::new(),
        }
    }
}

#[async_trait]
impl LoginProvider for DemoLoginProvider {
    async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
        tracing::debug!(user=%username, "login");

        if username != "alice" {
            bail!("user does not exist");
        }

        if password != "hunter2" {
            bail!("wrong password");
        }

        let storage = self.in_memory_store.builder("alice").await;
        let keys = self.keys.clone();

        Ok(Credentials { storage, keys })
    }

    async fn public_login(&self, email: &str) -> Result<PublicCredentials> {
        tracing::debug!(user=%email, "public_login");
        if email != "alice@example.tld" {
            bail!("invalid email address");
        }

        let storage = self.in_memory_store.builder("alice").await;
        let public_key = self.keys.public.clone();

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