auth middleware: redirect to specified route

This commit is contained in:
Aravinth Manivannan 2021-10-11 10:09:51 +05:30
parent d2c4e9a06b
commit d828a4a5d9
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
5 changed files with 32 additions and 13 deletions

View file

@ -221,8 +221,11 @@ async fn login(
id.remember(username); id.remember(username);
Ok(HttpResponse::Ok()) Ok(HttpResponse::Ok())
} }
fn get_check_login() -> crate::CheckLogin {
crate::CheckLogin::new(crate::V1_API_ROUTES.auth.register)
}
#[my_codegen::get(path = "crate::V1_API_ROUTES.auth.logout", wrap = "crate::CheckLogin")] #[my_codegen::get(path = "crate::V1_API_ROUTES.auth.logout", wrap = "get_check_login()")]
async fn signout(id: Identity) -> impl Responder { async fn signout(id: Identity) -> impl Responder {
if id.identity().is_some() { if id.identity().is_some() {
id.forget(); id.forget();
@ -231,4 +234,3 @@ async fn signout(id: Identity) -> impl Responder {
.append_header((header::LOCATION, crate::middleware::auth::AUTH)) .append_header((header::LOCATION, crate::middleware::auth::AUTH))
.finish() .finish()
} }

View file

@ -40,7 +40,11 @@ pub mod routes {
let submit = "/api/v1/benches/submit"; let submit = "/api/v1/benches/submit";
let register = "/api/v1/benches/register"; let register = "/api/v1/benches/register";
let scope = "/api/v1/benches/"; let scope = "/api/v1/benches/";
Benches { submit, register, scope } Benches {
submit,
register,
scope,
}
} }
} }
} }
@ -50,7 +54,6 @@ pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(register); cfg.service(register);
} }
pub mod runners { pub mod runners {
use super::*; use super::*;
@ -93,7 +96,6 @@ async fn register(data: AppData, id: Identity) -> ServiceResult<impl Responder>
Ok(HttpResponse::Ok()) Ok(HttpResponse::Ok())
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Bench { struct Bench {
duration: f32, duration: f32,
@ -114,9 +116,13 @@ struct SubmissionProof {
proof: String, proof: String,
} }
fn get_check_login() -> crate::CheckLogin {
crate::CheckLogin::new(crate::V1_API_ROUTES.benches.register)
}
#[my_codegen::post( #[my_codegen::post(
path = "crate::V1_API_ROUTES.benches.submit", path = "crate::V1_API_ROUTES.benches.submit",
wrap = "crate::CheckLogin" wrap = "get_check_login()"
)] )]
async fn submit( async fn submit(
data: AppData, data: AppData,

View file

@ -18,9 +18,9 @@
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
use argon2_creds::{Config, ConfigBuilder, PasswordPolicy};
use sqlx::postgres::PgPoolOptions; use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool; use sqlx::PgPool;
use argon2_creds::{Config, ConfigBuilder, PasswordPolicy};
use crate::SETTINGS; use crate::SETTINGS;
@ -28,7 +28,7 @@ use crate::SETTINGS;
pub struct Data { pub struct Data {
/// databse pool /// databse pool
pub db: PgPool, pub db: PgPool,
pub creds: Config, pub creds: Config,
} }
impl Data { impl Data {
@ -45,7 +45,7 @@ impl Data {
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]
/// create new instance of app data /// create new instance of app data
pub async fn new() -> Arc<Self> { pub async fn new() -> Arc<Self> {
let creds = Self::get_creds(); let creds = Self::get_creds();
let c = creds.clone(); let c = creds.clone();
#[allow(unused_variables)] #[allow(unused_variables)]
let init = thread::spawn(move || { let init = thread::spawn(move || {

View file

@ -150,7 +150,6 @@ pub fn get_identity_service() -> IdentityService<CookieIdentityPolicy> {
) )
} }
pub fn services(cfg: &mut actix_web::web::ServiceConfig) { pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
//pages::services(cfg); //pages::services(cfg);
api::v1::services(cfg); api::v1::services(cfg);

View file

@ -26,7 +26,15 @@ use futures::future::{ok, Either, Ready};
pub const AUTH: &str = crate::V1_API_ROUTES.auth.register; pub const AUTH: &str = crate::V1_API_ROUTES.auth.register;
pub struct CheckLogin; pub struct CheckLogin {
login: &'static str,
}
impl CheckLogin {
pub fn new(login: &'static str) -> Self {
Self { login }
}
}
impl<S> Transform<S, ServiceRequest> for CheckLogin impl<S> Transform<S, ServiceRequest> for CheckLogin
where where
@ -40,11 +48,15 @@ where
type Future = Ready<Result<Self::Transform, Self::InitError>>; type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
ok(CheckLoginMiddleware { service }) ok(CheckLoginMiddleware {
service,
login: self.login,
})
} }
} }
pub struct CheckLoginMiddleware<S> { pub struct CheckLoginMiddleware<S> {
service: S, service: S,
login: &'static str,
} }
impl<S> Service<ServiceRequest> for CheckLoginMiddleware<S> impl<S> Service<ServiceRequest> for CheckLoginMiddleware<S>
@ -73,7 +85,7 @@ where
let req = ServiceRequest::from_parts(r, pl); //.ok().unwrap(); let req = ServiceRequest::from_parts(r, pl); //.ok().unwrap();
Either::Right(ok(req.into_response( Either::Right(ok(req.into_response(
HttpResponse::Found() HttpResponse::Found()
.insert_header((http::header::LOCATION, AUTH)) .insert_header((http::header::LOCATION, self.login))
.finish(), .finish(),
))) )))
} }