feat: migrate pages mod to use actix_auth_middleware

This commit is contained in:
realaravinth 2022-05-07 12:29:02 +05:30
parent abe494b6e5
commit b057e48d72
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
12 changed files with 60 additions and 13 deletions

View file

@ -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)]

View file

@ -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()
}
}
}
}

View file

@ -13,7 +13,7 @@
*
* You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<routes::Routes> {
Authentication::with_identity(routes::ROUTES)
}
#[cfg(not(tarpaulin_include))]
#[cfg(test)]
mod tests {

View file

@ -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<impl Responder> {
let sitekeys = get_list_sitekeys(&data, &id).await?;
let body = IndexPage::new(sitekeys).render_once().unwrap();

View file

@ -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<impl Responder> {
let receiver = id.identity().unwrap();
// TODO handle error where payload.to doesnt exist

View file

@ -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<impl Responder> {
let username = id.identity().unwrap();
@ -93,7 +96,7 @@ async fn settings(data: AppData, id: Identity) -> PageResult<impl Responder> {
#[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::<u8, u8>::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::<u8, u8>::new(crate::V1_API_ROUTES.account.update_secret, None)

View file

@ -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()

View file

@ -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<String>) -> impl Responder {
let key = path.into_inner();
let data = vec![("sitekey", key)];

View file

@ -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<String>,
@ -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<String>,

View file

@ -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<impl Responder> {
let res = get_list_sitekeys(&data, &id).await?;
let body = IndexPage::new(res).render_once().unwrap();

View file

@ -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<String>,
data: AppData,

View file

@ -14,10 +14,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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::*;