From b057e48d72723316daf1f476d3c685f0d157e977 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 7 May 2022 12:29:02 +0530 Subject: [PATCH] feat: migrate pages mod to use actix_auth_middleware --- src/pages/auth/login.rs | 1 + src/pages/auth/mod.rs | 16 ++++++++++++++++ src/pages/mod.rs | 6 +++++- src/pages/panel/mod.rs | 5 ++++- src/pages/panel/notifications.rs | 5 ++++- src/pages/panel/settings.rs | 9 ++++++--- src/pages/panel/sitekey/add.rs | 4 ++-- src/pages/panel/sitekey/delete.rs | 5 ++++- src/pages/panel/sitekey/edit.rs | 4 ++-- src/pages/panel/sitekey/list.rs | 5 ++++- src/pages/panel/sitekey/view.rs | 5 ++++- src/pages/routes.rs | 8 ++++++++ 12 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/pages/auth/login.rs b/src/pages/auth/login.rs index fe1e399e..af06c1d0 100644 --- a/src/pages/auth/login.rs +++ b/src/pages/auth/login.rs @@ -20,6 +20,7 @@ use lazy_static::lazy_static; use my_codegen::get; use sailfish::TemplateOnce; +use crate::api::v1::RedirectQuery; use crate::PAGES; #[derive(Clone, TemplateOnce)] diff --git a/src/pages/auth/mod.rs b/src/pages/auth/mod.rs index 05c56ed4..96f68f0b 100644 --- a/src/pages/auth/mod.rs +++ b/src/pages/auth/mod.rs @@ -25,6 +25,8 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) { } pub mod routes { + use actix_auth_middleware::GetLoginRoute; + pub struct Auth { pub login: &'static str, pub join: &'static str, @@ -42,4 +44,18 @@ pub mod routes { [AUTH.login, AUTH.join] } } + + impl GetLoginRoute for Auth { + fn get_login_route(&self, src: Option<&str>) -> String { + if let Some(redirect_to) = src { + format!( + "{}?redirect_to={}", + self.login, + urlencoding::encode(redirect_to) + ) + } else { + self.login.to_string() + } + } + } } diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 745c79aa..383c6fd7 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -13,7 +13,7 @@ * * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ - +use actix_auth_middleware::Authentication; use actix_web::web::ServiceConfig; mod auth; @@ -31,6 +31,10 @@ pub fn services(cfg: &mut ServiceConfig) { cfg.service(sitemap::sitemap); } +pub fn get_middleware() -> Authentication { + Authentication::with_identity(routes::ROUTES) +} + #[cfg(not(tarpaulin_include))] #[cfg(test)] mod tests { diff --git a/src/pages/panel/mod.rs b/src/pages/panel/mod.rs index 5affc2ac..475a87c0 100644 --- a/src/pages/panel/mod.rs +++ b/src/pages/panel/mod.rs @@ -41,7 +41,10 @@ impl IndexPage { const PAGE: &str = "Dashboard"; -#[my_codegen::get(path = "crate::PAGES.panel.home", wrap = "crate::CheckLogin")] +#[my_codegen::get( + path = "crate::PAGES.panel.home", + wrap = "crate::pages::get_middleware()" +)] async fn panel(data: AppData, id: Identity) -> PageResult { let sitekeys = get_list_sitekeys(&data, &id).await?; let body = IndexPage::new(sitekeys).render_once().unwrap(); diff --git a/src/pages/panel/notifications.rs b/src/pages/panel/notifications.rs index de3a8765..35859353 100644 --- a/src/pages/panel/notifications.rs +++ b/src/pages/panel/notifications.rs @@ -66,7 +66,10 @@ impl Notification { const PAGE: &str = "Notifications"; -#[my_codegen::get(path = "crate::PAGES.panel.notifications", wrap = "crate::CheckLogin")] +#[my_codegen::get( + path = "crate::PAGES.panel.notifications", + wrap = "crate::pages::get_middleware()" +)] pub async fn notifications(data: AppData, id: Identity) -> PageResult { let receiver = id.identity().unwrap(); // TODO handle error where payload.to doesnt exist diff --git a/src/pages/panel/settings.rs b/src/pages/panel/settings.rs index 880ca5e9..bde2c647 100644 --- a/src/pages/panel/settings.rs +++ b/src/pages/panel/settings.rs @@ -62,7 +62,10 @@ pub struct IndexPage<'a> { username: &'a str, } -#[my_codegen::get(path = "crate::PAGES.panel.settings.home", wrap = "crate::CheckLogin")] +#[my_codegen::get( + path = "crate::PAGES.panel.settings.home", + wrap = "crate::pages::get_middleware()" +)] async fn settings(data: AppData, id: Identity) -> PageResult { let username = id.identity().unwrap(); @@ -93,7 +96,7 @@ async fn settings(data: AppData, id: Identity) -> PageResult { #[my_codegen::get( path = "crate::PAGES.panel.settings.delete_account", - wrap = "crate::CheckLogin" + wrap = "crate::pages::get_middleware()" )] async fn delete_account() -> impl Responder { let page = SudoPage::::new(crate::V1_API_ROUTES.account.delete, None) @@ -106,7 +109,7 @@ async fn delete_account() -> impl Responder { #[my_codegen::get( path = "crate::PAGES.panel.settings.update_secret", - wrap = "crate::CheckLogin" + wrap = "crate::pages::get_middleware()" )] async fn update_secret() -> impl Responder { let page = SudoPage::::new(crate::V1_API_ROUTES.account.update_secret, None) diff --git a/src/pages/panel/sitekey/add.rs b/src/pages/panel/sitekey/add.rs index 11abd361..1210e4de 100644 --- a/src/pages/panel/sitekey/add.rs +++ b/src/pages/panel/sitekey/add.rs @@ -49,7 +49,7 @@ impl<'a> Default for AdvanceIndexPage<'a> { #[my_codegen::get( path = "crate::PAGES.panel.sitekey.add_advance", - wrap = "crate::CheckLogin" + wrap = "crate::pages::get_middleware()" )] pub async fn advance() -> impl Responder { HttpResponse::Ok() @@ -81,7 +81,7 @@ impl<'a> Default for EasyIndexPage<'a> { #[my_codegen::get( path = "crate::PAGES.panel.sitekey.add_easy", - wrap = "crate::CheckLogin" + wrap = "crate::pages::get_middleware()" )] pub async fn easy() -> impl Responder { HttpResponse::Ok() diff --git a/src/pages/panel/sitekey/delete.rs b/src/pages/panel/sitekey/delete.rs index d9ba17e3..25985994 100644 --- a/src/pages/panel/sitekey/delete.rs +++ b/src/pages/panel/sitekey/delete.rs @@ -22,7 +22,10 @@ use sailfish::TemplateOnce; use crate::pages::auth::sudo::SudoPage; use crate::{PAGES, V1_API_ROUTES}; -#[get(path = "PAGES.panel.sitekey.delete", wrap = "crate::CheckLogin")] +#[get( + path = "PAGES.panel.sitekey.delete", + wrap = "crate::pages::get_middleware()" +)] pub async fn delete_sitekey(path: web::Path) -> impl Responder { let key = path.into_inner(); let data = vec![("sitekey", key)]; diff --git a/src/pages/panel/sitekey/edit.rs b/src/pages/panel/sitekey/edit.rs index d67db755..addab26c 100644 --- a/src/pages/panel/sitekey/edit.rs +++ b/src/pages/panel/sitekey/edit.rs @@ -61,7 +61,7 @@ impl AdvanceEditPage { /// route handler that renders individual views for sitekeys #[my_codegen::get( path = "crate::PAGES.panel.sitekey.edit_advance", - wrap = "crate::CheckLogin" + wrap = "crate::pages::get_middleware()" )] pub async fn advance( path: web::Path, @@ -123,7 +123,7 @@ impl<'a> EasyEditPage<'a> { /// route handler that renders individual views for sitekeys #[my_codegen::get( path = "crate::PAGES.panel.sitekey.edit_easy", - wrap = "crate::CheckLogin" + wrap = "crate::pages::get_middleware()" )] pub async fn easy( path: web::Path, diff --git a/src/pages/panel/sitekey/list.rs b/src/pages/panel/sitekey/list.rs index 148c3bd7..a49b3306 100644 --- a/src/pages/panel/sitekey/list.rs +++ b/src/pages/panel/sitekey/list.rs @@ -38,7 +38,10 @@ impl IndexPage { } /// render a list of all sitekeys that a user has -#[my_codegen::get(path = "crate::PAGES.panel.sitekey.list", wrap = "crate::CheckLogin")] +#[my_codegen::get( + path = "crate::PAGES.panel.sitekey.list", + wrap = "crate::pages::get_middleware()" +)] pub async fn list_sitekeys(data: AppData, id: Identity) -> PageResult { let res = get_list_sitekeys(&data, &id).await?; let body = IndexPage::new(res).render_once().unwrap(); diff --git a/src/pages/panel/sitekey/view.rs b/src/pages/panel/sitekey/view.rs index e5d9e195..96582ddb 100644 --- a/src/pages/panel/sitekey/view.rs +++ b/src/pages/panel/sitekey/view.rs @@ -66,7 +66,10 @@ impl IndexPage { } /// route handler that renders individual views for sitekeys -#[my_codegen::get(path = "crate::PAGES.panel.sitekey.view", wrap = "crate::CheckLogin")] +#[my_codegen::get( + path = "crate::PAGES.panel.sitekey.view", + wrap = "crate::pages::get_middleware()" +)] pub async fn view_sitekey( path: web::Path, data: AppData, diff --git a/src/pages/routes.rs b/src/pages/routes.rs index 85d87bfe..79cf8270 100644 --- a/src/pages/routes.rs +++ b/src/pages/routes.rs @@ -14,10 +14,12 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +use actix_auth_middleware::GetLoginRoute; use super::auth::routes::Auth; use super::errors::routes::Errors; use super::panel::routes::Panel; + pub const ROUTES: Routes = Routes::new(); pub struct Routes { @@ -58,6 +60,12 @@ impl Routes { } } +impl GetLoginRoute for Routes { + fn get_login_route(&self, src: Option<&str>) -> String { + self.auth.get_login_route(src) + } +} + #[cfg(test)] mod tests { use super::*;