framework-cqrs-lib 0.5.4

handle state-machine with data persist in journal and store mongo for restfull actix api
Documentation
use std::sync::Arc;
use actix_web::HttpRequest;

use crate::cqrs::infra::helpers::context::CanDecoreFromHttpRequest;
use crate::cqrs::infra::token::jwt_claims::JwtClaims;
use crate::cqrs::core::context::Context;
use crate::cqrs::core::token::TokenService;
use crate::cqrs::models::errors::{Error, ErrorHttpCustom, ResultErr};

pub async fn authenticated<T: TokenService>(
    req: &HttpRequest,
    jwt_token_service: &Arc<T>,
) -> ResultErr<Context> {
    let maybe_authorization = req.headers().get("Authorization");
    match maybe_authorization {
        Some(bearer_header_value) => {
            let bearer_str = bearer_header_value
                .to_str()
                .map_err(|err|
                    Error::Http(
                        ErrorHttpCustom::new(
                            err.to_string().as_str(),
                            "00TOKPA",
                            vec![],
                            None,
                        )
                    )

                )?;

            let jwt = *bearer_str
                .split(" ")
                .collect::<Vec<&str>>()
                .get(1)
                .unwrap_or(&"");

            let ctx: ResultErr<Context> = jwt_token_service
                .decode::<JwtClaims>(jwt).await
                .map(|claims| claims.into())
                .map_err(|err| {
                    println!("err: {err:?}");
                    err
                });
            ctx.map(|ct| ct.decore_with_http_header(req))
        }
        _ => Err(
            Error::Http(
                ErrorHttpCustom::new(
                    "Unauthorized, pas de token d'authentification",
                    "00MTOKE",
                    vec![],
                    Some(401),
                )
            )
        )
    }
}