diff --git a/services/identity/src/database.rs b/services/identity/src/database.rs --- a/services/identity/src/database.rs +++ b/services/identity/src/database.rs @@ -1,5 +1,13 @@ +use std::collections::HashMap; + +use opaque_ke::ServerRegistration; use rusoto_core::Region; -use rusoto_dynamodb::DynamoDbClient; +use rusoto_dynamodb::{ + AttributeValue, DynamoDb, DynamoDbClient, GetItemInput, GetItemOutput, +}; +use tracing::error; + +use crate::opaque::Cipher; pub struct DatabaseClient { client: DynamoDbClient, @@ -11,6 +19,67 @@ client: DynamoDbClient::new(region), } } + + pub async fn get_pake_registration( + &self, + user_id: String, + ) -> Option> { + let partition_key = HashMap::from([( + "userID".to_string(), + AttributeValue { + s: Some(user_id.clone()), + ..Default::default() + }, + )]); + let get_item_input = GetItemInput { + table_name: "identity-pake-registration".to_string(), + key: partition_key, + consistent_read: Some(true), + ..GetItemInput::default() + }; + let get_item_result = self.client.get_item(get_item_input).await; + match get_item_result { + Ok(GetItemOutput { + item: Some(item), .. + }) => { + if let Some(AttributeValue { + b: Some(server_registration_bytes), + .. + }) = item.get("pakeRegistrationData") + { + match ServerRegistration::::deserialize( + server_registration_bytes, + ) { + Ok(server_registration) => Some(server_registration), + Err(e) => { + error!( + "Failed to deserialize ServerRegistration struct for user {}: {}", + user_id, e + ); + None + } + } + } else { + error!("No registration data found for registered user {}", user_id); + None + } + } + Ok(_) => { + error!( + "No item found for user {} in PAKE registration table", + user_id + ); + None + } + Err(e) => { + error!( + "DynamoDB client failed to get registration data for user {}: {}", + user_id, e + ); + None + } + } + } } impl Default for DatabaseClient {