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 @@ -10,6 +10,7 @@ pub mod exact_user_search; pub mod farcaster; +pub mod login; pub mod registration; pub mod ffi { @@ -17,6 +18,7 @@ pub use exact_user_search::ffi::*; pub use farcaster::ffi::*; + pub use login::ffi::*; pub use registration::ffi::*; pub fn generate_nonce(promise_id: u32) { diff --git a/native/native_rust_library/src/identity/login.rs b/native/native_rust_library/src/identity/login.rs new file mode 100644 --- /dev/null +++ b/native/native_rust_library/src/identity/login.rs @@ -0,0 +1,185 @@ +use comm_opaque2::client::Login; +use grpc_clients::identity::{ + get_unauthenticated_client, + protos::unauth::{ + DeviceKeyUpload, IdentityKeyInfo, OpaqueLoginFinishRequest, + OpaqueLoginStartRequest, Prekey, WalletAuthRequest, + }, +}; +use tracing::instrument; + +use super::{PasswordUserInfo, UserIDAndDeviceAccessToken, WalletUserInfo}; +use crate::utils::jsi_callbacks::handle_string_result_as_callback; +use crate::{Error, CODE_VERSION, DEVICE_TYPE, IDENTITY_SOCKET_ADDR, RUNTIME}; + +pub mod ffi { + use super::*; + + #[instrument] + pub fn log_in_password_user( + username: String, + password: String, + key_payload: String, + key_payload_signature: String, + content_prekey: String, + content_prekey_signature: String, + notif_prekey: String, + notif_prekey_signature: String, + promise_id: u32, + ) { + RUNTIME.spawn(async move { + let password_user_info = PasswordUserInfo { + username, + password, + key_payload, + key_payload_signature, + content_prekey, + content_prekey_signature, + notif_prekey, + notif_prekey_signature, + content_one_time_keys: Vec::new(), + notif_one_time_keys: Vec::new(), + farcaster_id: None, + }; + let result = log_in_password_user_helper(password_user_info).await; + handle_string_result_as_callback(result, promise_id); + }); + } + + #[instrument] + pub fn log_in_wallet_user( + siwe_message: String, + siwe_signature: String, + key_payload: String, + key_payload_signature: String, + content_prekey: String, + content_prekey_signature: String, + notif_prekey: String, + notif_prekey_signature: String, + promise_id: u32, + ) { + RUNTIME.spawn(async move { + let wallet_user_info = WalletUserInfo { + siwe_message, + siwe_signature, + key_payload, + key_payload_signature, + content_prekey, + content_prekey_signature, + notif_prekey, + notif_prekey_signature, + content_one_time_keys: Vec::new(), + notif_one_time_keys: Vec::new(), + farcaster_id: None, + }; + let result = log_in_wallet_user_helper(wallet_user_info).await; + handle_string_result_as_callback(result, promise_id); + }); + } +} + +async fn log_in_password_user_helper( + password_user_info: PasswordUserInfo, +) -> Result { + let mut client_login = Login::new(); + let opaque_login_request = client_login + .start(&password_user_info.password) + .map_err(crate::handle_error)?; + let login_start_request = OpaqueLoginStartRequest { + opaque_login_request, + username: password_user_info.username, + device_key_upload: Some(DeviceKeyUpload { + device_key_info: Some(IdentityKeyInfo { + payload: password_user_info.key_payload, + payload_signature: password_user_info.key_payload_signature, + social_proof: None, + }), + content_upload: Some(Prekey { + prekey: password_user_info.content_prekey, + prekey_signature: password_user_info.content_prekey_signature, + }), + notif_upload: Some(Prekey { + prekey: password_user_info.notif_prekey, + prekey_signature: password_user_info.notif_prekey_signature, + }), + one_time_content_prekeys: password_user_info.content_one_time_keys, + one_time_notif_prekeys: password_user_info.notif_one_time_keys, + device_type: DEVICE_TYPE.into(), + }), + force: None, + }; + + let mut identity_client = get_unauthenticated_client( + IDENTITY_SOCKET_ADDR, + CODE_VERSION, + DEVICE_TYPE.as_str_name().to_lowercase(), + ) + .await?; + + let response = identity_client + .log_in_password_user_start(login_start_request) + .await?; + + let login_start_response = response.into_inner(); + + let opaque_login_upload = client_login + .finish(&login_start_response.opaque_login_response) + .map_err(crate::handle_error)?; + + let login_finish_request = OpaqueLoginFinishRequest { + session_id: login_start_response.session_id, + opaque_login_upload, + }; + + let login_finish_response = identity_client + .log_in_password_user_finish(login_finish_request) + .await? + .into_inner(); + let user_id_and_access_token = + UserIDAndDeviceAccessToken::from(login_finish_response); + Ok(serde_json::to_string(&user_id_and_access_token)?) +} + +async fn log_in_wallet_user_helper( + wallet_user_info: WalletUserInfo, +) -> Result { + let login_request = WalletAuthRequest { + siwe_message: wallet_user_info.siwe_message, + siwe_signature: wallet_user_info.siwe_signature, + device_key_upload: Some(DeviceKeyUpload { + device_key_info: Some(IdentityKeyInfo { + payload: wallet_user_info.key_payload, + payload_signature: wallet_user_info.key_payload_signature, + social_proof: None, // The SIWE message and signature are the social proof + }), + content_upload: Some(Prekey { + prekey: wallet_user_info.content_prekey, + prekey_signature: wallet_user_info.content_prekey_signature, + }), + notif_upload: Some(Prekey { + prekey: wallet_user_info.notif_prekey, + prekey_signature: wallet_user_info.notif_prekey_signature, + }), + one_time_content_prekeys: wallet_user_info.content_one_time_keys, + one_time_notif_prekeys: wallet_user_info.notif_one_time_keys, + device_type: DEVICE_TYPE.into(), + }), + farcaster_id: None, + }; + + let mut identity_client = get_unauthenticated_client( + IDENTITY_SOCKET_ADDR, + CODE_VERSION, + DEVICE_TYPE.as_str_name().to_lowercase(), + ) + .await?; + + let login_response = identity_client + .log_in_wallet_user(login_request) + .await? + .into_inner(); + + let user_id_and_access_token = + UserIDAndDeviceAccessToken::from(login_response); + Ok(serde_json::to_string(&user_id_and_access_token)?) +} 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,4 +1,4 @@ -use comm_opaque2::client::{Login, Registration}; +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, @@ -10,9 +10,8 @@ UploadOneTimeKeysRequest, }; use grpc_clients::identity::protos::unauth::{ - DeviceKeyUpload, DeviceType, Empty, IdentityKeyInfo, - OpaqueLoginFinishRequest, OpaqueLoginStartRequest, Prekey, - SecondaryDeviceKeysUploadRequest, WalletAuthRequest, + DeviceKeyUpload, DeviceType, Empty, IdentityKeyInfo, Prekey, + SecondaryDeviceKeysUploadRequest, }; use grpc_clients::identity::{get_auth_client, get_unauthenticated_client}; use lazy_static::lazy_static; @@ -29,9 +28,7 @@ mod utils; use crate::argon2_tools::compute_backup_key_str; -use crate::identity::{ - AuthInfo, PasswordUserInfo, UserIDAndDeviceAccessToken, WalletUserInfo, -}; +use crate::identity::{AuthInfo, UserIDAndDeviceAccessToken}; use crate::utils::jsi_callbacks::{ handle_string_result_as_callback, handle_void_result_as_callback, }; @@ -470,174 +467,6 @@ Ok(serde_json::to_string(&keyserver_keys)?) } -#[instrument] -fn log_in_password_user( - username: String, - password: String, - key_payload: String, - key_payload_signature: String, - content_prekey: String, - content_prekey_signature: String, - notif_prekey: String, - notif_prekey_signature: String, - promise_id: u32, -) { - RUNTIME.spawn(async move { - let password_user_info = PasswordUserInfo { - username, - password, - key_payload, - key_payload_signature, - content_prekey, - content_prekey_signature, - notif_prekey, - notif_prekey_signature, - content_one_time_keys: Vec::new(), - notif_one_time_keys: Vec::new(), - farcaster_id: None, - }; - let result = log_in_password_user_helper(password_user_info).await; - handle_string_result_as_callback(result, promise_id); - }); -} - -async fn log_in_password_user_helper( - password_user_info: PasswordUserInfo, -) -> Result { - let mut client_login = Login::new(); - let opaque_login_request = client_login - .start(&password_user_info.password) - .map_err(handle_error)?; - let login_start_request = OpaqueLoginStartRequest { - opaque_login_request, - username: password_user_info.username, - device_key_upload: Some(DeviceKeyUpload { - device_key_info: Some(IdentityKeyInfo { - payload: password_user_info.key_payload, - payload_signature: password_user_info.key_payload_signature, - social_proof: None, - }), - content_upload: Some(Prekey { - prekey: password_user_info.content_prekey, - prekey_signature: password_user_info.content_prekey_signature, - }), - notif_upload: Some(Prekey { - prekey: password_user_info.notif_prekey, - prekey_signature: password_user_info.notif_prekey_signature, - }), - one_time_content_prekeys: password_user_info.content_one_time_keys, - one_time_notif_prekeys: password_user_info.notif_one_time_keys, - device_type: DEVICE_TYPE.into(), - }), - force: None, - }; - - let mut identity_client = get_unauthenticated_client( - IDENTITY_SOCKET_ADDR, - CODE_VERSION, - DEVICE_TYPE.as_str_name().to_lowercase(), - ) - .await?; - - let response = identity_client - .log_in_password_user_start(login_start_request) - .await?; - - let login_start_response = response.into_inner(); - - let opaque_login_upload = client_login - .finish(&login_start_response.opaque_login_response) - .map_err(handle_error)?; - - let login_finish_request = OpaqueLoginFinishRequest { - session_id: login_start_response.session_id, - opaque_login_upload, - }; - - let login_finish_response = identity_client - .log_in_password_user_finish(login_finish_request) - .await? - .into_inner(); - let user_id_and_access_token = - UserIDAndDeviceAccessToken::from(login_finish_response); - Ok(serde_json::to_string(&user_id_and_access_token)?) -} - -#[instrument] -fn log_in_wallet_user( - siwe_message: String, - siwe_signature: String, - key_payload: String, - key_payload_signature: String, - content_prekey: String, - content_prekey_signature: String, - notif_prekey: String, - notif_prekey_signature: String, - promise_id: u32, -) { - RUNTIME.spawn(async move { - let wallet_user_info = WalletUserInfo { - siwe_message, - siwe_signature, - key_payload, - key_payload_signature, - content_prekey, - content_prekey_signature, - notif_prekey, - notif_prekey_signature, - content_one_time_keys: Vec::new(), - notif_one_time_keys: Vec::new(), - farcaster_id: None, - }; - let result = log_in_wallet_user_helper(wallet_user_info).await; - handle_string_result_as_callback(result, promise_id); - }); -} - -async fn log_in_wallet_user_helper( - wallet_user_info: WalletUserInfo, -) -> Result { - let login_request = WalletAuthRequest { - siwe_message: wallet_user_info.siwe_message, - siwe_signature: wallet_user_info.siwe_signature, - device_key_upload: Some(DeviceKeyUpload { - device_key_info: Some(IdentityKeyInfo { - payload: wallet_user_info.key_payload, - payload_signature: wallet_user_info.key_payload_signature, - social_proof: None, // The SIWE message and signature are the social proof - }), - content_upload: Some(Prekey { - prekey: wallet_user_info.content_prekey, - prekey_signature: wallet_user_info.content_prekey_signature, - }), - notif_upload: Some(Prekey { - prekey: wallet_user_info.notif_prekey, - prekey_signature: wallet_user_info.notif_prekey_signature, - }), - one_time_content_prekeys: wallet_user_info.content_one_time_keys, - one_time_notif_prekeys: wallet_user_info.notif_one_time_keys, - device_type: DEVICE_TYPE.into(), - }), - farcaster_id: None, - }; - - let mut identity_client = get_unauthenticated_client( - IDENTITY_SOCKET_ADDR, - CODE_VERSION, - DEVICE_TYPE.as_str_name().to_lowercase(), - ) - .await?; - - let login_response = identity_client - .log_in_wallet_user(login_request) - .await? - .into_inner(); - - let user_id_and_access_token = - UserIDAndDeviceAccessToken::from(login_response); - Ok(serde_json::to_string(&user_id_and_access_token)?) -} - struct UpdatePasswordInfo { user_id: String, device_id: String,