Two factor authentication : TOTP #5

Merged
pierre merged 22 commits from twofactors into master 2022-04-20 07:40:51 +00:00
2 changed files with 10 additions and 10 deletions
Showing only changes of commit 3add7a5d37 - Show all commits

View File

@ -6,7 +6,7 @@ use crate::actors::users_actor;
use crate::actors::users_actor::UsersActor; use crate::actors::users_actor::UsersActor;
use crate::data::current_user::CurrentUser; use crate::data::current_user::CurrentUser;
use crate::data::totp_key::TotpKey; use crate::data::totp_key::TotpKey;
use crate::data::user::{FactorID, SecondFactor, SecondFactorType, User}; use crate::data::user::{FactorID, TwoFactor, TwoFactorType, User};
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct AddTOTPRequest { pub struct AddTOTPRequest {
@ -31,10 +31,10 @@ pub async fn save_totp_factor(user: CurrentUser, form: web::Json<AddTOTPRequest>
} }
let mut user = User::from(user); let mut user = User::from(user);
user.add_factor(SecondFactor { user.add_factor(TwoFactor {
id: FactorID(Uuid::new_v4().to_string()), id: FactorID(Uuid::new_v4().to_string()),
name: form.0.factor_name, name: form.0.factor_name,
kind: SecondFactorType::TOTP(key), kind: TwoFactorType::TOTP(key),
}); });
let res = users.send(users_actor::UpdateUserRequest(user)).await.unwrap().0; let res = users.send(users_actor::UpdateUserRequest(user)).await.unwrap().0;

View File

@ -9,21 +9,21 @@ pub type UserID = String;
pub struct FactorID(pub String); pub struct FactorID(pub String);
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum SecondFactorType { pub enum TwoFactorType {
TOTP(TotpKey) TOTP(TotpKey)
} }
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct SecondFactor { pub struct TwoFactor {
pub id: FactorID, pub id: FactorID,
pub name: String, pub name: String,
pub kind: SecondFactorType, pub kind: TwoFactorType,
} }
impl SecondFactor { impl TwoFactor {
pub fn type_str(&self) -> &'static str { pub fn type_str(&self) -> &'static str {
match self.kind { match self.kind {
SecondFactorType::TOTP(_) => "Authenticator app" TwoFactorType::TOTP(_) => "Authenticator app"
} }
} }
} }
@ -42,7 +42,7 @@ pub struct User {
/// 2FA /// 2FA
#[serde(default)] #[serde(default)]
pub two_factor: Vec<SecondFactor>, pub two_factor: Vec<TwoFactor>,
/// None = all services /// None = all services
/// Some([]) = no service /// Some([]) = no service
@ -69,7 +69,7 @@ impl User {
!self.two_factor.is_empty() !self.two_factor.is_empty()
} }
pub fn add_factor(&mut self, factor: SecondFactor) { pub fn add_factor(&mut self, factor: TwoFactor) {
self.two_factor.push(factor); self.two_factor.push(factor);
} }