Can remove created factors
This commit is contained in:
@ -6,4 +6,4 @@ pub mod admin_controller;
|
||||
pub mod admin_api;
|
||||
pub mod openid_controller;
|
||||
pub mod two_factors_controller;
|
||||
pub mod two_factors_api;
|
||||
pub mod two_factor_api;
|
@ -1,20 +1,21 @@
|
||||
use actix::Addr;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::actors::users_actor;
|
||||
use crate::actors::users_actor::UsersActor;
|
||||
use crate::data::current_user::CurrentUser;
|
||||
use crate::data::totp_key::TotpKey;
|
||||
use crate::data::user::{SecondFactor, User};
|
||||
use crate::data::user::{FactorID, SecondFactor, SecondFactorType, User};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Request {
|
||||
pub struct AddTOTPRequest {
|
||||
factor_name: String,
|
||||
secret: String,
|
||||
first_code: String,
|
||||
}
|
||||
|
||||
pub async fn save_totp_factor(user: CurrentUser, form: web::Json<Request>,
|
||||
pub async fn save_totp_factor(user: CurrentUser, form: web::Json<AddTOTPRequest>,
|
||||
users: web::Data<Addr<UsersActor>>) -> impl Responder {
|
||||
let key = TotpKey::from_encoded_secret(&form.secret);
|
||||
|
||||
@ -30,7 +31,11 @@ pub async fn save_totp_factor(user: CurrentUser, form: web::Json<Request>,
|
||||
}
|
||||
|
||||
let mut user = User::from(user);
|
||||
user.add_factor(SecondFactor::TOTP(key));
|
||||
user.add_factor(SecondFactor {
|
||||
id: FactorID(Uuid::new_v4().to_string()),
|
||||
name: form.0.factor_name,
|
||||
kind: SecondFactorType::TOTP(key),
|
||||
});
|
||||
let res = users.send(users_actor::UpdateUserRequest(user)).await.unwrap().0;
|
||||
|
||||
if !res {
|
||||
@ -38,4 +43,23 @@ pub async fn save_totp_factor(user: CurrentUser, form: web::Json<Request>,
|
||||
} else {
|
||||
HttpResponse::Ok().body("Added new factor!")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DeleteFactorRequest {
|
||||
id: FactorID,
|
||||
}
|
||||
|
||||
pub async fn delete_factor(user: CurrentUser, form: web::Json<DeleteFactorRequest>,
|
||||
users: web::Data<Addr<UsersActor>>) -> impl Responder {
|
||||
let mut user = User::from(user);
|
||||
user.remove_factor(form.0.id);
|
||||
|
||||
let res = users.send(users_actor::UpdateUserRequest(user)).await.unwrap().0;
|
||||
|
||||
if !res {
|
||||
HttpResponse::InternalServerError().body("Failed to update user information!")
|
||||
} else {
|
||||
HttpResponse::Ok().body("Removed factor!")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user