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
use crate::config::AuthConfig;
use crate::repository::AuthRepositoryManager;
use crate::service::auth_account::LsAuthAccountService;
use crate::service::password_codec::LsPasswordCodecService;
use c3p0::IdType;
use lightspeed_core::error::LsError;
use lightspeed_core::web::types::MaybeWeb;
use log::*;
use std::sync::Arc;

pub mod config;
pub mod dto;
pub mod model;
pub mod repository;
pub mod service;

#[derive(Clone)]
pub struct LsAuthModule<Id: IdType + MaybeWeb, RepoManager: AuthRepositoryManager<Id>> {
    pub auth_config: AuthConfig,

    pub repo_manager: RepoManager,

    pub password_codec: Arc<service::password_codec::LsPasswordCodecService>,
    pub auth_account_service: Arc<service::auth_account::LsAuthAccountService<Id, RepoManager>>,
    pub token_service: Arc<service::token::LsTokenService<Id, RepoManager>>,
}

impl<Id: IdType + MaybeWeb, RepoManager: AuthRepositoryManager<Id>> LsAuthModule<Id, RepoManager> {
    pub fn new(repo_manager: RepoManager, auth_config: AuthConfig) -> Self {
        println!("Creating LsAuthModule");
        info!("Creating LsAuthModule");

        let password_codec = Arc::new(LsPasswordCodecService::new(auth_config.bcrypt_password_hash_cost));

        let token_service =
            Arc::new(service::token::LsTokenService::new(auth_config.clone(), repo_manager.token_repo()));

        let auth_account_service = Arc::new(LsAuthAccountService::new(
            repo_manager.c3p0().clone(),
            auth_config.clone(),
            token_service.clone(),
            password_codec.clone(),
            repo_manager.auth_account_repo(),
        ));

        LsAuthModule { auth_config, repo_manager, password_codec, auth_account_service, token_service }
    }
}

impl<Id: IdType + MaybeWeb, RepoManager: AuthRepositoryManager<Id>> lightspeed_core::module::LsModule
    for LsAuthModule<Id, RepoManager>
{
    async fn start(&mut self) -> Result<(), LsError> {
        info!("Starting LsAuthModule");
        self.repo_manager.start().await?;
        Ok(())
    }
}