-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Keyserver/rust] Add publish_prekeys implementation
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
1 parent
3e9860b
commit 7524181
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
keyserver/addons/rust-node-addon/src/identity_client/auth_client.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
keyserver/addons/rust-node-addon/src/identity_client/prekey.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |