diff --git a/native/native_rust_library/src/identity.rs b/native/native_rust_library/src/identity.rs --- a/native/native_rust_library/src/identity.rs +++ b/native/native_rust_library/src/identity.rs @@ -8,6 +8,7 @@ use crate::{Error, RUNTIME}; use crate::{CODE_VERSION, DEVICE_TYPE, IDENTITY_SOCKET_ADDR}; +pub mod account_actions; pub mod exact_user_search; pub mod farcaster; pub mod login; @@ -17,6 +18,7 @@ pub mod ffi { use super::*; + pub use account_actions::ffi::*; pub use exact_user_search::ffi::*; pub use farcaster::ffi::*; pub use login::ffi::*; diff --git a/native/native_rust_library/src/identity/account_actions.rs b/native/native_rust_library/src/identity/account_actions.rs new file mode 100644 --- /dev/null +++ b/native/native_rust_library/src/identity/account_actions.rs @@ -0,0 +1,149 @@ +use comm_opaque2::client::Registration; +use grpc_clients::identity::get_auth_client; +use grpc_clients::identity::protos::auth::{ + UpdateUserPasswordFinishRequest, UpdateUserPasswordStartRequest, +}; +use grpc_clients::identity::protos::unauth::Empty; + +use crate::identity::AuthInfo; +use crate::utils::jsi_callbacks::handle_void_result_as_callback; +use crate::{Error, CODE_VERSION, DEVICE_TYPE, IDENTITY_SOCKET_ADDR, RUNTIME}; + +pub mod ffi { + use super::*; + + pub fn update_user_password( + user_id: String, + device_id: String, + access_token: String, + password: String, + promise_id: u32, + ) { + RUNTIME.spawn(async move { + let update_password_info = UpdatePasswordInfo { + access_token, + user_id, + device_id, + password, + }; + let result = update_user_password_helper(update_password_info).await; + handle_void_result_as_callback(result, promise_id); + }); + } + + pub fn delete_user( + user_id: String, + device_id: String, + access_token: String, + promise_id: u32, + ) { + RUNTIME.spawn(async move { + let auth_info = AuthInfo { + access_token, + user_id, + device_id, + }; + let result = delete_user_helper(auth_info).await; + handle_void_result_as_callback(result, promise_id); + }); + } + + pub fn log_out( + user_id: String, + device_id: String, + access_token: String, + promise_id: u32, + ) { + RUNTIME.spawn(async move { + let auth_info = AuthInfo { + access_token, + user_id, + device_id, + }; + let result = log_out_helper(auth_info).await; + handle_void_result_as_callback(result, promise_id); + }); + } +} + +struct UpdatePasswordInfo { + user_id: String, + device_id: String, + access_token: String, + password: String, +} + +async fn update_user_password_helper( + update_password_info: UpdatePasswordInfo, +) -> Result<(), Error> { + let mut client_registration = Registration::new(); + let opaque_registration_request = client_registration + .start(&update_password_info.password) + .map_err(crate::handle_error)?; + let update_password_start_request = UpdateUserPasswordStartRequest { + opaque_registration_request, + }; + let mut identity_client = get_auth_client( + IDENTITY_SOCKET_ADDR, + update_password_info.user_id, + update_password_info.device_id, + update_password_info.access_token, + CODE_VERSION, + DEVICE_TYPE.as_str_name().to_lowercase(), + ) + .await?; + + let response = identity_client + .update_user_password_start(update_password_start_request) + .await?; + + let update_password_start_response = response.into_inner(); + + let opaque_registration_upload = client_registration + .finish( + &update_password_info.password, + &update_password_start_response.opaque_registration_response, + ) + .map_err(crate::handle_error)?; + + let update_password_finish_request = UpdateUserPasswordFinishRequest { + session_id: update_password_start_response.session_id, + opaque_registration_upload, + }; + + identity_client + .update_user_password_finish(update_password_finish_request) + .await?; + + Ok(()) +} + +async fn delete_user_helper(auth_info: AuthInfo) -> Result<(), Error> { + let mut identity_client = get_auth_client( + IDENTITY_SOCKET_ADDR, + auth_info.user_id, + auth_info.device_id, + auth_info.access_token, + CODE_VERSION, + DEVICE_TYPE.as_str_name().to_lowercase(), + ) + .await?; + identity_client.delete_user(Empty {}).await?; + + Ok(()) +} + +async fn log_out_helper(auth_info: AuthInfo) -> Result<(), Error> { + let mut identity_client = get_auth_client( + IDENTITY_SOCKET_ADDR, + auth_info.user_id, + auth_info.device_id, + auth_info.access_token, + CODE_VERSION, + DEVICE_TYPE.as_str_name().to_lowercase(), + ) + .await?; + identity_client.log_out_user(Empty {}).await?; + + Ok(()) +} diff --git a/native/native_rust_library/src/lib.rs b/native/native_rust_library/src/lib.rs --- a/native/native_rust_library/src/lib.rs +++ b/native/native_rust_library/src/lib.rs @@ -1,14 +1,10 @@ -use comm_opaque2::client::Registration; use comm_opaque2::grpc::opaque_error_to_grpc_status as handle_error; use grpc_clients::identity::protos::auth::{ GetDeviceListRequest, UpdateDeviceListRequest, }; -use grpc_clients::identity::protos::authenticated::{ - UpdateUserPasswordFinishRequest, UpdateUserPasswordStartRequest, -}; use grpc_clients::identity::protos::unauth::{ - DeviceKeyUpload, DeviceType, Empty, ExistingDeviceLoginRequest, - IdentityKeyInfo, Prekey, SecondaryDeviceKeysUploadRequest, + DeviceKeyUpload, DeviceType, ExistingDeviceLoginRequest, IdentityKeyInfo, + Prekey, SecondaryDeviceKeysUploadRequest, }; use grpc_clients::identity::{get_auth_client, get_unauthenticated_client}; use lazy_static::lazy_static; @@ -425,140 +421,6 @@ } } -struct UpdatePasswordInfo { - user_id: String, - device_id: String, - access_token: String, - password: String, -} - -fn update_user_password( - user_id: String, - device_id: String, - access_token: String, - password: String, - promise_id: u32, -) { - RUNTIME.spawn(async move { - let update_password_info = UpdatePasswordInfo { - access_token, - user_id, - device_id, - password, - }; - let result = update_user_password_helper(update_password_info).await; - handle_void_result_as_callback(result, promise_id); - }); -} - -async fn update_user_password_helper( - update_password_info: UpdatePasswordInfo, -) -> Result<(), Error> { - let mut client_registration = Registration::new(); - let opaque_registration_request = client_registration - .start(&update_password_info.password) - .map_err(handle_error)?; - let update_password_start_request = UpdateUserPasswordStartRequest { - opaque_registration_request, - }; - let mut identity_client = get_auth_client( - IDENTITY_SOCKET_ADDR, - update_password_info.user_id, - update_password_info.device_id, - update_password_info.access_token, - CODE_VERSION, - DEVICE_TYPE.as_str_name().to_lowercase(), - ) - .await?; - - let response = identity_client - .update_user_password_start(update_password_start_request) - .await?; - - let update_password_start_response = response.into_inner(); - - let opaque_registration_upload = client_registration - .finish( - &update_password_info.password, - &update_password_start_response.opaque_registration_response, - ) - .map_err(handle_error)?; - - let update_password_finish_request = UpdateUserPasswordFinishRequest { - session_id: update_password_start_response.session_id, - opaque_registration_upload, - }; - - identity_client - .update_user_password_finish(update_password_finish_request) - .await?; - - Ok(()) -} - -fn delete_user( - user_id: String, - device_id: String, - access_token: String, - promise_id: u32, -) { - RUNTIME.spawn(async move { - let auth_info = AuthInfo { - access_token, - user_id, - device_id, - }; - let result = delete_user_helper(auth_info).await; - handle_void_result_as_callback(result, promise_id); - }); -} - -async fn delete_user_helper(auth_info: AuthInfo) -> Result<(), Error> { - let mut identity_client = get_auth_client( - IDENTITY_SOCKET_ADDR, - auth_info.user_id, - auth_info.device_id, - auth_info.access_token, - CODE_VERSION, - DEVICE_TYPE.as_str_name().to_lowercase(), - ) - .await?; - identity_client.delete_user(Empty {}).await?; - - Ok(()) -} - -fn log_out( - user_id: String, - device_id: String, - access_token: String, - promise_id: u32, -) { - RUNTIME.spawn(async move { - let auth_info = AuthInfo { - access_token, - user_id, - device_id, - }; - let result = log_out_helper(auth_info).await; - handle_void_result_as_callback(result, promise_id); - }); -} - -async fn log_out_helper(auth_info: AuthInfo) -> Result<(), Error> { - let mut identity_client = get_auth_client( - IDENTITY_SOCKET_ADDR, - auth_info.user_id, - auth_info.device_id, - auth_info.access_token, - CODE_VERSION, - DEVICE_TYPE.as_str_name().to_lowercase(), - ) - .await?; - identity_client.log_out_user(Empty {}).await?; - - Ok(()) -} fn get_device_list_for_user( auth_user_id: String, auth_device_id: String,