Skip to content

Commit

Permalink
[Keyserver/rust] Add publish_prekeys implementation
Browse files Browse the repository at this point in the history
Summary:
Implement the rust bindings for publishing prekeys to
identity service.

Part of https://linear.app/comm/issue/ENG-3944

Depends on D8464

Test Plan: Tested in a later diff

Reviewers: bartek, varun

Reviewed By: varun

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D8473
  • Loading branch information
jonringer-comm committed Aug 8, 2023
1 parent 3e9860b commit 7524181
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
5 changes: 4 additions & 1 deletion keyserver/addons/rust-node-addon/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ fn main() {
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));
Expand Down
9 changes: 9 additions & 0 deletions keyserver/addons/rust-node-addon/rust-binding-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ type RustNativeBindingAPI = {
message: string,
signature: string,
) => Promise<void>,
+publish_prekeys: (
userId: string,
deviceId: string,
accessToken: string,
contentPrekey: string,
contentPrekeySignature: string,
notifPrekey: string,
notifPrekeySignature: string,
) => Promise<boolean>,
};

export type { RustNativeBindingAPI };
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
pub mod client {
tonic::include_proto!("identity.client");
}
pub mod auth_proto {
tonic::include_proto!("identity.authenticated");
}
use auth_proto::identity_client_service_client::IdentityClientServiceClient as AuthClient;
use tonic::{
codegen::InterceptedService,
metadata::{errors::InvalidMetadataValue, Ascii, MetadataValue},
service::Interceptor,
transport::{Channel, Endpoint},
Request, Status,
};

use super::IDENTITY_SERVICE_CONFIG;

pub struct AuthLayer {
user_id: String,
device_id: String,
access_token: String,
}

trait ToAscii {
fn parse_to_ascii(&self) -> Result<MetadataValue<Ascii>, Status>;
}

impl ToAscii for str {
fn parse_to_ascii(&self) -> Result<MetadataValue<Ascii>, Status> {
self.parse().map_err(|e: InvalidMetadataValue| {
Status::invalid_argument(format!(
"Non-Ascii character present in metadata value: {}",
e
))
})
}
}

impl Interceptor for AuthLayer {
fn call(&mut self, mut request: Request<()>) -> Result<Request<()>, Status> {
let metadata = request.metadata_mut();
metadata.insert("user_id", self.user_id.parse_to_ascii()?);
metadata.insert("device_id", self.device_id.parse_to_ascii()?);
metadata.insert("access_token", self.access_token.parse_to_ascii()?);

Ok(request)
}
}
pub async fn get_auth_client(
user_id: String,
device_id: String,
access_token: String,
) -> AuthClient<InterceptedService<Channel, AuthLayer>> {
let channel =
Endpoint::from_static(&IDENTITY_SERVICE_CONFIG.identity_socket_addr)
.connect()
.await
.unwrap();

let interceptor = AuthLayer {
user_id,
device_id,
access_token,
};

AuthClient::with_interceptor(channel, interceptor)
}
2 changes: 2 additions & 0 deletions keyserver/addons/rust-node-addon/src/identity_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod add_reserved_usernames;
pub mod auth_client;
pub mod login;
pub mod prekey;
pub mod register_user;
pub mod remove_reserved_usernames;
pub mod identity_client {
Expand Down
41 changes: 41 additions & 0 deletions keyserver/addons/rust-node-addon/src/identity_client/prekey.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use super::auth_client::{
auth_proto::RefreshUserPreKeysRequest, client::PreKey, get_auth_client,
};
use super::{Error, Status};
use napi::Result;
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<bool> {
// Once this rust addon can do getCommConfig, remove explicit passing of user
// credentials within this scope
let mut client = get_auth_client(user_id, device_id, access_token).await;

let message = 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(message).await.map_err(|e| {
warn!(
"Failed to upload new prekeys to identity service: {:?}",
e.message()
);
Error::from_status(Status::GenericFailure)
})?;

Ok(true)
}

0 comments on commit 7524181

Please sign in to comment.