actix_jwt_session/
builder.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
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 actix_web::dev::{self, ServiceFactory, ServiceRequest};
use actix_web::web::Data;
use actix_web::{App, Error as ActixWebError};
use deadpool_redis::Pool;

use crate::{Claims, Extractors, SessionMiddlewareFactory};

/**
    This trait gives the ability to call [`Self::use_jwt`] on the implemented type.
*/
pub trait UseJwt {
    /**
        Calls `wrap` on the `scope` will passing the `authority`.
        Then it adds the `scope` as a service on `self`.

        If there is a [`crate::TokenSigner`] set on the `authority`, it is clone it and adds it as app data on `self`.
    */
    fn use_jwt<AppClaims: Claims>(
        self,
        extractors: Extractors<AppClaims>,
        pool: Option<Pool>,
    ) -> App<
        impl ServiceFactory<
            dev::ServiceRequest,
            Error = actix_web::Error,
            Config = (),
            InitError = (),
            Response = dev::ServiceResponse,
        >,
    >;
}

impl<T> UseJwt for App<T>
where
    T: ServiceFactory<
            ServiceRequest,
            Config = (),
            Error = ActixWebError,
            InitError = (),
            Response = dev::ServiceResponse,
        > + 'static,
{
    fn use_jwt<AppClaims: Claims>(
        self,
        extractors: Extractors<AppClaims>,
        pool: Option<Pool>,
    ) -> App<
        impl ServiceFactory<
            dev::ServiceRequest,
            Error = actix_web::Error,
            Config = (),
            InitError = (),
            Response = dev::ServiceResponse,
        >,
    > {
        let mut builder =
            SessionMiddlewareFactory::build_ed_dsa().with_extractors(extractors.clone());
        if let Some(pool) = pool {
            builder = builder.with_redis_pool(pool);
        }
        // create new [SessionStorage] and [SessionMiddlewareFactory]
        let (storage, factory) = builder.finish();
        self.app_data(Data::new(extractors.clone()))
            .app_data(Data::new(storage))
            .wrap(factory)
    }
}