Skip to content

Commit

Permalink
Actually use Device Type for mails
Browse files Browse the repository at this point in the history
- match Bitwarden behaviour
- remove upcase_first, no longer needed and it would also interfere with device types like iOS, macOS
  • Loading branch information
dfunkt committed Sep 3, 2024
1 parent 248e561 commit 261ae78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
27 changes: 24 additions & 3 deletions src/api/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,14 @@ async fn _password_login(
let twofactor_token = twofactor_auth(&user, &data, &mut device, ip, conn).await?;

if CONFIG.mail_enabled() && new_device {
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &now, &device.name).await {
if let Err(e) = mail::send_new_device_logged_in(
&user.email,
&ip.ip.to_string(),
&now,
&DeviceType::from_i32(device.atype).to_string(),
)
.await
{
error!("Error sending new device email: {:#?}", e);

if CONFIG.require_device_email() {
Expand Down Expand Up @@ -421,7 +428,14 @@ async fn _user_api_key_login(

if CONFIG.mail_enabled() && new_device {
let now = Utc::now().naive_utc();
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &now, &device.name).await {
if let Err(e) = mail::send_new_device_logged_in(
&user.email,
&ip.ip.to_string(),
&now,
&DeviceType::from_i32(device.atype).to_string(),
)
.await
{
error!("Error sending new device email: {:#?}", e);

if CONFIG.require_device_email() {
Expand Down Expand Up @@ -535,7 +549,14 @@ async fn twofactor_auth(
return Ok(None);
}

TwoFactorIncomplete::mark_incomplete(&user.uuid, &device.uuid, &device.name, ip, conn).await?;
TwoFactorIncomplete::mark_incomplete(
&user.uuid,
&device.uuid,
&DeviceType::from_i32(device.atype).to_string(),
ip,
conn,
)
.await?;

let twofactor_ids: Vec<_> = twofactors.iter().map(|tf| tf.atype).collect();
let selected_id = data.two_factor_provider.unwrap_or(twofactor_ids[0]); // If we aren't given a two factor provider, assume the first one
Expand Down
6 changes: 0 additions & 6 deletions src/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,6 @@ pub async fn send_invite_confirmed(address: &str, org_name: &str) -> EmptyResult
}

pub async fn send_new_device_logged_in(address: &str, ip: &str, dt: &NaiveDateTime, device: &str) -> EmptyResult {
use crate::util::upcase_first;
let device = upcase_first(device);

let fmt = "%A, %B %_d, %Y at %r %Z";
let (subject, body_html, body_text) = get_text(
"email/new_device_logged_in",
Expand All @@ -466,9 +463,6 @@ pub async fn send_new_device_logged_in(address: &str, ip: &str, dt: &NaiveDateTi
}

pub async fn send_incomplete_2fa_login(address: &str, ip: &str, dt: &NaiveDateTime, device: &str) -> EmptyResult {
use crate::util::upcase_first;
let device = upcase_first(device);

let fmt = "%A, %B %_d, %Y at %r %Z";
let (subject, body_html, body_text) = get_text(
"email/incomplete_2fa_login",
Expand Down
9 changes: 0 additions & 9 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,6 @@ pub fn get_uuid() -> String {

use std::str::FromStr;

#[inline]
pub fn upcase_first(s: &str) -> String {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}

#[inline]
pub fn lcase_first(s: &str) -> String {
let mut c = s.chars();
Expand Down

0 comments on commit 261ae78

Please sign in to comment.