diff --git a/keyserver/addons/rust-node-addon/build.rs b/keyserver/addons/rust-node-addon/build.rs --- a/keyserver/addons/rust-node-addon/build.rs +++ b/keyserver/addons/rust-node-addon/build.rs @@ -5,7 +5,10 @@ tonic_build::configure() .build_server(false) .compile( - &["../../../shared/protos/identity_client.proto"], + &[ + "../../../shared/protos/identity_client.proto", + "../../../shared/protos/identity_authenticated.proto", + ], &["../../../shared/protos"], ) .unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e)); diff --git a/keyserver/addons/rust-node-addon/rust-binding-types.js b/keyserver/addons/rust-node-addon/rust-binding-types.js --- a/keyserver/addons/rust-node-addon/rust-binding-types.js +++ b/keyserver/addons/rust-node-addon/rust-binding-types.js @@ -35,6 +35,15 @@ message: string, signature: string, ) => Promise, + +publish_prekeys: ( + user_id: string, + device_id: string, + access_token: string, + content_prekey: string, + content_prekey_signature: string, + notif_prekey: string, + notif_prekey_signature: string, + ) => Promise, }; export type { RustNativeBindingAPI }; 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 @@ -1,5 +1,6 @@ pub mod add_reserved_usernames; pub mod login; +pub mod prekey; pub mod register_user; pub mod remove_reserved_usernames; pub mod identity_client { diff --git a/keyserver/addons/rust-node-addon/src/identity_client/prekey.rs b/keyserver/addons/rust-node-addon/src/identity_client/prekey.rs new file mode 100644 --- /dev/null +++ b/keyserver/addons/rust-node-addon/src/identity_client/prekey.rs @@ -0,0 +1,64 @@ +pub mod client { + tonic::include_proto!("identity.client"); +} +mod auth_proto { + tonic::include_proto!("identity.authenticated"); +} +use super::{Error, Status, IDENTITY_SERVICE_CONFIG}; +use auth_proto::identity_client_service_client::IdentityClientServiceClient as AuthClient; +use auth_proto::RefreshUserPreKeysRequest; +use client::PreKey; +use napi::Result; +use tonic::{transport::Endpoint, Request}; +use tracing::warn; + +pub async fn publish_prekeys( + user_id: String, + device_id: String, + access_token: String, + content_prekey: String, + content_prekey_signature: String, + notif_prekey: String, + notif_prekey_signature: String, +) -> Result { + let channel = + Endpoint::from_static(&IDENTITY_SERVICE_CONFIG.identity_socket_addr) + .connect() + .await + .unwrap(); + + // Once this rust addon can do getCommConfig, remove explicit passing of user + // credentials within this scope + let mut client = + AuthClient::with_interceptor(channel, |mut request: Request<()>| { + let metadata = request.metadata_mut(); + metadata.append("user_id", user_id.parse().unwrap()); + metadata.append("device_id", device_id.parse().unwrap()); + metadata.append("access_token", access_token.parse().unwrap()); + Ok(request) + }); + + let upload_request = RefreshUserPreKeysRequest { + new_content_pre_keys: Some(PreKey { + pre_key: content_prekey, + pre_key_signature: content_prekey_signature, + }), + new_notif_pre_keys: Some(PreKey { + pre_key: notif_prekey, + pre_key_signature: notif_prekey_signature, + }), + }; + + client + .refresh_user_pre_keys(upload_request) + .await + .map_err(|e| { + warn!( + "Failed to upload new prekeys to identity service: {:?}", + e.message() + ); + Error::from_status(Status::GenericFailure) + })?; + + Ok(true) +}