actix_jwt_auth_middleware/use_jwt/
resource.rs

1use crate::AuthenticationService;
2use crate::Authority;
3
4use actix_web::dev::ServiceFactory;
5use actix_web::dev::ServiceRequest;
6use actix_web::dev::ServiceResponse;
7use actix_web::Error as ActixWebError;
8use actix_web::FromRequest;
9use actix_web::Handler;
10use actix_web::Resource;
11use jwt_compact::Algorithm as JWTAlgorithm;
12use serde::de::DeserializeOwned;
13use serde::Serialize;
14
15/**
16    This trait gives the ability to call [`Self::use_jwt`] on the implemented type.
17
18    It is currently behind a features flag, `use_jwt_on_resource`, because it uses some experimental rust features.
19*/
20pub trait UseJWTOnResource<Claims, Algorithm, ReAuth, Args>
21where
22    Claims: Serialize + DeserializeOwned + 'static,
23    Algorithm: JWTAlgorithm + Clone,
24    Algorithm::SigningKey: Clone,
25    ReAuth: Handler<Args, Output = Result<(), ActixWebError>>,
26    Args: FromRequest,
27{
28    /**
29        Calls `wrap` on the `scope` will passing the `authority`.
30        Then it adds the `scope` as a service on `self`.
31
32        If there is a [`crate::TokenSigner`] set on the `authority`, it is clone it and adds it as app data on `self`.
33    */
34    fn use_jwt(
35        self,
36        authority: Authority<Claims, Algorithm, ReAuth, Args>,
37    ) -> Resource<
38        impl ServiceFactory<
39            ServiceRequest,
40            Config = (),
41            Response = ServiceResponse,
42            Error = ActixWebError,
43            InitError = (),
44        >,
45    >;
46}
47
48impl<Claims, Algorithm, ReAuth, Args> UseJWTOnResource<Claims, Algorithm, ReAuth, Args> for Resource
49where
50    Claims: Serialize + DeserializeOwned + 'static,
51    Algorithm: JWTAlgorithm + Clone + 'static,
52    Algorithm::SigningKey: Clone,
53    ReAuth: Handler<Args, Output = Result<(), ActixWebError>>,
54    Args: FromRequest + 'static,
55{
56    fn use_jwt(
57        self,
58        authority: Authority<Claims, Algorithm, ReAuth, Args>,
59    ) -> Resource<
60        impl ServiceFactory<
61            ServiceRequest,
62            Config = (),
63            Response = ServiceResponse,
64            Error = ActixWebError,
65            InitError = (),
66        >,
67    > {
68        self.wrap(AuthenticationService::new(authority))
69    }
70}