actix_jwt_session/
builder.rs

1use actix_web::dev::{self, ServiceFactory, ServiceRequest};
2use actix_web::web::Data;
3use actix_web::{App, Error as ActixWebError};
4use deadpool_redis::Pool;
5
6use crate::{Claims, Extractors, SessionMiddlewareFactory};
7
8/**
9    This trait gives the ability to call [`Self::use_jwt`] on the implemented type.
10*/
11pub trait UseJwt {
12    /**
13        Calls `wrap` on the `scope` will passing the `authority`.
14        Then it adds the `scope` as a service on `self`.
15
16        If there is a [`crate::TokenSigner`] set on the `authority`, it is clone it and adds it as app data on `self`.
17    */
18    fn use_jwt<AppClaims: Claims>(
19        self,
20        extractors: Extractors<AppClaims>,
21        pool: Option<Pool>,
22    ) -> App<
23        impl ServiceFactory<
24            dev::ServiceRequest,
25            Error = actix_web::Error,
26            Config = (),
27            InitError = (),
28            Response = dev::ServiceResponse,
29        >,
30    >;
31}
32
33impl<T> UseJwt for App<T>
34where
35    T: ServiceFactory<
36            ServiceRequest,
37            Config = (),
38            Error = ActixWebError,
39            InitError = (),
40            Response = dev::ServiceResponse,
41        > + 'static,
42{
43    fn use_jwt<AppClaims: Claims>(
44        self,
45        extractors: Extractors<AppClaims>,
46        pool: Option<Pool>,
47    ) -> App<
48        impl ServiceFactory<
49            dev::ServiceRequest,
50            Error = actix_web::Error,
51            Config = (),
52            InitError = (),
53            Response = dev::ServiceResponse,
54        >,
55    > {
56        let mut builder =
57            SessionMiddlewareFactory::build_ed_dsa().with_extractors(extractors.clone());
58        if let Some(pool) = pool {
59            builder = builder.with_redis_pool(pool);
60        }
61        // create new [SessionStorage] and [SessionMiddlewareFactory]
62        let (storage, factory) = builder.finish();
63        self.app_data(Data::new(extractors.clone()))
64            .app_data(Data::new(storage))
65            .wrap(factory)
66    }
67}