pub mod authn; use super::{AuthEmailMessenger, AuthStore, Secd, SecdError}; use crate::{ client::{ email, store::sql_db::{PgClient, SqliteClient}, }, ENV_AUTH_STORE_CONN_STRING, ENV_EMAIL_MESSENGER, ENV_EMAIL_MESSENGER_CLIENT_ID, ENV_EMAIL_MESSENGER_CLIENT_SECRET, }; use log::{error, info}; use std::{env::var, str::FromStr, sync::Arc}; impl Secd { /// init /// /// Initialize SecD with the specified configuration, established the necessary /// constraints, persistance stores, and options. pub async fn init() -> Result { let auth_store = AuthStore::from(var(ENV_AUTH_STORE_CONN_STRING).ok()); let email_messenger = AuthEmailMessenger::from_str( &var(ENV_EMAIL_MESSENGER).unwrap_or(AuthEmailMessenger::Local.to_string()), ) .expect("unreachable f4ad0f48-0812-427f-b477-0f9c67bb69c5"); let email_messenger_client_id = var(ENV_EMAIL_MESSENGER_CLIENT_ID).ok(); let email_messenger_client_secret = var(ENV_EMAIL_MESSENGER_CLIENT_SECRET).ok(); info!("starting client with auth_store: {:?}", auth_store); info!("starting client with email_messenger: {:?}", auth_store); let store = match auth_store { AuthStore::Sqlite { conn } => { SqliteClient::new( sqlx::sqlite::SqlitePoolOptions::new() .connect(&conn) .await .map_err(|e| { SecdError::StoreInitFailure(format!("failed to init sqlite: {}", e)) })?, ) .await } AuthStore::Postgres { conn } => { PgClient::new( sqlx::postgres::PgPoolOptions::new() .connect(&conn) .await .map_err(|e| { SecdError::StoreInitFailure(format!("failed to init sqlite: {}", e)) })?, ) .await } rest @ _ => { error!( "requested an AuthStore which has not yet been implemented: {:?}", rest ); unimplemented!() } }; let email_sender = match email_messenger { AuthEmailMessenger::Local => email::LocalMailer {}, _ => unimplemented!(), }; Ok(Secd { store, email_messenger: Arc::new(email_sender), }) } }