|
|
@ -1,4 +1,4 @@ |
|
|
use chrono::{NaiveDateTime, Utc}; |
|
|
use chrono::Utc; |
|
|
use num_traits::FromPrimitive; |
|
|
use num_traits::FromPrimitive; |
|
|
use rocket::{ |
|
|
use rocket::{ |
|
|
form::{Form, FromForm}, |
|
|
form::{Form, FromForm}, |
|
|
@ -147,7 +147,7 @@ async fn _refresh_login(data: ConnectData, conn: &DbConn, ip: &ClientIp) -> Json |
|
|
} |
|
|
} |
|
|
Ok((mut device, auth_tokens)) => { |
|
|
Ok((mut device, auth_tokens)) => { |
|
|
// Save to update `device.updated_at` to track usage and toggle new status
|
|
|
// Save to update `device.updated_at` to track usage and toggle new status
|
|
|
device.save(conn).await?; |
|
|
device.save(true, conn).await?; |
|
|
|
|
|
|
|
|
let result = json!({ |
|
|
let result = json!({ |
|
|
"refresh_token": auth_tokens.refresh_token(), |
|
|
"refresh_token": auth_tokens.refresh_token(), |
|
|
@ -267,6 +267,7 @@ async fn _sso_login( |
|
|
} |
|
|
} |
|
|
Some((mut user, sso_user)) => { |
|
|
Some((mut user, sso_user)) => { |
|
|
let mut device = get_device(&data, conn, &user).await?; |
|
|
let mut device = get_device(&data, conn, &user).await?; |
|
|
|
|
|
|
|
|
let twofactor_token = twofactor_auth(&mut user, &data, &mut device, ip, client_version, conn).await?; |
|
|
let twofactor_token = twofactor_auth(&mut user, &data, &mut device, ip, client_version, conn).await?; |
|
|
|
|
|
|
|
|
if user.private_key.is_none() { |
|
|
if user.private_key.is_none() { |
|
|
@ -313,7 +314,7 @@ async fn _sso_login( |
|
|
auth_user.expires_in, |
|
|
auth_user.expires_in, |
|
|
)?; |
|
|
)?; |
|
|
|
|
|
|
|
|
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, &now, conn, ip).await |
|
|
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, conn, ip).await |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async fn _password_login( |
|
|
async fn _password_login( |
|
|
@ -435,7 +436,7 @@ async fn _password_login( |
|
|
|
|
|
|
|
|
let auth_tokens = auth::AuthTokens::new(&device, &user, AuthMethod::Password, data.client_id); |
|
|
let auth_tokens = auth::AuthTokens::new(&device, &user, AuthMethod::Password, data.client_id); |
|
|
|
|
|
|
|
|
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, &now, conn, ip).await |
|
|
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, conn, ip).await |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async fn authenticated_response( |
|
|
async fn authenticated_response( |
|
|
@ -443,12 +444,12 @@ async fn authenticated_response( |
|
|
device: &mut Device, |
|
|
device: &mut Device, |
|
|
auth_tokens: auth::AuthTokens, |
|
|
auth_tokens: auth::AuthTokens, |
|
|
twofactor_token: Option<String>, |
|
|
twofactor_token: Option<String>, |
|
|
now: &NaiveDateTime, |
|
|
|
|
|
conn: &DbConn, |
|
|
conn: &DbConn, |
|
|
ip: &ClientIp, |
|
|
ip: &ClientIp, |
|
|
) -> JsonResult { |
|
|
) -> JsonResult { |
|
|
if CONFIG.mail_enabled() && device.is_new() { |
|
|
if CONFIG.mail_enabled() && device.is_new() { |
|
|
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), now, device).await { |
|
|
let now = Utc::now().naive_utc(); |
|
|
|
|
|
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &now, device).await { |
|
|
error!("Error sending new device email: {e:#?}"); |
|
|
error!("Error sending new device email: {e:#?}"); |
|
|
|
|
|
|
|
|
if CONFIG.require_device_email() { |
|
|
if CONFIG.require_device_email() { |
|
|
@ -468,7 +469,7 @@ async fn authenticated_response( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Save to update `device.updated_at` to track usage and toggle new status
|
|
|
// Save to update `device.updated_at` to track usage and toggle new status
|
|
|
device.save(conn).await?; |
|
|
device.save(true, conn).await?; |
|
|
|
|
|
|
|
|
let master_password_policy = master_password_policy(user, conn).await; |
|
|
let master_password_policy = master_password_policy(user, conn).await; |
|
|
|
|
|
|
|
|
@ -585,7 +586,7 @@ async fn _user_api_key_login( |
|
|
let access_claims = auth::LoginJwtClaims::default(&device, &user, &AuthMethod::UserApiKey, data.client_id); |
|
|
let access_claims = auth::LoginJwtClaims::default(&device, &user, &AuthMethod::UserApiKey, data.client_id); |
|
|
|
|
|
|
|
|
// Save to update `device.updated_at` to track usage and toggle new status
|
|
|
// Save to update `device.updated_at` to track usage and toggle new status
|
|
|
device.save(conn).await?; |
|
|
device.save(true, conn).await?; |
|
|
|
|
|
|
|
|
info!("User {} logged in successfully via API key. IP: {}", user.email, ip.ip); |
|
|
info!("User {} logged in successfully via API key. IP: {}", user.email, ip.ip); |
|
|
|
|
|
|
|
|
@ -648,7 +649,12 @@ async fn get_device(data: &ConnectData, conn: &DbConn, user: &User) -> ApiResult |
|
|
// Find device or create new
|
|
|
// Find device or create new
|
|
|
match Device::find_by_uuid_and_user(&device_id, &user.uuid, conn).await { |
|
|
match Device::find_by_uuid_and_user(&device_id, &user.uuid, conn).await { |
|
|
Some(device) => Ok(device), |
|
|
Some(device) => Ok(device), |
|
|
None => Device::new(device_id, user.uuid.clone(), device_name, device_type, conn).await, |
|
|
None => { |
|
|
|
|
|
let mut device = Device::new(device_id, user.uuid.clone(), device_name, device_type); |
|
|
|
|
|
// save device without updating `device.updated_at`
|
|
|
|
|
|
device.save(false, conn).await?; |
|
|
|
|
|
Ok(device) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|