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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
 *
 * Use of this source code is governed by the Apache 2.0 and/or the MIT
 * License.
 */
//!<div align="center"><h1>Actix Authentication Middleware</h1>
//!
//![![Documentation](https://img.shields.io/badge/docs-master-blue)](https://realaravinth.github.io/actix-auth-middleware/actix_auth_middleware/)
//![![Build](https://github.com/realaravinth/actix-auth-middleware/actions/workflows/linux.yml/badge.svg)](https://github.com/realaravinth/actix-auth-middleware/actions/workflows/linux.yml)
//![![codecov](https://codecov.io/gh/realaravinth/actix-auth-middleware/branch/master/graph/badge.svg?token=TYZXLOOHYQ)](https://codecov.io/gh/realaravinth/actix-auth-middleware)
//!
//![![dependency status](https://deps.rs/repo/github/realaravinth/actix-auth-middleware/status.svg)](https://deps.rs/repo/github/realaravinth/actix-auth-middleware)
//!
//!  <p>
//!    <strong>Checks if session is authenticated</strong>
//!  </p>
//!<br /></div>
//!
//! ## What
//!
//! This library provides a generic middleware to protect authenticated
//! routes from unauthenticated access. The middleware provides options to
//! customise authentication checking mechanism, which enables it to
//! support a wide range of session management mechanisms.
//!
//! If a session is authenticated, then the request will be dispatched to
//! the matching handler and if unauthenticated, it will be redirected to a
//! user specified route, ideally a sign in route.
//!
//! ## Usage
//!
//! ```rust,no_run
//! use actix_auth_middleware::{Authentication, GetLoginRoute};
//!
//! use actix_identity::{CookieIdentityPolicy, Identity, IdentityService};
//! use actix_web::http::header;
//! use actix_web::FromRequest;
//! use actix_web::{App, HttpResponse, HttpServer};
//! use actix_web::{web, Responder};
//! use serde::Deserialize;
//!
//! pub struct Routes {
//!     signin: &'static str,
//!     authenticated_route: &'static str,
//! }
//!
//! impl Routes {
//!     const fn new() -> Self {
//!         let signin = "/siginin";
//!         let authenticated_route = "/";
//!
//!         Self {
//!             signin,
//!             authenticated_route,
//!         }
//!     }
//! }
//!
//! impl GetLoginRoute for Routes {
//!     fn get_login_route(&self, src: Option<&str>) -> String {
//!         if let Some(redirect_to) = src {
//!             format!(
//!                 "{}?redirect_to={}",
//!                 self.signin,
//!                 urlencoding::encode(redirect_to)
//!             )
//!         } else {
//!             self.signin.to_string()
//!         }
//!     }
//! }
//!
//! pub const ROUTES: Routes = Routes::new();
//!
//! fn get_middleware() -> Authentication<Routes> {
//!     Authentication::with_identity(ROUTES)
//! }
//!
//! fn get_identity_service() -> IdentityService<CookieIdentityPolicy> {
//!     IdentityService::new(
//!         CookieIdentityPolicy::new(&[0; 32])
//!             .path("/")
//!             .name("auth")
//!             .max_age_secs(60 * 60 * 24 * 365)
//!             .domain("localhost")
//!             .secure(false),
//!     )
//! }
//!
//! #[derive(Deserialize)]
//! pub struct RedirectQuery {
//!     pub redirect_to: Option<String>,
//! }
//!
//! #[my_codegen::get(path = "ROUTES.signin")]
//! async fn signin_route_hander(id: Identity, path: web::Query<RedirectQuery>) -> HttpResponse {
//!     id.remember("foo".into());
//!     println!("authenticated");
//!     let path = path.into_inner();
//!     if let Some(redirect_to) = path.redirect_to {
//!         println!("redirecting");
//!         HttpResponse::Found()
//!             .insert_header((header::LOCATION, redirect_to))
//!             .finish()
//!     } else {
//!         let page = format!(
//!             "
//!         <html>
//!         <body>
//!         <p>
//!         You are authenticated
//!         <a href='/{}'>Click here to view restricted resource</a>
//!         </p>
//!         </body>
//!         </html>
//!         ",
//!             ROUTES.authenticated_route
//!         );
//!         HttpResponse::Ok()
//!             .content_type("text/html; charset=utf-8")
//!             .body(page)
//!     }
//! }
//!
//! fn services(cfg: &mut web::ServiceConfig) {
//!     cfg.service(signin_route_hander);
//!     cfg.service(authenticated_route_handler);
//! }
//!
//! #[my_codegen::get(path = "ROUTES.authenticated_route", wrap = "get_middleware()")]
//! async fn authenticated_route_handler() -> impl Responder {
//!     HttpResponse::Ok()
//!         .content_type("text/html; charset=utf-8")
//!         .body("You are viewing a restricted resoucre")
//! }
//!
//! #[actix_web::main]
//! async fn main() -> std::io::Result<()> {
//!     HttpServer::new(move || App::new().wrap(get_identity_service()).configure(services))
//!         .bind("localhost:7000")
//!         .unwrap()
//!         .run()
//!         .await?;
//!     Ok(())
//! }
//!```
use std::rc::Rc;

use actix_http::body::BoxBody;
use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::{dev::Payload, http, Error, HttpRequest, HttpResponse};

use futures::future::{ok, Either, Ready};

/// The route to which unauthenticated sessions should be redirected to. `src` specifies the
/// destination of the request, which can be used to redirect the user post authentication
pub trait GetLoginRoute {
    ///
    /// ```rust
    ///use actix_auth_middleware::{Authentication, GetLoginRoute};
    ///
    /// pub struct Routes {
    ///     signin: &'static str,
    ///     authenticated_route: &'static str,
    /// }
    ///
    /// impl GetLoginRoute for Routes {
    /// // return login route and if redirection mechanism is implemented at the login
    /// // handler, then set redirection location
    ///     fn get_login_route(&self, src: Option<&str>) -> String {
    ///         if let Some(redirect_to) = src {
    ///             format!(
    ///                 "{}?redirect_to={}",
    ///                 self.signin,
    ///                 urlencoding::encode(redirect_to)
    ///             )
    ///         } else {
    ///             self.signin.to_string()
    ///         }
    ///     }
    /// }
    /// ```
    fn get_login_route(&self, src: Option<&str>) -> String;
}

/// Function to check if a request is authenticated
/// ```rust
/// # use actix_identity::{CookieIdentityPolicy, Identity, IdentityService};
/// # use actix_web::http::header;
/// # use actix_web::FromRequest;
/// # use actix_web::{dev::Payload, App, HttpRequest, HttpResponse, HttpServer};
/// # use actix_web::{web, Responder};
/// // implementation for actix::Identity based session management
/// fn is_authenticated(r: &HttpRequest, pl: &mut Payload) -> bool {
///     matches!(
///         Identity::from_request(r, pl)
///             .into_inner()
///             .map(|id| id.identity()),
///         Ok(Some(_))
///     )
/// }
/// ```
pub type IsAuthenticated = fn(&HttpRequest, &mut Payload) -> bool;

/// Authentication middleware configuration
pub struct Authentication<T: GetLoginRoute> {
    login: Rc<T>,
    is_authenticated: IsAuthenticated,
}

impl<T: GetLoginRoute> Authentication<T> {
    /// Create a new instance of authentication middleware
    pub fn new(login: T, is_authenticated: IsAuthenticated) -> Self {
        let login = Rc::new(login);
        Self {
            login,
            is_authenticated,
        }
    }

    #[cfg(feature = "actix_identity_backend")]
    /// `actix::identity` backend
    pub fn with_identity(login: T) -> Authentication<T> {
        use actix_web::FromRequest;

        fn is_authenticated(r: &HttpRequest, pl: &mut Payload) -> bool {
            matches!(
                actix_identity::Identity::from_request(r, pl)
                    .into_inner()
                    .map(|id| id.identity()),
                Ok(Some(_))
            )
        }

        Authentication::new(login, is_authenticated)
    }
}

impl<S, GT> Transform<S, ServiceRequest> for Authentication<GT>
where
    S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error>,
    S::Future: 'static,
    GT: GetLoginRoute,
{
    type Response = ServiceResponse<BoxBody>;
    type Error = Error;
    type Transform = AuthenticationMiddleware<S, GT>;
    type InitError = ();
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ok(AuthenticationMiddleware {
            service,
            login: self.login.clone(),
            is_authenticated: self.is_authenticated,
            //          session_type: self.session_type.clone(),
        })
    }
}

/// Authentication middleware
pub struct AuthenticationMiddleware<S, GT: GetLoginRoute> {
    service: S,
    login: Rc<GT>,
    is_authenticated: IsAuthenticated,
}

impl<S, GT> Service<ServiceRequest> for AuthenticationMiddleware<S, GT>
where
    S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error>,
    S::Future: 'static,
    GT: GetLoginRoute,
{
    type Response = ServiceResponse<BoxBody>;
    type Error = Error;
    #[allow(clippy::type_complexity)]
    type Future = Either<S::Future, Ready<Result<Self::Response, Self::Error>>>;

    actix_service::forward_ready!(service);

    fn call(&self, req: ServiceRequest) -> Self::Future {
        let (r, mut pl) = req.into_parts();

        if (self.is_authenticated)(&r, &mut pl) {
            let req = ServiceRequest::from_parts(r, pl);
            Either::Left(self.service.call(req))
        } else {
            let path = r.uri().path_and_query().map(|path| path.as_str());
            let path = self.login.get_login_route(path);
            let req = ServiceRequest::from_parts(r, pl);
            Either::Right(ok(req.into_response(
                HttpResponse::Found()
                    .insert_header((http::header::LOCATION, path))
                    .finish(),
            )))
        }
    }
}

#[cfg(test)]
#[macro_use]
mod tests {
    use super::*;

    use actix_http::body::EitherBody;
    use actix_identity::{CookieIdentityPolicy, Identity, IdentityService};
    use actix_web::cookie::Cookie;
    use actix_web::http::header;
    use actix_web::{dev::ServiceResponse, http::StatusCode};
    use actix_web::{test, web, Responder};
    use serde::Deserialize;
    use url::Url;

    fn get_middleware() -> Authentication<Routes> {
        Authentication::with_identity(ROUTES)
    }

    fn get_identity_service() -> IdentityService<CookieIdentityPolicy> {
        IdentityService::new(
            CookieIdentityPolicy::new(&[0; 32])
                .path("/")
                .name("auth")
                .max_age_secs(60 * 60 * 24 * 365)
                .domain("example.com")
                .secure(false),
        )
    }

    #[macro_export]
    macro_rules! get_cookie {
        ($resp:expr) => {
            $resp.response().cookies().next().unwrap().to_owned()
        };
    }

    #[macro_export]
    macro_rules! post_request {
        ($uri:expr) => {
            test::TestRequest::post().uri($uri)
        };

        ($uri:expr) => {
            test::TestRequest::post().uri($uri)
        };

        ($serializable:expr, $uri:expr, FORM) => {
            test::TestRequest::post().uri($uri).set_form($serializable)
        };
    }

    #[macro_export]
    macro_rules! get_app {
        ("APP") => {
            actix_web::App::new()
                .wrap(get_identity_service())
                .configure(services)
        };

        () => {
            test::init_service(get_app!("APP"))
        };
    }

    /// signin util
    pub async fn signin() -> ServiceResponse<EitherBody<BoxBody>> {
        let app = get_app!().await;

        let signin_resp = test::call_service(&app, post_request!(ROUTES.signin).to_request()).await;
        assert_eq!(signin_resp.status(), StatusCode::OK);
        signin_resp
    }

    pub async fn authenticated_route(cookies: Cookie<'_>) -> ServiceResponse<EitherBody<BoxBody>> {
        let app = get_app!().await;
        let auth_resp = test::call_service(
            &app,
            post_request!(ROUTES.authenticated_route)
                .cookie(cookies)
                .to_request(),
        )
        .await;
        assert_eq!(auth_resp.status(), StatusCode::OK);
        auth_resp
    }

    struct Routes {
        signin: &'static str,
        authenticated_route: &'static str,
    }

    impl Routes {
        const fn new() -> Self {
            let signin = "/siginin";
            let authenticated_route = "/authenticated/route";

            Self {
                signin,
                authenticated_route,
            }
        }
    }

    impl GetLoginRoute for Routes {
        fn get_login_route(&self, src: Option<&str>) -> String {
            if let Some(redirect_to) = src {
                format!(
                    "{}?redirect_to={}",
                    self.signin,
                    urlencoding::encode(redirect_to)
                )
            } else {
                self.signin.to_string()
            }
        }
    }

    const ROUTES: Routes = Routes::new();

    fn services(cfg: &mut web::ServiceConfig) {
        cfg.service(signin_route_hander);
        cfg.service(authenticated_route_handler);
    }

    #[derive(Deserialize)]
    pub struct RedirectQuery {
        pub redirect_to: Option<String>,
    }

    #[my_codegen::post(path = "ROUTES.signin")]
    async fn signin_route_hander(id: Identity, path: web::Query<RedirectQuery>) -> HttpResponse {
        id.remember("foo".into());
        let path = path.into_inner();
        if let Some(redirect_to) = path.redirect_to {
            HttpResponse::Found()
                .insert_header((header::LOCATION, redirect_to))
                .finish()
        } else {
            HttpResponse::Ok().into()
        }
    }

    #[my_codegen::post(path = "ROUTES.authenticated_route", wrap = "get_middleware()")]
    async fn authenticated_route_handler() -> impl Responder {
        HttpResponse::Ok()
    }

    #[actix_rt::test]
    async fn auth_middleware_works() {
        fn make_uri(path: &str, queries: &Option<Vec<(&str, &str)>>) -> String {
            let mut url = Url::parse("http://x/").unwrap();
            url.set_path(path);

            if let Some(queries) = queries {
                {
                    let mut query_pairs = url.query_pairs_mut();
                    queries.iter().for_each(|(k, v)| {
                        query_pairs.append_pair(k, v);
                    });
                }

                format!("{}?{}", url.path(), url.query().unwrap())
            } else {
                url.path().to_string()
            }
        }

        let queries = Some(vec![
            ("foo", "bar"),
            ("src", "/x/y/z"),
            ("with_q", "/a/b/c/?goo=x"),
        ]);

        let signin_resp = signin().await;
        let cookies = get_cookie!(signin_resp);

        let bench_routes = vec![
            (&ROUTES.authenticated_route, queries.clone()),
            (&ROUTES.authenticated_route, None),
        ];

        let app = get_app!().await;

        for (from, query) in bench_routes.iter() {
            let route = make_uri(from, query);
            let unauth_req = test::call_service(&app, post_request!(&route).to_request()).await;
            assert_eq!(unauth_req.status(), StatusCode::FOUND);

            let redirect_to = ROUTES.get_login_route(Some(&route));
            let headers = unauth_req.headers();
            assert_eq!(headers.get(header::LOCATION).unwrap(), &redirect_to);

            let auth_req = authenticated_route(cookies.clone()).await;
            assert_eq!(auth_req.status(), StatusCode::OK);
        }
    }
}