1
1
#![ deny( clippy:: all) ]
2
2
#![ deny( rust_2018_idioms) ]
3
3
4
- use crate :: chain:: chain;
5
4
use crate :: db:: DbPool ;
6
5
use crate :: event_aggregator:: EventAggregator ;
7
- use crate :: middleware:: auth;
8
- use crate :: middleware:: channel:: { channel_load, get_channel_id} ;
9
- use crate :: middleware:: cors:: { cors, Cors } ;
10
6
use crate :: routes:: channel:: channel_status;
11
7
use crate :: routes:: event_aggregate:: list_channel_event_aggregates;
12
8
use crate :: routes:: validator_message:: { extract_params, list_validator_messages} ;
13
9
use chrono:: Utc ;
14
- use futures:: future:: { BoxFuture , FutureExt } ;
15
10
use hyper:: { Body , Method , Request , Response , StatusCode } ;
16
11
use lazy_static:: lazy_static;
12
+ use middleware:: {
13
+ auth:: { AuthRequired , Authenticate } ,
14
+ channel:: { ChannelLoad , GetChannelId } ,
15
+ cors:: { cors, Cors } ,
16
+ } ;
17
+ use middleware:: { Chain , Middleware } ;
17
18
use primitives:: adapter:: Adapter ;
18
19
use primitives:: sentry:: ValidationErrorResponse ;
19
20
use primitives:: { Config , ValidatorId } ;
@@ -25,15 +26,10 @@ use routes::channel::{
25
26
channel_list, channel_validate, create_channel, create_validator_messages, insert_events,
26
27
last_approved,
27
28
} ;
28
- use slog:: { error , Logger } ;
29
+ use slog:: Logger ;
29
30
use std:: collections:: HashMap ;
30
31
31
- pub mod middleware {
32
- pub mod auth;
33
- pub mod channel;
34
- pub mod cors;
35
- }
36
-
32
+ pub mod middleware;
37
33
pub mod routes {
38
34
pub mod analytics;
39
35
pub mod cfg;
@@ -44,7 +40,6 @@ pub mod routes {
44
40
45
41
pub mod access;
46
42
pub mod analytics_recorder;
47
- mod chain;
48
43
pub mod db;
49
44
pub mod event_aggregator;
50
45
pub mod event_reducer;
@@ -64,20 +59,6 @@ lazy_static! {
64
59
static ref CREATE_EVENTS_BY_CHANNEL_ID : Regex = Regex :: new( r"^/channel/0x([a-zA-Z0-9]{64})/events/?$" ) . expect( "The regex should be valid" ) ;
65
60
}
66
61
67
- fn auth_required_middleware < ' a , A : Adapter > (
68
- req : Request < Body > ,
69
- _: & Application < A > ,
70
- ) -> BoxFuture < ' a , Result < Request < Body > , ResponseError > > {
71
- async move {
72
- if req. extensions ( ) . get :: < Session > ( ) . is_some ( ) {
73
- Ok ( req)
74
- } else {
75
- Err ( ResponseError :: Unauthorized )
76
- }
77
- }
78
- . boxed ( )
79
- }
80
-
81
62
#[ derive( Debug ) ]
82
63
pub struct RouteParams ( Vec < String > ) ;
83
64
@@ -127,12 +108,9 @@ impl<A: Adapter + 'static> Application<A> {
127
108
None => Default :: default ( ) ,
128
109
} ;
129
110
130
- let req = match auth :: for_request ( req, & self . adapter , self . redis . clone ( ) ) . await {
111
+ let req = match Authenticate . call ( req, & self ) . await {
131
112
Ok ( req) => req,
132
- Err ( error) => {
133
- error ! ( & self . logger, "{}" , & error; "module" => "middleware-auth" ) ;
134
- return map_response_error ( ResponseError :: Unauthorized ) ;
135
- }
113
+ Err ( error) => return map_response_error ( error) ,
136
114
} ;
137
115
138
116
let mut response = match ( req. uri ( ) . path ( ) , req. method ( ) ) {
@@ -143,7 +121,7 @@ impl<A: Adapter + 'static> Application<A> {
143
121
144
122
( "/analytics" , & Method :: GET ) => analytics ( req, & self ) . await ,
145
123
( "/analytics/advanced" , & Method :: GET ) => {
146
- let req = match chain ( req, & self , vec ! [ Box :: new ( auth_required_middleware ) ] ) . await {
124
+ let req = match AuthRequired . call ( req, & self ) . await {
147
125
Ok ( req) => req,
148
126
Err ( error) => {
149
127
return map_response_error ( error) ;
@@ -153,7 +131,7 @@ impl<A: Adapter + 'static> Application<A> {
153
131
advanced_analytics ( req, & self ) . await
154
132
}
155
133
( "/analytics/for-advertiser" , & Method :: GET ) => {
156
- let req = match chain ( req, & self , vec ! [ Box :: new ( auth_required_middleware ) ] ) . await {
134
+ let req = match AuthRequired . call ( req, & self ) . await {
157
135
Ok ( req) => req,
158
136
Err ( error) => {
159
137
return map_response_error ( error) ;
@@ -162,7 +140,7 @@ impl<A: Adapter + 'static> Application<A> {
162
140
advertiser_analytics ( req, & self ) . await
163
141
}
164
142
( "/analytics/for-publisher" , & Method :: GET ) => {
165
- let req = match chain ( req, & self , vec ! [ Box :: new ( auth_required_middleware ) ] ) . await {
143
+ let req = match AuthRequired . call ( req, & self ) . await {
166
144
Ok ( req) => req,
167
145
Err ( error) => {
168
146
return map_response_error ( error) ;
@@ -199,12 +177,12 @@ async fn analytics_router<A: Adapter + 'static>(
199
177
. map_or( "" . to_string( ) , |m| m. as_str( ) . to_string( ) ) ] ) ;
200
178
req. extensions_mut ( ) . insert ( param) ;
201
179
202
- let req = chain (
203
- req ,
204
- app ,
205
- vec ! [ Box :: new ( channel_load ) , Box :: new ( get_channel_id ) ] ,
206
- )
207
- . await ?;
180
+ // apply middlewares
181
+ req = Chain :: new ( )
182
+ . chain ( ChannelLoad )
183
+ . chain ( GetChannelId )
184
+ . apply ( req , app )
185
+ . await ?;
208
186
209
187
analytics ( req, app) . await
210
188
} else if let Some ( caps) = ADVERTISER_ANALYTICS_BY_CHANNEL_ID . captures ( route) {
@@ -213,12 +191,12 @@ async fn analytics_router<A: Adapter + 'static>(
213
191
. map_or( "" . to_string( ) , |m| m. as_str( ) . to_string( ) ) ] ) ;
214
192
req. extensions_mut ( ) . insert ( param) ;
215
193
216
- let req = chain (
217
- req ,
218
- app ,
219
- vec ! [ Box :: new ( auth_required_middleware ) , Box :: new ( get_channel_id ) ] ,
220
- )
221
- . await ?;
194
+ // apply middlewares
195
+ req = Chain :: new ( )
196
+ . chain ( AuthRequired )
197
+ . chain ( GetChannelId )
198
+ . apply ( req , app )
199
+ . await ?;
222
200
223
201
advertiser_analytics ( req, app) . await
224
202
} else if let Some ( caps) = PUBLISHER_ANALYTICS_BY_CHANNEL_ID . captures ( route) {
@@ -227,12 +205,12 @@ async fn analytics_router<A: Adapter + 'static>(
227
205
. map_or( "" . to_string( ) , |m| m. as_str( ) . to_string( ) ) ] ) ;
228
206
req. extensions_mut ( ) . insert ( param) ;
229
207
230
- let req = chain (
231
- req ,
232
- app ,
233
- vec ! [ Box :: new ( auth_required_middleware ) , Box :: new ( get_channel_id ) ] ,
234
- )
235
- . await ?;
208
+ // apply middlewares
209
+ req = Chain :: new ( )
210
+ . chain ( AuthRequired )
211
+ . chain ( GetChannelId )
212
+ . apply ( req , app )
213
+ . await ?;
236
214
237
215
publisher_analytics ( req, app) . await
238
216
} else {
@@ -274,7 +252,7 @@ async fn channels_router<A: Adapter + 'static>(
274
252
. map_or( "" . to_string( ) , |m| m. as_str( ) . to_string( ) ) ] ) ;
275
253
req. extensions_mut ( ) . insert ( param) ;
276
254
277
- let req = channel_load ( req, app) . await ?;
255
+ req = ChannelLoad . call ( req, app) . await ?;
278
256
channel_status ( req, app) . await
279
257
} else if let ( Some ( caps) , & Method :: GET ) = ( CHANNEL_VALIDATOR_MESSAGES . captures ( & path) , method)
280
258
{
@@ -284,12 +262,7 @@ async fn channels_router<A: Adapter + 'static>(
284
262
285
263
req. extensions_mut ( ) . insert ( param) ;
286
264
287
- let req = match chain ( req, app, vec ! [ Box :: new( channel_load) ] ) . await {
288
- Ok ( req) => req,
289
- Err ( error) => {
290
- return Err ( error) ;
291
- }
292
- } ;
265
+ req = ChannelLoad . call ( req, app) . await ?;
293
266
294
267
// @TODO: Move this to a middleware?!
295
268
let extract_params = match extract_params ( caps. get ( 2 ) . map_or ( "" , |m| m. as_str ( ) ) ) {
@@ -308,24 +281,15 @@ async fn channels_router<A: Adapter + 'static>(
308
281
309
282
req. extensions_mut ( ) . insert ( param) ;
310
283
311
- let req = match chain (
312
- req,
313
- app,
314
- vec ! [ Box :: new( auth_required_middleware) , Box :: new( channel_load) ] ,
315
- )
316
- . await
317
- {
318
- Ok ( req) => req,
319
- Err ( error) => {
320
- return Err ( error) ;
321
- }
322
- } ;
284
+ let req = Chain :: new ( )
285
+ . chain ( AuthRequired )
286
+ . chain ( ChannelLoad )
287
+ . apply ( req, app)
288
+ . await ?;
323
289
324
290
create_validator_messages ( req, & app) . await
325
291
} else if let ( Some ( caps) , & Method :: GET ) = ( CHANNEL_EVENTS_AGGREGATES . captures ( & path) , method) {
326
- if req. extensions ( ) . get :: < Session > ( ) . is_none ( ) {
327
- return Err ( ResponseError :: Unauthorized ) ;
328
- }
292
+ req = AuthRequired . call ( req, app) . await ?;
329
293
330
294
let param = RouteParams ( vec ! [
331
295
caps. get( 1 )
@@ -335,7 +299,7 @@ async fn channels_router<A: Adapter + 'static>(
335
299
] ) ;
336
300
req. extensions_mut ( ) . insert ( param) ;
337
301
338
- let req = chain ( req, app, vec ! [ Box :: new ( channel_load ) ] ) . await ?;
302
+ req = ChannelLoad . call ( req, app) . await ?;
339
303
340
304
list_channel_event_aggregates ( req, app) . await
341
305
} else {
@@ -365,6 +329,12 @@ where
365
329
}
366
330
}
367
331
332
+ impl Into < Response < Body > > for ResponseError {
333
+ fn into ( self ) -> Response < Body > {
334
+ map_response_error ( self )
335
+ }
336
+ }
337
+
368
338
pub fn map_response_error ( error : ResponseError ) -> Response < Body > {
369
339
match error {
370
340
ResponseError :: NotFound => not_found ( ) ,
0 commit comments