Can force 2FA authent
This commit is contained in:
36
src/data/force_2fa_auth.rs
Normal file
36
src/data/force_2fa_auth.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use crate::data::current_user::CurrentUser;
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::{web, Error, FromRequest, HttpRequest};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Force2FAAuthQuery {
|
||||
#[serde(default)]
|
||||
force_2fa: bool,
|
||||
}
|
||||
|
||||
pub struct Force2FAAuth {
|
||||
pub force: bool,
|
||||
}
|
||||
|
||||
impl FromRequest for Force2FAAuth {
|
||||
type Error = Error;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
let req = req.clone();
|
||||
|
||||
let query = web::Query::<Force2FAAuthQuery>::from_request(&req, payload)
|
||||
.into_inner()
|
||||
.unwrap();
|
||||
|
||||
Box::pin(async move {
|
||||
let user = CurrentUser::from_request(&req, &mut Payload::None).await?;
|
||||
|
||||
Ok(Self {
|
||||
force: query.force_2fa && user.has_two_factor(),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user