Skip to main content

framework_cqrs_lib/cqrs/infra/authentication/
mod.rs

1use std::sync::Arc;
2use crate::cqrs::infra::token::services::jwt_hmac::JwtHMACTokenService;
3use crate::cqrs::infra::token::services::jwt_hmac_encoder::JwtHMACEncoderTokenService;
4use crate::cqrs::models::errors::{Error, ResultErr};
5
6pub struct AuthenticationComponent {
7    pub jwt_token_decoder_service: Arc<JwtHMACTokenService>,
8    pub jwt_token_encoder_service: Arc<JwtHMACEncoderTokenService>,
9}
10
11impl AuthenticationComponent {
12    pub fn new() -> ResultErr<Self> {
13        let jwt_secret = std::env::var("JWT_SECRET")
14            .map_err(|_|
15                Error::Simple("env variable JWT_SECRET is not defined".to_string())
16            )?;
17        Ok(
18            Self {
19                jwt_token_decoder_service: Arc::new(JwtHMACTokenService::new(jwt_secret.clone())),
20                jwt_token_encoder_service: Arc::new(JwtHMACEncoderTokenService::new(jwt_secret)),
21            }
22        )
23    }
24}