From df71f57d869d981d56697d255ad3e87630d3629e Mon Sep 17 00:00:00 2001 From: vpl Date: Thu, 25 Jul 2019 20:47:58 +0200 Subject: [PATCH] Move send device email to end of password login Send new device email after two factor authentication. --- src/api/identity.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/api/identity.rs b/src/api/identity.rs index b293b7e..0fbe77e 100644 --- a/src/api/identity.rs +++ b/src/api/identity.rs @@ -101,10 +101,14 @@ fn _password_login(data: ConnectData, conn: DbConn, ip: ClientIp) -> JsonResult ) } - let mut device = get_device(&data, &conn, &user, &ip)?; + let (mut device, new_device) = get_device(&data, &conn, &user); let twofactor_token = twofactor_auth(&user.uuid, &data, &mut device, &conn)?; + if CONFIG.mail_enabled() && new_device { + mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &device.updated_at, &device.name)? + } + // Common let user = User::find_by_uuid(&device.user_uuid, &conn).unwrap(); let orgs = UserOrganization::find_by_user(&user.uuid, &conn); @@ -119,7 +123,6 @@ fn _password_login(data: ConnectData, conn: DbConn, ip: ClientIp) -> JsonResult "refresh_token": device.refresh_token, "Key": user.akey, "PrivateKey": user.private_key, - //"TwoFactorToken": "11122233333444555666777888999" }); if let Some(token) = twofactor_token { @@ -130,7 +133,8 @@ fn _password_login(data: ConnectData, conn: DbConn, ip: ClientIp) -> JsonResult Ok(Json(result)) } -fn get_device(data: &ConnectData, conn: &DbConn, user: &User, ip: &ClientIp) -> Result { +/// Retrieves an existing device or creates a new device from ConnectData and the User +fn get_device(data: &ConnectData, conn: &DbConn, user: &User) -> (Device, bool) { // On iOS, device_type sends "iOS", on others it sends a number let device_type = util::try_parse_string(data.device_type.as_ref()).unwrap_or(0); let device_id = data.device_identifier.clone().expect("No device id provided"); @@ -155,11 +159,7 @@ fn get_device(data: &ConnectData, conn: &DbConn, user: &User, ip: &ClientIp) -> } }; - if CONFIG.mail_enabled() && new_device { - mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &device.updated_at, &device.name)? - } - - Ok(device) + (device, new_device) } fn twofactor_auth(