Changeset View
Changeset View
Standalone View
Standalone View
services/identity/src/database.rs
Show First 20 Lines • Show All 237 Lines • ▼ Show 20 Lines | self | ||||
.set_item(Some(user)) | .set_item(Some(user)) | ||||
.send() | .send() | ||||
.await | .await | ||||
.map_err(|e| Error::AwsSdk(e.into()))?; | .map_err(|e| Error::AwsSdk(e.into()))?; | ||||
self | self | ||||
.append_one_time_prekeys( | .append_one_time_prekeys( | ||||
flattened_device_key_upload.device_id_key, | flattened_device_key_upload.device_id_key, | ||||
flattened_device_key_upload.content_onetime_keys, | flattened_device_key_upload.content_one_time_keys, | ||||
flattened_device_key_upload.notif_onetime_keys, | flattened_device_key_upload.notif_one_time_keys, | ||||
) | ) | ||||
.await?; | .await?; | ||||
Ok(user_id) | Ok(user_id) | ||||
} | } | ||||
pub async fn add_password_user_device_to_users_table( | pub async fn add_password_user_device_to_users_table( | ||||
&self, | &self, | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | ) -> Result<Option<OutboundKeys>, Error> { | ||||
// Assert that the user has a keyserver, if they don't return None | // Assert that the user has a keyserver, if they don't return None | ||||
let keyserver_id = match maybe_keyserver_id { | let keyserver_id = match maybe_keyserver_id { | ||||
None => return Ok(None), | None => return Ok(None), | ||||
Some(id) => id, | Some(id) => id, | ||||
}; | }; | ||||
let keyserver = devices.get_map(keyserver_id)?; | let keyserver = devices.get_map(keyserver_id)?; | ||||
let notif_one_time_key: Option<String> = self | let notif_one_time_key: Option<String> = self | ||||
.get_onetime_key(keyserver_id, OlmAccountType::Notification) | .get_one_time_key(keyserver_id, OlmAccountType::Notification) | ||||
.await?; | .await?; | ||||
let content_one_time_key: Option<String> = self | let content_one_time_key: Option<String> = self | ||||
.get_onetime_key(keyserver_id, OlmAccountType::Content) | .get_one_time_key(keyserver_id, OlmAccountType::Content) | ||||
.await?; | .await?; | ||||
debug!( | debug!( | ||||
"Able to get notif key for keyserver {}: {}", | "Able to get notif key for keyserver {}: {}", | ||||
keyserver_id, | keyserver_id, | ||||
notif_one_time_key.is_some() | notif_one_time_key.is_some() | ||||
); | ); | ||||
debug!( | debug!( | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | let outbound_payload = OutboundKeys { | ||||
notif_prekey: full_notif_prekey, | notif_prekey: full_notif_prekey, | ||||
content_one_time_key, | content_one_time_key, | ||||
notif_one_time_key, | notif_one_time_key, | ||||
}; | }; | ||||
return Ok(Some(outbound_payload)); | return Ok(Some(outbound_payload)); | ||||
} | } | ||||
/// Will "mint" a single onetime key by attempting to successfully deleting | /// Will "mint" a single one time key by attempting to successfully deleting | ||||
/// a key | /// a key | ||||
pub async fn get_onetime_key( | pub async fn get_one_time_key( | ||||
&self, | &self, | ||||
device_id: &str, | device_id: &str, | ||||
account_type: OlmAccountType, | account_type: OlmAccountType, | ||||
) -> Result<Option<String>, Error> { | ) -> Result<Option<String>, Error> { | ||||
use crate::constants::one_time_keys_table as otk_table; | use crate::constants::one_time_keys_table as otk_table; | ||||
use crate::constants::ONETIME_KEY_MINIMUM_THRESHOLD; | use crate::constants::ONE_TIME_KEY_MINIMUM_THRESHOLD; | ||||
let query_result = self.get_onetime_keys(device_id, account_type).await?; | let query_result = self.get_one_time_keys(device_id, account_type).await?; | ||||
let items = query_result.items(); | let items = query_result.items(); | ||||
// If no onetime keys exists, return none early | // If no one time keys exists, return none early | ||||
let Some(item_vec) = items else { | let Some(item_vec) = items else { | ||||
debug!("Unable to find {:?} onetime-key", account_type); | debug!("Unable to find {:?} one time key", account_type); | ||||
return Ok(None); | return Ok(None); | ||||
}; | }; | ||||
if item_vec.len() < ONETIME_KEY_MINIMUM_THRESHOLD { | if item_vec.len() < ONE_TIME_KEY_MINIMUM_THRESHOLD { | ||||
// Avoid device_id being moved out-of-scope by "move" | // Avoid device_id being moved out-of-scope by "move" | ||||
let device_id = device_id.to_string(); | let device_id = device_id.to_string(); | ||||
tokio::spawn(async move { | tokio::spawn(async move { | ||||
debug!("Attempting to request more keys for device: {}", &device_id); | debug!("Attempting to request more keys for device: {}", &device_id); | ||||
let result = | let result = | ||||
crate::tunnelbroker::send_refresh_keys_request(&device_id).await; | crate::tunnelbroker::send_refresh_keys_request(&device_id).await; | ||||
consume_error(result); | consume_error(result); | ||||
}); | }); | ||||
} | } | ||||
let mut result = None; | let mut result = None; | ||||
// Attempt to delete the onetime keys individually, a successful delete | // Attempt to delete the one time keys individually, a successful delete | ||||
// mints the onetime key to the requester | // mints the one time key to the requester | ||||
for item in item_vec { | for item in item_vec { | ||||
let pk = item.get_string(otk_table::PARTITION_KEY)?; | let pk = item.get_string(otk_table::PARTITION_KEY)?; | ||||
let otk = item.get_string(otk_table::SORT_KEY)?; | let otk = item.get_string(otk_table::SORT_KEY)?; | ||||
let composite_key = HashMap::from([ | let composite_key = HashMap::from([ | ||||
( | ( | ||||
otk_table::PARTITION_KEY.to_string(), | otk_table::PARTITION_KEY.to_string(), | ||||
AttributeValue::S(pk.to_string()), | AttributeValue::S(pk.to_string()), | ||||
), | ), | ||||
( | ( | ||||
otk_table::SORT_KEY.to_string(), | otk_table::SORT_KEY.to_string(), | ||||
AttributeValue::S(otk.to_string()), | AttributeValue::S(otk.to_string()), | ||||
), | ), | ||||
]); | ]); | ||||
debug!("Attempting to delete a {:?} onetime-key", account_type); | debug!("Attempting to delete a {:?} one time key", account_type); | ||||
match self | match self | ||||
.client | .client | ||||
.delete_item() | .delete_item() | ||||
.set_key(Some(composite_key)) | .set_key(Some(composite_key)) | ||||
.table_name(otk_table::NAME) | .table_name(otk_table::NAME) | ||||
.send() | .send() | ||||
.await | .await | ||||
{ | { | ||||
Show All 9 Lines | for item in item_vec { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// Return deleted key | // Return deleted key | ||||
Ok(result) | Ok(result) | ||||
} | } | ||||
pub async fn get_onetime_keys( | pub async fn get_one_time_keys( | ||||
&self, | &self, | ||||
device_id: &str, | device_id: &str, | ||||
account_type: OlmAccountType, | account_type: OlmAccountType, | ||||
) -> Result<QueryOutput, Error> { | ) -> Result<QueryOutput, Error> { | ||||
use crate::constants::one_time_keys_table::*; | use crate::constants::one_time_keys_table::*; | ||||
// Add related prefix to partition key to grab the correct result set | // Add related prefix to partition key to grab the correct result set | ||||
let partition_key = | let partition_key = | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | async fn add_device_to_users_table( | ||||
&self, | &self, | ||||
user_id: String, | user_id: String, | ||||
flattened_device_key_upload: FlattenedDeviceKeyUpload, | flattened_device_key_upload: FlattenedDeviceKeyUpload, | ||||
social_proof: Option<String>, | social_proof: Option<String>, | ||||
) -> Result<(), Error> { | ) -> Result<(), Error> { | ||||
// Avoid borrowing from lifetime of flattened_device_key_upload | // Avoid borrowing from lifetime of flattened_device_key_upload | ||||
let device_id = flattened_device_key_upload.device_id_key.clone(); | let device_id = flattened_device_key_upload.device_id_key.clone(); | ||||
let content_one_time_keys = | let content_one_time_keys = | ||||
flattened_device_key_upload.content_onetime_keys.clone(); | flattened_device_key_upload.content_one_time_keys.clone(); | ||||
let notif_one_time_keys = | let notif_one_time_keys = | ||||
flattened_device_key_upload.notif_onetime_keys.clone(); | flattened_device_key_upload.notif_one_time_keys.clone(); | ||||
let device_info = | let device_info = | ||||
create_device_info(flattened_device_key_upload, social_proof); | create_device_info(flattened_device_key_upload, social_proof); | ||||
let update_expression = | let update_expression = | ||||
format!("SET {}.#{} = :v", USERS_TABLE_DEVICES_ATTRIBUTE, "deviceID",); | format!("SET {}.#{} = :v", USERS_TABLE_DEVICES_ATTRIBUTE, "deviceID",); | ||||
let expression_attribute_names = | let expression_attribute_names = | ||||
HashMap::from([(format!("#{}", "deviceID"), device_id.clone())]); | HashMap::from([(format!("#{}", "deviceID"), device_id.clone())]); | ||||
let expression_attribute_values = | let expression_attribute_values = | ||||
▲ Show 20 Lines • Show All 890 Lines • Show Last 20 Lines |