From aad1f19b45073d7f670a8310689c3245940d667a Mon Sep 17 00:00:00 2001 From: Stefan Melmuk <509385+stefan0xC@users.noreply.github.com> Date: Sun, 23 Nov 2025 21:55:20 +0100 Subject: [PATCH] fix email as 2fa provider (#6473) --- src/api/core/two_factor/email.rs | 21 ++++++++++----------- src/db/models/user.rs | 14 +------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/api/core/two_factor/email.rs b/src/api/core/two_factor/email.rs index 63e4508b..cc6909af 100644 --- a/src/api/core/two_factor/email.rs +++ b/src/api/core/two_factor/email.rs @@ -10,7 +10,7 @@ use crate::{ auth::Headers, crypto, db::{ - models::{DeviceId, EventType, TwoFactor, TwoFactorType, User, UserId}, + models::{EventType, TwoFactor, TwoFactorType, User, UserId}, DbConn, }, error::{Error, MapResult}, @@ -24,16 +24,10 @@ pub fn routes() -> Vec { #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct SendEmailLoginData { - #[serde(alias = "DeviceIdentifier")] - device_identifier: DeviceId, - - #[allow(unused)] #[serde(alias = "Email")] - email: Option, - - #[allow(unused)] + email: String, #[serde(alias = "MasterPasswordHash")] - master_password_hash: Option, + master_password_hash: String, } /// User is trying to login and wants to use email 2FA. @@ -45,14 +39,19 @@ async fn send_email_login(data: Json, conn: DbConn) -> Empty use crate::db::models::User; // Get the user - let Some(user) = User::find_by_device_id(&data.device_identifier, &conn).await else { - err!("Cannot find user. Try again.") + let Some(user) = User::find_by_mail(&data.email, &conn).await else { + err!("Username or password is incorrect. Try again.") }; if !CONFIG._enable_email_2fa() { err!("Email 2FA is disabled") } + // Check password + if !user.check_valid_password(&data.master_password_hash) { + err!("Username or password is incorrect. Try again.") + } + send_token(&user.uuid, &conn).await?; Ok(()) diff --git a/src/db/models/user.rs b/src/db/models/user.rs index e14c4218..c7f4e1bc 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -1,4 +1,4 @@ -use crate::db::schema::{devices, invitations, sso_users, users}; +use crate::db::schema::{invitations, sso_users, users}; use chrono::{NaiveDateTime, TimeDelta, Utc}; use derive_more::{AsRef, Deref, Display, From}; use diesel::prelude::*; @@ -10,7 +10,6 @@ use super::{ use crate::{ api::EmptyResult, crypto, - db::models::DeviceId, db::DbConn, error::MapResult, sso::OIDCIdentifier, @@ -387,17 +386,6 @@ impl User { }} } - pub async fn find_by_device_id(device_uuid: &DeviceId, conn: &DbConn) -> Option { - db_run! { conn: { - users::table - .inner_join(devices::table.on(devices::user_uuid.eq(users::uuid))) - .filter(devices::uuid.eq(device_uuid)) - .select(users::all_columns) - .first::(conn) - .ok() - }} - } - pub async fn get_all(conn: &DbConn) -> Vec<(Self, Option)> { db_run! { conn: { users::table