aboutsummaryrefslogtreecommitdiff
path: root/crates/secd/src/command/mod.rs
blob: cd0d8c371849fcb9539ec2b85655a0f9cb89057c (plain)
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
pub mod admin;
pub mod authn;

use crate::client::{
    email,
    sqldb::{PgClient, SqliteClient},
};
use crate::{AuthEmail, AuthStore, Secd, SecdError};
use log::error;
use std::sync::Arc;

impl Secd {
    /// init
    ///
    /// Initialize SecD with the specified configuration, established the necessary
    /// constraints, persistance stores, and options.
    pub async fn init(
        auth_store: AuthStore,
        conn_string: Option<&str>,
        email_messenger: AuthEmail,
        email_template_login: Option<String>,
        email_template_signup: Option<String>,
    ) -> Result<Self, SecdError> {
        let store = match auth_store {
            AuthStore::Sqlite => {
                SqliteClient::new(
                    sqlx::sqlite::SqlitePoolOptions::new()
                        .connect(conn_string.unwrap_or("sqlite::memory:".into()))
                        .await
                        .map_err(|e| SecdError::InitializationFailure(e))?,
                )
                .await
            }
            AuthStore::Postgres => {
                PgClient::new(
                    sqlx::postgres::PgPoolOptions::new()
                        .connect(conn_string.expect("No postgres connection string provided."))
                        .await
                        .map_err(|e| SecdError::InitializationFailure(e))?,
                )
                .await
            }
            rest @ _ => {
                error!(
                    "requested an AuthStore which has not yet been implemented: {:?}",
                    rest
                );
                unimplemented!()
            }
        };

        let email_sender = match email_messenger {
            // TODO: initialize email and SMS templates with secd
            AuthEmail::LocalStub => email::LocalEmailStubber {
                email_template_login,
                email_template_signup,
            },
            _ => unimplemented!(),
        };

        Ok(Secd {
            store,
            email_messenger: Arc::new(email_sender),
        })
    }
}