diff --git a/keyserver/addons/rust-node-addon/src/identity_client/login_user.rs b/keyserver/addons/rust-node-addon/src/identity_client/login_user.rs --- a/keyserver/addons/rust-node-addon/src/identity_client/login_user.rs +++ b/keyserver/addons/rust-node-addon/src/identity_client/login_user.rs @@ -1,5 +1,73 @@ use super::*; +#[napi] +#[instrument(skip_all)] +async fn login_user_wallet( + user_id: String, + signing_public_key: String, + siwe_message: String, + siwe_signature: String, + mut session_initialization_info: HashMap, + social_proof: String, +) -> Result { + let channel = Channel::from_static(&IDENTITY_SERVICE_SOCKET_ADDR) + .connect() + .await + .map_err(|_| { + Error::new( + Status::GenericFailure, + "Unable to connect to identity service".to_string(), + ) + })?; + let token: MetadataValue<_> = AUTH_TOKEN + .parse() + .map_err(|_| Error::from_status(Status::GenericFailure))?; + let mut identity_client = + IdentityServiceClient::with_interceptor(channel, |mut req: Request<()>| { + req.metadata_mut().insert("authorization", token.clone()); + Ok(req) + }); + + // Create a LoginRequest channel and use ReceiverStream to turn the + // MPSC receiver into a Stream for outbound messages + let (tx, rx) = mpsc::channel(1); + let stream = ReceiverStream::new(rx); + let request = Request::new(stream); + + // `response` is the Stream for inbound messages + let mut response = identity_client + .login_user(request) + .await + .map_err(|_| Error::from_status(Status::GenericFailure))? + .into_inner(); + + // Start wallet login on client and send initial login request to Identity + // service + session_initialization_info.insert("socialProof".to_string(), social_proof); + let login_request = LoginRequest { + data: Some(WalletLoginRequest(WalletLoginRequestStruct { + user_id, + signing_public_key, + siwe_message, + siwe_signature, + session_initialization_info: Some(SessionInitializationInfo { + info: session_initialization_info, + }), + })), + }; + if let Err(e) = tx.send(login_request).await { + error!("Response was dropped: {}", e); + return Err(Error::from_status(Status::GenericFailure)); + } + + // Return access token + let message = response.message().await.map_err(|e| { + error!("Received an error from inbound message stream: {}", e); + Error::from_status(Status::GenericFailure) + })?; + handle_wallet_login_response(message) +} + #[napi] #[instrument(skip_all)] async fn login_user_pake( @@ -134,3 +202,16 @@ Err(handle_unexpected_response(message)) } } + +fn handle_wallet_login_response( + message: Option, +) -> Result { + if let Some(LoginResponse { + data: Some(WalletLoginResponse(WalletLoginResponseStruct { access_token })), + }) = message + { + Ok(access_token) + } else { + Err(handle_unexpected_response(message)) + } +} diff --git a/keyserver/addons/rust-node-addon/src/identity_client/mod.rs b/keyserver/addons/rust-node-addon/src/identity_client/mod.rs --- a/keyserver/addons/rust-node-addon/src/identity_client/mod.rs +++ b/keyserver/addons/rust-node-addon/src/identity_client/mod.rs @@ -9,7 +9,9 @@ use identity::identity_service_client::IdentityServiceClient; use identity::{ login_request::Data::PakeLoginRequest, + login_request::Data::WalletLoginRequest, login_response::Data::PakeLoginResponse as LoginPakeLoginResponse, + login_response::Data::WalletLoginResponse, pake_login_request::Data::PakeCredentialFinalization as LoginPakeCredentialFinalization, pake_login_request::Data::PakeCredentialRequestAndUserId, pake_login_response::Data::AccessToken, @@ -26,7 +28,8 @@ PakeRegistrationRequestAndUserId as PakeRegistrationRequestAndUserIdStruct, PakeRegistrationUploadAndCredentialRequest as PakeRegistrationUploadAndCredentialRequestStruct, RegistrationRequest, RegistrationResponse as RegistrationResponseMessage, - SessionInitializationInfo, + SessionInitializationInfo, WalletLoginRequest as WalletLoginRequestStruct, + WalletLoginResponse as WalletLoginResponseStruct, }; use lazy_static::lazy_static; use napi::bindgen_prelude::*; diff --git a/lib/types/rust-binding-types.js b/lib/types/rust-binding-types.js --- a/lib/types/rust-binding-types.js +++ b/lib/types/rust-binding-types.js @@ -29,6 +29,14 @@ password: string, sessionInitializationInfo: SignedIdentityKeysBlob, ) => Promise, + +loginUserWallet: ( + userId: string, + signingPublicKey: string, + siweMessage: string, + siweSignature: string, + sessionInitializationInfo: SignedIdentityKeysBlob, + socialProof: string, + ) => Promise, +deleteUser: (userId: string) => Promise, +TunnelbrokerClient: Class, };