Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Feb 8, 2023
1 parent 3a35b4a commit 279d01d
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 37 deletions.
4 changes: 2 additions & 2 deletions libsignal-service/src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,12 @@ pub async fn get_preferred_protocol_address<S: SessionStore>(
address: &ServiceAddress,
device_id: DeviceId,
) -> Result<ProtocolAddress, libsignal_protocol::error::SignalProtocolError> {
let address = ProtocolAddress::new(address.to_string(), device_id);
let address = address.to_protocol_address(device_id);
if session_store.load_session(&address, None).await?.is_some() {
return Ok(address);
}

Ok(ProtocolAddress::new(address.to_string(), device_id))
Ok(address)
}

/// Decrypt a Sealed Sender message `ciphertext` in either the v1 or v2 format, validate its sender
Expand Down
2 changes: 1 addition & 1 deletion libsignal-service/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Metadata {

impl Metadata {
pub(crate) fn protocol_address(&self) -> ProtocolAddress {
ProtocolAddress::new(self.sender.to_string(), self.sender_device.into())
self.sender.to_protocol_address(self.sender_device)
}
}

Expand Down
2 changes: 1 addition & 1 deletion libsignal-service/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Envelope {
}

pub fn source_address(&self) -> ServiceAddress {
let uuid: Uuid = self
let uuid = self
.source_uuid
.as_deref()
.and_then(|u| Uuid::parse_str(u).ok())
Expand Down
13 changes: 6 additions & 7 deletions libsignal-service/src/profile_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ impl ProfileService {
address: ServiceAddress,
profile_key: Option<zkgroup::profiles::ProfileKey>,
) -> Result<SignalServiceProfile, ServiceError> {
let endpoint = match (profile_key, address.uuid) {
(Some(key), uuid) => {
let uid_bytes = uuid.as_bytes();
let endpoint = match profile_key {
Some(key) => {
let uid_bytes = address.uuid.as_bytes();
let version = bincode::serialize(
&key.get_profile_key_version(*uid_bytes),
)?;
let version = std::str::from_utf8(&version)
.expect("hex encoded profile key version");
format!("/v1/profile/{}/{}", uuid, version)
format!("/v1/profile/{}/{}", address.uuid, version)
},
(_, uuid) => {
// TODO: check if this is even possible?
format!("/v1/profile/{}", uuid)
None => {
format!("/v1/profile/{}", address.uuid)
},
};

Expand Down
12 changes: 6 additions & 6 deletions libsignal-service/src/push_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ pub trait PushService: MaybeSend {
&mut self,
messages: OutgoingPushMessages,
) -> Result<SendMessageResponse, ServiceError> {
let path = format!("/v1/messages/{}", messages.recipient);
let path = format!("/v1/messages/{}", messages.recipient.uuid);
self.put_json(
Endpoint::Service,
&path,
Expand Down Expand Up @@ -668,9 +668,9 @@ pub trait PushService: MaybeSend {
bincode::serialize(&key.get_profile_key_version(*uid_bytes))?;
let version = std::str::from_utf8(&version)
.expect("hex encoded profile key version");
format!("/v1/profile/{}/{}", address, version)
format!("/v1/profile/{}/{}", address.uuid, version)
} else {
format!("/v1/profile/{}", address)
format!("/v1/profile/{}", address.uuid)
};
// TODO: set locale to en_US
self.get_json(
Expand Down Expand Up @@ -700,7 +700,7 @@ pub trait PushService: MaybeSend {
destination: &ServiceAddress,
device_id: u32,
) -> Result<PreKeyBundle, ServiceError> {
let path = format!("/v2/keys/{}/{}", destination, device_id);
let path = format!("/v2/keys/{}/{}", destination.uuid, device_id);

let mut pre_key_response: PreKeyResponse = self
.get_json(Endpoint::Service, &path, HttpAuthOverride::NoOverride)
Expand All @@ -718,9 +718,9 @@ pub trait PushService: MaybeSend {
device_id: u32,
) -> Result<Vec<PreKeyBundle>, ServiceError> {
let path = if device_id == 1 {
format!("/v2/keys/{}/*", destination)
format!("/v2/keys/{}/*", destination.uuid)
} else {
format!("/v2/keys/{}/{}", destination, device_id)
format!("/v2/keys/{}/{}", destination.uuid, device_id)
};
let pre_key_response: PreKeyResponse = self
.get_json(Endpoint::Service, &path, HttpAuthOverride::NoOverride)
Expand Down
18 changes: 8 additions & 10 deletions libsignal-service/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::SystemTime;
use chrono::prelude::*;
use libsignal_protocol::{
process_prekey_bundle, DeviceId, IdentityKeyStore, PreKeyStore,
ProtocolAddress, SenderCertificate, SenderKeyStore, SessionStore,
SignalProtocolError, SignedPreKeyStore,
SenderCertificate, SenderKeyStore, SessionStore, SignalProtocolError,
SignedPreKeyStore,
};
use log::{info, trace};
use rand::{CryptoRng, Rng};
Expand Down Expand Up @@ -104,7 +104,7 @@ pub enum MessageSenderError {
#[error("Failed to upload attachment {0}")]
AttachmentUploadError(#[from] AttachmentUploadError),

#[error("Untrusted identity key with {address}")]
#[error("Untrusted identity key with {address:?}")]
UntrustedIdentity { address: ServiceAddress },

#[error("Exceeded maximum number of retries")]
Expand Down Expand Up @@ -359,7 +359,7 @@ where
}

if end_session {
log::debug!("ending session with {}", recipient);
log::debug!("ending session with {}", recipient.uuid);
self.session_store
.delete_all_sessions(&recipient.uuid.to_string())
.await?;
Expand Down Expand Up @@ -495,10 +495,8 @@ where
"creating session with missing device {}",
missing_device_id
);
let remote_address = ProtocolAddress::new(
recipient.to_string(),
(*missing_device_id).into(),
);
let remote_address =
recipient.to_protocol_address(*missing_device_id);
let pre_key = self
.service
.get_pre_key(&recipient, *missing_device_id)
Expand Down Expand Up @@ -770,14 +768,14 @@ where
..
} = sent;
UnidentifiedDeliveryStatus {
destination_uuid: Some(recipient.to_string()),
destination_uuid: Some(recipient.uuid.to_string()),
unidentified: Some(*unidentified),
}
})
.collect();
ContentBody::SynchronizeMessage(SyncMessage {
sent: Some(sync_message::Sent {
destination_uuid: recipient.map(|r| r.to_string()),
destination_uuid: recipient.map(|r| r.uuid.to_string()),
destination_e164: None,
message: data_message,
timestamp: Some(timestamp),
Expand Down
14 changes: 9 additions & 5 deletions libsignal-service/src/service_address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{convert::TryFrom, fmt};
use std::convert::TryFrom;

use libsignal_protocol::{DeviceId, ProtocolAddress};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

Expand All @@ -8,7 +9,7 @@ pub enum ParseServiceAddressError {
#[error("Supplied UUID could not be parsed")]
InvalidUuid(#[from] uuid::Error),

#[error("Envelope with no Uuid")]
#[error("Envelope without UUID")]
NoUuid,
}

Expand All @@ -17,9 +18,12 @@ pub struct ServiceAddress {
pub uuid: Uuid,
}

impl fmt::Display for ServiceAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.uuid)
impl ServiceAddress {
pub fn to_protocol_address(
&self,
device_id: impl Into<DeviceId>,
) -> ProtocolAddress {
ProtocolAddress::new(self.uuid.to_string(), device_id.into())
}
}

Expand Down
5 changes: 1 addition & 4 deletions libsignal-service/src/session_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ pub trait SessionStoreExt: SessionStore {
) -> Result<usize, SignalProtocolError> {
let mut count = 0;
match self
.delete_session(&ProtocolAddress::new(
address.to_string(),
device_id.into(),
))
.delete_session(&address.to_protocol_address(device_id))
.await
{
Ok(()) => {
Expand Down
2 changes: 1 addition & 1 deletion libsignal-service/src/websocket/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl SignalWebSocket {
&mut self,
messages: OutgoingPushMessages,
) -> Result<SendMessageResponse, ServiceError> {
let path = format!("/v1/messages/{}", messages.recipient);
let path = format!("/v1/messages/{}", messages.recipient.uuid);
self.put_json(&path, messages).await
}
}

0 comments on commit 279d01d

Please sign in to comment.