1
- use axum:: extract:: Request ;
2
1
use futures_core:: ready;
3
2
use futures_util:: future:: { self , BoxFuture } ;
3
+ use http:: Request ;
4
4
use jsonwebtoken:: TokenData ;
5
5
use pin_project:: pin_project;
6
6
use serde:: de:: DeserializeOwned ;
@@ -15,25 +15,26 @@ use crate::authorizer::Authorizer;
15
15
use crate :: AuthError ;
16
16
17
17
/// Trait for authorizing requests.
18
- pub trait Authorize {
19
- type Future : Future < Output = Result < Request , AuthError > > ;
18
+ pub trait Authorize < B > {
19
+ type Future : Future < Output = Result < Request < B > , AuthError > > ;
20
20
21
21
/// Authorize the request.
22
22
///
23
23
/// If the future resolves to `Ok(request)` then the request is allowed through, otherwise not.
24
- fn authorize ( & self , request : Request ) -> Self :: Future ;
24
+ fn authorize ( & self , request : Request < B > ) -> Self :: Future ;
25
25
}
26
26
27
- impl < S , C > Authorize for AuthorizationService < S , C >
27
+ impl < S , B , C > Authorize < B > for AuthorizationService < S , C >
28
28
where
29
+ B : Send + ' static ,
29
30
C : Clone + DeserializeOwned + Send + Sync + ' static ,
30
31
{
31
- type Future = BoxFuture < ' static , Result < Request , AuthError > > ;
32
+ type Future = BoxFuture < ' static , Result < Request < B > , AuthError > > ;
32
33
33
34
/// The authorizers are sequentially applied (check_auth) until one of them validates the token.
34
35
/// If no authorizer validates the token the request is rejected.
35
36
///
36
- fn authorize ( & self , mut request : Request ) -> Self :: Future {
37
+ fn authorize ( & self , mut request : Request < B > ) -> Self :: Future {
37
38
let tkns_auths: Vec < ( String , Arc < Authorizer < C > > ) > = self
38
39
. auths
39
40
. iter ( )
@@ -154,21 +155,22 @@ where
154
155
}
155
156
}
156
157
157
- impl < S , C > Service < Request > for AuthorizationService < S , C >
158
+ impl < S , C , B > Service < Request < B > > for AuthorizationService < S , C >
158
159
where
159
- S : Service < Request > + Clone ,
160
+ B : Send + ' static ,
161
+ S : Service < Request < B > > + Clone ,
160
162
S :: Response : From < AuthError > ,
161
163
C : Clone + DeserializeOwned + Send + Sync + ' static ,
162
164
{
163
165
type Response = S :: Response ;
164
166
type Error = S :: Error ;
165
- type Future = ResponseFuture < S , C > ;
167
+ type Future = ResponseFuture < S , C , B > ;
166
168
167
169
fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
168
170
self . inner . poll_ready ( cx)
169
171
}
170
172
171
- fn call ( & mut self , req : Request ) -> Self :: Future {
173
+ fn call ( & mut self , req : Request < B > ) -> Self :: Future {
172
174
let inner = self . inner . clone ( ) ;
173
175
// take the service that was ready
174
176
let inner = std:: mem:: replace ( & mut self . inner , inner) ;
@@ -184,13 +186,14 @@ where
184
186
185
187
#[ pin_project]
186
188
/// Response future for [`AuthorizationService`].
187
- pub struct ResponseFuture < S , C >
189
+ pub struct ResponseFuture < S , C , B >
188
190
where
189
- S : Service < Request > ,
191
+ B : Send + ' static ,
192
+ S : Service < Request < B > > ,
190
193
C : Clone + DeserializeOwned + Send + Sync + ' static ,
191
194
{
192
195
#[ pin]
193
- state : State < <AuthorizationService < S , C > as Authorize >:: Future , S :: Future > ,
196
+ state : State < <AuthorizationService < S , C > as Authorize < B > >:: Future , S :: Future > ,
194
197
service : S ,
195
198
}
196
199
@@ -206,9 +209,10 @@ enum State<A, SFut> {
206
209
} ,
207
210
}
208
211
209
- impl < S , C > Future for ResponseFuture < S , C >
212
+ impl < S , C , B > Future for ResponseFuture < S , C , B >
210
213
where
211
- S : Service < Request > ,
214
+ B : Send ,
215
+ S : Service < Request < B > > ,
212
216
S :: Response : From < AuthError > ,
213
217
C : Clone + DeserializeOwned + Send + Sync ,
214
218
{
0 commit comments