auth middleware: redirect to specified route
This commit is contained in:
parent
d2c4e9a06b
commit
d828a4a5d9
5 changed files with 32 additions and 13 deletions
|
@ -221,8 +221,11 @@ async fn login(
|
|||
id.remember(username);
|
||||
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 {
|
||||
if id.identity().is_some() {
|
||||
id.forget();
|
||||
|
@ -231,4 +234,3 @@ async fn signout(id: Identity) -> impl Responder {
|
|||
.append_header((header::LOCATION, crate::middleware::auth::AUTH))
|
||||
.finish()
|
||||
}
|
||||
|
||||
|
|
|
@ -40,7 +40,11 @@ pub mod routes {
|
|||
let submit = "/api/v1/benches/submit";
|
||||
let register = "/api/v1/benches/register";
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
pub mod runners {
|
||||
use super::*;
|
||||
|
||||
|
@ -93,7 +96,6 @@ async fn register(data: AppData, id: Identity) -> ServiceResult<impl Responder>
|
|||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Bench {
|
||||
duration: f32,
|
||||
|
@ -114,9 +116,13 @@ struct SubmissionProof {
|
|||
proof: String,
|
||||
}
|
||||
|
||||
fn get_check_login() -> crate::CheckLogin {
|
||||
crate::CheckLogin::new(crate::V1_API_ROUTES.benches.register)
|
||||
}
|
||||
|
||||
#[my_codegen::post(
|
||||
path = "crate::V1_API_ROUTES.benches.submit",
|
||||
wrap = "crate::CheckLogin"
|
||||
wrap = "get_check_login()"
|
||||
)]
|
||||
async fn submit(
|
||||
data: AppData,
|
||||
|
|
|
@ -18,9 +18,9 @@
|
|||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
|
||||
use argon2_creds::{Config, ConfigBuilder, PasswordPolicy};
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use sqlx::PgPool;
|
||||
use argon2_creds::{Config, ConfigBuilder, PasswordPolicy};
|
||||
|
||||
use crate::SETTINGS;
|
||||
|
||||
|
@ -28,7 +28,7 @@ use crate::SETTINGS;
|
|||
pub struct Data {
|
||||
/// databse pool
|
||||
pub db: PgPool,
|
||||
pub creds: Config,
|
||||
pub creds: Config,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
|
@ -45,7 +45,7 @@ impl Data {
|
|||
#[cfg(not(tarpaulin_include))]
|
||||
/// create new instance of app data
|
||||
pub async fn new() -> Arc<Self> {
|
||||
let creds = Self::get_creds();
|
||||
let creds = Self::get_creds();
|
||||
let c = creds.clone();
|
||||
#[allow(unused_variables)]
|
||||
let init = thread::spawn(move || {
|
||||
|
|
|
@ -150,7 +150,6 @@ pub fn get_identity_service() -> IdentityService<CookieIdentityPolicy> {
|
|||
)
|
||||
}
|
||||
|
||||
|
||||
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
||||
//pages::services(cfg);
|
||||
api::v1::services(cfg);
|
||||
|
|
|
@ -26,7 +26,15 @@ use futures::future::{ok, Either, Ready};
|
|||
|
||||
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
|
||||
where
|
||||
|
@ -40,11 +48,15 @@ where
|
|||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(CheckLoginMiddleware { service })
|
||||
ok(CheckLoginMiddleware {
|
||||
service,
|
||||
login: self.login,
|
||||
})
|
||||
}
|
||||
}
|
||||
pub struct CheckLoginMiddleware<S> {
|
||||
service: S,
|
||||
login: &'static str,
|
||||
}
|
||||
|
||||
impl<S> Service<ServiceRequest> for CheckLoginMiddleware<S>
|
||||
|
@ -73,7 +85,7 @@ where
|
|||
let req = ServiceRequest::from_parts(r, pl); //.ok().unwrap();
|
||||
Either::Right(ok(req.into_response(
|
||||
HttpResponse::Found()
|
||||
.insert_header((http::header::LOCATION, AUTH))
|
||||
.insert_header((http::header::LOCATION, self.login))
|
||||
.finish(),
|
||||
)))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue