|
|
|
@ -1,4 +1,4 @@ |
|
|
|
use std::collections::HashSet; |
|
|
|
use std::collections::{HashMap, HashSet}; |
|
|
|
|
|
|
|
use chrono::Utc; |
|
|
|
use num_traits::FromPrimitive; |
|
|
|
@ -72,6 +72,7 @@ pub fn routes() -> Vec<rocket::Route> { |
|
|
|
get_device, |
|
|
|
post_device_lost_trust, |
|
|
|
post_device_token, |
|
|
|
post_devices_untrust, |
|
|
|
put_device_token, |
|
|
|
put_clear_device_token, |
|
|
|
post_clear_device_token, |
|
|
|
@ -1241,11 +1242,19 @@ struct CommonUnlockData { |
|
|
|
organization_account_recovery_unlock_data: Vec<UpdateResetPasswordData>, |
|
|
|
#[expect(dead_code, reason = "Required like upstream, but unused")] |
|
|
|
passkey_unlock_data: Vec<Value>, |
|
|
|
#[expect(dead_code, reason = "Required like upstream, but unused")] |
|
|
|
device_key_unlock_data: Vec<Value>, |
|
|
|
#[serde(default)] |
|
|
|
device_key_unlock_data: Vec<UpdateDeviceKeysData>, |
|
|
|
v2_upgrade_token: Option<V2UpgradeTokenData>, |
|
|
|
} |
|
|
|
|
|
|
|
#[derive(Deserialize)] |
|
|
|
#[serde(rename_all = "camelCase")] |
|
|
|
struct UpdateDeviceKeysData { |
|
|
|
device_id: DeviceId, |
|
|
|
encrypted_user_key: String, |
|
|
|
encrypted_public_key: String, |
|
|
|
} |
|
|
|
|
|
|
|
/// Lets other sessions with the v1 user key unwrap the v2 one after an upgrade. Opaque to us. Stored on the user and
|
|
|
|
/// the memberships as this serializes, and read back with it (the account recovery V2 upgrade routes).
|
|
|
|
#[derive(Deserialize, Serialize)] |
|
|
|
@ -1369,6 +1378,7 @@ impl KDFData { |
|
|
|
fn validate_rotation_data( |
|
|
|
data: &RotateData, |
|
|
|
existing_ciphers: &[Cipher], |
|
|
|
existing_devices: &[Device], |
|
|
|
existing_folders: &[Folder], |
|
|
|
existing_emergency_access: &[EmergencyAccess], |
|
|
|
existing_memberships: &[Membership], |
|
|
|
@ -1417,6 +1427,15 @@ fn validate_rotation_data( |
|
|
|
err!("All existing reset password keys must be included in the rotation") |
|
|
|
} |
|
|
|
|
|
|
|
// Check that we're correctly rotating all the user's trusted device
|
|
|
|
let existing_trusted_device_ids = |
|
|
|
existing_devices.iter().filter(|d| d.is_trusted()).map(|c| &c.uuid).collect::<HashSet<&DeviceId>>(); |
|
|
|
let provided_device_ids = |
|
|
|
data.unlock_data.device_key_unlock_data.iter().map(|rp| &rp.device_id).collect::<HashSet<&DeviceId>>(); |
|
|
|
if !provided_device_ids.is_superset(&existing_trusted_device_ids) { |
|
|
|
err!("All existing trusted device keys must be included in the rotation") |
|
|
|
} |
|
|
|
|
|
|
|
// Check that we're correctly rotating all the user's sends
|
|
|
|
let existing_send_ids = existing_sends.iter().map(|s| &s.uuid).collect::<HashSet<&SendId>>(); |
|
|
|
let provided_send_ids = data.account_data.sends.iter().filter_map(|s| s.id.as_ref()).collect::<HashSet<&SendId>>(); |
|
|
|
@ -1637,6 +1656,7 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, conn: DbConn, nt: |
|
|
|
async fn rotate_account(data: RotateData, headers: Headers, conn: DbConn, nt: Notify<'_>) -> EmptyResult { |
|
|
|
let user_id = &headers.user.uuid; |
|
|
|
let mut existing_ciphers = Cipher::find_owned_by_user(user_id, &conn).await; |
|
|
|
let existing_devices = Device::find_by_user(user_id, &conn).await; |
|
|
|
let mut existing_folders = Folder::find_by_user(user_id, &conn).await; |
|
|
|
let mut existing_emergency_access = EmergencyAccess::find_all_confirmed_by_grantor_uuid(user_id, &conn).await; |
|
|
|
// Like upstream, only the ones with a key take part
|
|
|
|
@ -1649,6 +1669,7 @@ async fn rotate_account(data: RotateData, headers: Headers, conn: DbConn, nt: No |
|
|
|
validate_rotation_data( |
|
|
|
&data, |
|
|
|
&existing_ciphers, |
|
|
|
&existing_devices, |
|
|
|
&existing_folders, |
|
|
|
&existing_emergency_access, |
|
|
|
&existing_memberships, |
|
|
|
@ -1747,6 +1768,20 @@ async fn rotate_account(data: RotateData, headers: Headers, conn: DbConn, nt: No |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Update device
|
|
|
|
let mut device_updates: HashMap<DeviceId, UpdateDeviceKeysData> = |
|
|
|
data.unlock_data.device_key_unlock_data.into_iter().map(|d| (d.device_id.clone(), d)).collect(); |
|
|
|
for mut device in existing_devices { |
|
|
|
if let Some(update) = device_updates.remove(&device.uuid) { |
|
|
|
device.encrypted_public_key = Some(update.encrypted_public_key); |
|
|
|
device.encrypted_user_key = Some(update.encrypted_user_key); |
|
|
|
device.save(true, &conn).await?; |
|
|
|
} else if device.is_trusted() { |
|
|
|
device.lost_trust(); |
|
|
|
device.save(true, &conn).await?; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Update send data
|
|
|
|
for send_data in data.account_data.sends { |
|
|
|
if let Some(send) = send_data.id.as_ref().and_then(|id| existing_sends.iter_mut().find(|s| &s.uuid == id)) { |
|
|
|
@ -2393,15 +2428,40 @@ async fn get_all_devices(headers: Headers, conn: DbConn) -> JsonResult { |
|
|
|
} |
|
|
|
|
|
|
|
#[post("/devices/lost-trust")] |
|
|
|
async fn post_device_lost_trust(headers: Headers, conn: DbConn) -> JsonResult { |
|
|
|
async fn post_device_lost_trust(headers: Headers, conn: DbConn) -> EmptyResult { |
|
|
|
let mut device = headers.device; |
|
|
|
|
|
|
|
device.encrypted_user_key = None; |
|
|
|
device.encrypted_public_key = None; |
|
|
|
device.encrypted_private_key = None; |
|
|
|
device.save(true, &conn).await?; |
|
|
|
if device.lost_trust() { |
|
|
|
device.save(true, &conn).await?; |
|
|
|
} |
|
|
|
|
|
|
|
Ok(Json(json!({}))) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
#[derive(Deserialize)] |
|
|
|
#[serde(rename_all = "camelCase")] |
|
|
|
struct DevicesUntrusted { |
|
|
|
devices: Vec<DeviceId>, |
|
|
|
} |
|
|
|
|
|
|
|
#[post("/devices/untrust", data = "<data>")] |
|
|
|
async fn post_devices_untrust(headers: Headers, data: Json<DevicesUntrusted>, conn: DbConn) -> EmptyResult { |
|
|
|
let untrusted = data.into_inner(); |
|
|
|
if !untrusted.devices.is_empty() { |
|
|
|
for device_id in untrusted.devices { |
|
|
|
warn!("Cannot find user {} device {} to untrust", device_id, headers.user.email); |
|
|
|
|
|
|
|
if let Some(mut device) = Device::find_by_uuid_and_user(&device_id, &headers.user.uuid, &conn).await { |
|
|
|
if device.lost_trust() { |
|
|
|
device.save(true, &conn).await?; |
|
|
|
} |
|
|
|
} else { |
|
|
|
warn!("Cannot find user {} device {} to untrust", device_id, headers.user.email); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
#[get("/devices/identifier/<device_id>")] |
|
|
|
|