mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-02-17 08:59:46 +03:00
feat: migrate pages mod to use actix_auth_middleware
This commit is contained in:
parent
abe494b6e5
commit
b057e48d72
12 changed files with 60 additions and 13 deletions
|
@ -20,6 +20,7 @@ use lazy_static::lazy_static;
|
||||||
use my_codegen::get;
|
use my_codegen::get;
|
||||||
use sailfish::TemplateOnce;
|
use sailfish::TemplateOnce;
|
||||||
|
|
||||||
|
use crate::api::v1::RedirectQuery;
|
||||||
use crate::PAGES;
|
use crate::PAGES;
|
||||||
|
|
||||||
#[derive(Clone, TemplateOnce)]
|
#[derive(Clone, TemplateOnce)]
|
||||||
|
|
|
@ -25,6 +25,8 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod routes {
|
pub mod routes {
|
||||||
|
use actix_auth_middleware::GetLoginRoute;
|
||||||
|
|
||||||
pub struct Auth {
|
pub struct Auth {
|
||||||
pub login: &'static str,
|
pub login: &'static str,
|
||||||
pub join: &'static str,
|
pub join: &'static str,
|
||||||
|
@ -42,4 +44,18 @@ pub mod routes {
|
||||||
[AUTH.login, AUTH.join]
|
[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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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/>.
|
* 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;
|
use actix_web::web::ServiceConfig;
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
|
@ -31,6 +31,10 @@ pub fn services(cfg: &mut ServiceConfig) {
|
||||||
cfg.service(sitemap::sitemap);
|
cfg.service(sitemap::sitemap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_middleware() -> Authentication<routes::Routes> {
|
||||||
|
Authentication::with_identity(routes::ROUTES)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -41,7 +41,10 @@ impl IndexPage {
|
||||||
|
|
||||||
const PAGE: &str = "Dashboard";
|
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> {
|
async fn panel(data: AppData, id: Identity) -> PageResult<impl Responder> {
|
||||||
let sitekeys = get_list_sitekeys(&data, &id).await?;
|
let sitekeys = get_list_sitekeys(&data, &id).await?;
|
||||||
let body = IndexPage::new(sitekeys).render_once().unwrap();
|
let body = IndexPage::new(sitekeys).render_once().unwrap();
|
||||||
|
|
|
@ -66,7 +66,10 @@ impl Notification {
|
||||||
|
|
||||||
const PAGE: &str = "Notifications";
|
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> {
|
pub async fn notifications(data: AppData, id: Identity) -> PageResult<impl Responder> {
|
||||||
let receiver = id.identity().unwrap();
|
let receiver = id.identity().unwrap();
|
||||||
// TODO handle error where payload.to doesnt exist
|
// TODO handle error where payload.to doesnt exist
|
||||||
|
|
|
@ -62,7 +62,10 @@ pub struct IndexPage<'a> {
|
||||||
username: &'a str,
|
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> {
|
async fn settings(data: AppData, id: Identity) -> PageResult<impl Responder> {
|
||||||
let username = id.identity().unwrap();
|
let username = id.identity().unwrap();
|
||||||
|
|
||||||
|
@ -93,7 +96,7 @@ async fn settings(data: AppData, id: Identity) -> PageResult<impl Responder> {
|
||||||
|
|
||||||
#[my_codegen::get(
|
#[my_codegen::get(
|
||||||
path = "crate::PAGES.panel.settings.delete_account",
|
path = "crate::PAGES.panel.settings.delete_account",
|
||||||
wrap = "crate::CheckLogin"
|
wrap = "crate::pages::get_middleware()"
|
||||||
)]
|
)]
|
||||||
async fn delete_account() -> impl Responder {
|
async fn delete_account() -> impl Responder {
|
||||||
let page = SudoPage::<u8, u8>::new(crate::V1_API_ROUTES.account.delete, None)
|
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(
|
#[my_codegen::get(
|
||||||
path = "crate::PAGES.panel.settings.update_secret",
|
path = "crate::PAGES.panel.settings.update_secret",
|
||||||
wrap = "crate::CheckLogin"
|
wrap = "crate::pages::get_middleware()"
|
||||||
)]
|
)]
|
||||||
async fn update_secret() -> impl Responder {
|
async fn update_secret() -> impl Responder {
|
||||||
let page = SudoPage::<u8, u8>::new(crate::V1_API_ROUTES.account.update_secret, None)
|
let page = SudoPage::<u8, u8>::new(crate::V1_API_ROUTES.account.update_secret, None)
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl<'a> Default for AdvanceIndexPage<'a> {
|
||||||
|
|
||||||
#[my_codegen::get(
|
#[my_codegen::get(
|
||||||
path = "crate::PAGES.panel.sitekey.add_advance",
|
path = "crate::PAGES.panel.sitekey.add_advance",
|
||||||
wrap = "crate::CheckLogin"
|
wrap = "crate::pages::get_middleware()"
|
||||||
)]
|
)]
|
||||||
pub async fn advance() -> impl Responder {
|
pub async fn advance() -> impl Responder {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
|
@ -81,7 +81,7 @@ impl<'a> Default for EasyIndexPage<'a> {
|
||||||
|
|
||||||
#[my_codegen::get(
|
#[my_codegen::get(
|
||||||
path = "crate::PAGES.panel.sitekey.add_easy",
|
path = "crate::PAGES.panel.sitekey.add_easy",
|
||||||
wrap = "crate::CheckLogin"
|
wrap = "crate::pages::get_middleware()"
|
||||||
)]
|
)]
|
||||||
pub async fn easy() -> impl Responder {
|
pub async fn easy() -> impl Responder {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
|
|
|
@ -22,7 +22,10 @@ use sailfish::TemplateOnce;
|
||||||
use crate::pages::auth::sudo::SudoPage;
|
use crate::pages::auth::sudo::SudoPage;
|
||||||
use crate::{PAGES, V1_API_ROUTES};
|
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 {
|
pub async fn delete_sitekey(path: web::Path<String>) -> impl Responder {
|
||||||
let key = path.into_inner();
|
let key = path.into_inner();
|
||||||
let data = vec![("sitekey", key)];
|
let data = vec![("sitekey", key)];
|
||||||
|
|
|
@ -61,7 +61,7 @@ impl AdvanceEditPage {
|
||||||
/// route handler that renders individual views for sitekeys
|
/// route handler that renders individual views for sitekeys
|
||||||
#[my_codegen::get(
|
#[my_codegen::get(
|
||||||
path = "crate::PAGES.panel.sitekey.edit_advance",
|
path = "crate::PAGES.panel.sitekey.edit_advance",
|
||||||
wrap = "crate::CheckLogin"
|
wrap = "crate::pages::get_middleware()"
|
||||||
)]
|
)]
|
||||||
pub async fn advance(
|
pub async fn advance(
|
||||||
path: web::Path<String>,
|
path: web::Path<String>,
|
||||||
|
@ -123,7 +123,7 @@ impl<'a> EasyEditPage<'a> {
|
||||||
/// route handler that renders individual views for sitekeys
|
/// route handler that renders individual views for sitekeys
|
||||||
#[my_codegen::get(
|
#[my_codegen::get(
|
||||||
path = "crate::PAGES.panel.sitekey.edit_easy",
|
path = "crate::PAGES.panel.sitekey.edit_easy",
|
||||||
wrap = "crate::CheckLogin"
|
wrap = "crate::pages::get_middleware()"
|
||||||
)]
|
)]
|
||||||
pub async fn easy(
|
pub async fn easy(
|
||||||
path: web::Path<String>,
|
path: web::Path<String>,
|
||||||
|
|
|
@ -38,7 +38,10 @@ impl IndexPage {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// render a list of all sitekeys that a user has
|
/// 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> {
|
pub async fn list_sitekeys(data: AppData, id: Identity) -> PageResult<impl Responder> {
|
||||||
let res = get_list_sitekeys(&data, &id).await?;
|
let res = get_list_sitekeys(&data, &id).await?;
|
||||||
let body = IndexPage::new(res).render_once().unwrap();
|
let body = IndexPage::new(res).render_once().unwrap();
|
||||||
|
|
|
@ -66,7 +66,10 @@ impl IndexPage {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// route handler that renders individual views for sitekeys
|
/// 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(
|
pub async fn view_sitekey(
|
||||||
path: web::Path<String>,
|
path: web::Path<String>,
|
||||||
data: AppData,
|
data: AppData,
|
||||||
|
|
|
@ -14,10 +14,12 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* 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/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
use actix_auth_middleware::GetLoginRoute;
|
||||||
|
|
||||||
use super::auth::routes::Auth;
|
use super::auth::routes::Auth;
|
||||||
use super::errors::routes::Errors;
|
use super::errors::routes::Errors;
|
||||||
use super::panel::routes::Panel;
|
use super::panel::routes::Panel;
|
||||||
|
|
||||||
pub const ROUTES: Routes = Routes::new();
|
pub const ROUTES: Routes = Routes::new();
|
||||||
|
|
||||||
pub struct Routes {
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Add table
Reference in a new issue