framework_cqrs_lib/cqrs/infra/authentication/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::sync::Arc;
use crate::cqrs::infra::token::services::jwt_hmac::JwtHMACTokenService;
use crate::cqrs::infra::token::services::jwt_hmac_encoder::JwtHMACEncoderTokenService;
use crate::cqrs::models::errors::{Error, ResultErr};

pub struct AuthenticationComponent {
    pub jwt_token_decoder_service: Arc<JwtHMACTokenService>,
    pub jwt_token_encoder_service: Arc<JwtHMACEncoderTokenService>,
}

impl AuthenticationComponent {
    pub fn new() -> ResultErr<Self> {
        let jwt_secret = std::env::var("JWT_SECRET")
            .map_err(|_|
                Error::Simple("env variable JWT_SECRET is not defined".to_string())
            )?;
        Ok(
            Self {
                jwt_token_decoder_service: Arc::new(JwtHMACTokenService::new(jwt_secret.clone())),
                jwt_token_encoder_service: Arc::new(JwtHMACEncoderTokenService::new(jwt_secret)),
            }
        )
    }
}