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
use crate::config::AuthConfig;
use crate::repository::AuthRepositoryManager;
use crate::service::auth_account::AuthAccountService;
use crate::service::password_codec::PasswordCodecService;
use lightspeed_core::error::LightSpeedError;
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 AuthModule<RepoManager: AuthRepositoryManager> {
    pub auth_config: AuthConfig,

    pub repo_manager: RepoManager,

    pub password_codec: Arc<service::password_codec::PasswordCodecService>,
    pub auth_account_service: Arc<service::auth_account::AuthAccountService<RepoManager>>,
    pub token_service: Arc<service::token::TokenService<RepoManager>>,
}

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

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

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

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

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

#[async_trait::async_trait]
impl<RepoManager: AuthRepositoryManager> lightspeed_core::module::Module
    for AuthModule<RepoManager>
{
    async fn start(&mut self) -> Result<(), LightSpeedError> {
        info!("Starting AuthModule");
        self.repo_manager.start().await?;
        Ok(())
    }
}