Compare commits

..

No commits in common. "3add7a5d371c966287542ffdb020696c28a65c9a" and "5903ec2e8c48430761662feab543ea64526b2131" have entirely different histories.

5 changed files with 26 additions and 20 deletions

View File

@ -81,9 +81,10 @@ pub async fn users_route(user: CurrentUser, users: web::Data<Addr<UsersActor>>,
user.enabled = update.0.enabled.is_some();
user.admin = update.0.admin.is_some();
if let Some(factors) = user.two_factor.as_mut() {
let factors_to_keep = update.0.two_factor.split(';').collect::<Vec<_>>();
user.two_factor.retain(|f| factors_to_keep.contains(&f.id.0.as_str()));
factors.retain(|f| factors_to_keep.contains(&f.id.0.as_str()));
}
user.authorized_clients = match update.0.grant_type.as_str() {
"all_clients" => None,

View File

@ -6,7 +6,7 @@ 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::{FactorID, TwoFactor, TwoFactorType, User};
use crate::data::user::{FactorID, SecondFactor, SecondFactorType, User};
#[derive(serde::Deserialize)]
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);
user.add_factor(TwoFactor {
user.add_factor(SecondFactor {
id: FactorID(Uuid::new_v4().to_string()),
name: form.0.factor_name,
kind: TwoFactorType::TOTP(key),
kind: SecondFactorType::TOTP(key),
});
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);
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum TwoFactorType {
pub enum SecondFactorType {
TOTP(TotpKey)
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct TwoFactor {
pub struct SecondFactor {
pub id: FactorID,
pub name: String,
pub kind: TwoFactorType,
pub kind: SecondFactorType,
}
impl TwoFactor {
impl SecondFactor {
pub fn type_str(&self) -> &'static str {
match self.kind {
TwoFactorType::TOTP(_) => "Authenticator app"
SecondFactorType::TOTP(_) => "Authenticator app"
}
}
}
@ -41,8 +41,7 @@ pub struct User {
pub admin: bool,
/// 2FA
#[serde(default)]
pub two_factor: Vec<TwoFactor>,
pub two_factor: Option<Vec<SecondFactor>>,
/// None = all services
/// Some([]) = no service
@ -66,15 +65,21 @@ impl User {
}
pub fn has_two_factor(&self) -> bool {
!self.two_factor.is_empty()
self.two_factor.as_ref().map(|f| !f.is_empty()).unwrap_or(false)
}
pub fn add_factor(&mut self, factor: TwoFactor) {
self.two_factor.push(factor);
pub fn add_factor(&mut self, factor: SecondFactor) {
if self.two_factor.is_none() {
self.two_factor = Some(vec![]);
}
self.two_factor.as_mut().unwrap().push(factor);
}
pub fn remove_factor(&mut self, factor_id: FactorID) {
self.two_factor.retain(|f| f.id != factor_id);
if let Some(f) = self.two_factor.as_mut() {
f.retain(|f| f.id != factor_id);
}
}
}
@ -98,7 +103,7 @@ impl Default for User {
need_reset_password: false,
enabled: true,
admin: false,
two_factor: vec![],
two_factor: Some(vec![]),
authorized_clients: Some(Vec::new()),
}
}

View File

@ -74,7 +74,7 @@
<fieldset class="form-group">
<legend class="mt-4">Two factor authentication</legend>
<strong>If you uncheck a factor, it will be DELETED</strong>
{% for f in u.two_factor %}
{% for f in u.two_factor.as_deref().unwrap_or_default() %}
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input two-fact-checkbox"

View File

@ -23,7 +23,7 @@
</tr>
</thead>
<tbody>
{% for f in user.two_factor %}
{% for f in user.two_factor.as_deref().unwrap_or_default() %}
<tr id="factor-{{ f.id.0 }}">
<td>{{ f.type_str() }}</td>
<td>{{ f.name }}</td>