Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PNI in PendingMember #329

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/groups_v2/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
use zkgroup::profiles::ProfileKey;

use crate::ServiceAddress;

use super::GroupDecodingError;

#[derive(Copy, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
Expand All @@ -30,15 +32,15 @@ impl PartialEq for Member {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingMember {
pub uuid: Uuid,
pub address: ServiceAddress,
pub role: Role,
pub added_by_uuid: Uuid,
pub timestamp: u64,
}

#[derive(Derivative, Clone, Deserialize, Serialize)]
#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub struct RequestingMember {
pub uuid: Uuid,
Expand Down Expand Up @@ -69,7 +71,7 @@ pub struct AccessControl {
pub add_from_invite_link: AccessRequired,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq)]
pub struct Group {
pub title: String,
pub avatar: String,
Expand Down
17 changes: 15 additions & 2 deletions src/groups_v2/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ impl From<zkgroup::ZkGroupVerificationFailure> for GroupDecodingError {
}

impl GroupOperations {
fn decrypt_service_id(
&self,
ciphertext: &[u8],
) -> Result<ServiceId, GroupDecodingError> {
match self
.group_secret_params
.decrypt_service_id(bincode::deserialize(ciphertext)?)?
{
ServiceId::Aci(aci) => Ok(ServiceId::from(aci)),
ServiceId::Pni(pni) => Ok(ServiceId::from(pni)),
}
}

fn decrypt_aci(
&self,
ciphertext: &[u8],
Expand Down Expand Up @@ -131,11 +144,11 @@ impl GroupOperations {
) -> Result<PendingMember, GroupDecodingError> {
let inner_member =
member.member.ok_or(GroupDecodingError::WrongBlob)?;
let aci = self.decrypt_aci(&inner_member.user_id)?;
let service_id = self.decrypt_service_id(&inner_member.user_id)?;
let added_by_uuid = self.decrypt_aci(&member.added_by_user_id)?;

Ok(PendingMember {
uuid: aci.into(),
address: service_id.into(),
role: inner_member.role.try_into()?,
added_by_uuid: added_by_uuid.into(),
timestamp: member.timestamp,
Expand Down
37 changes: 30 additions & 7 deletions src/service_address.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::TryFrom;

use libsignal_protocol::{DeviceId, ProtocolAddress};
use libsignal_protocol::{DeviceId, ProtocolAddress, ServiceId};
use uuid::Uuid;

pub use crate::push_service::ServiceIdType;
Expand Down Expand Up @@ -48,14 +48,24 @@ impl ServiceAddress {
}
}

#[deprecated]
pub fn new_aci(uuid: Uuid) -> Self {
Self::from_aci(uuid)
}

pub fn from_aci(uuid: Uuid) -> Self {
Self {
uuid,
identity: ServiceIdType::AccountIdentity,
}
}

#[deprecated]
pub fn new_pni(uuid: Uuid) -> Self {
Self::from_pni(uuid)
}

pub fn from_pni(uuid: Uuid) -> Self {
Self {
uuid,
identity: ServiceIdType::PhoneNumberIdentity,
Expand Down Expand Up @@ -87,15 +97,28 @@ impl ServiceAddress {
}
}

impl From<ServiceId> for ServiceAddress {
fn from(service_id: ServiceId) -> Self {
match service_id {
ServiceId::Aci(service_id) => {
ServiceAddress::from_aci(service_id.into())
},
ServiceId::Pni(service_id) => {
ServiceAddress::from_pni(service_id.into())
},
}
}
}

impl TryFrom<&ProtocolAddress> for ServiceAddress {
type Error = ParseServiceAddressError;

fn try_from(addr: &ProtocolAddress) -> Result<Self, Self::Error> {
let value = addr.name();
if let Some(pni) = value.strip_prefix("PNI:") {
Ok(ServiceAddress::new_pni(Uuid::parse_str(pni)?))
Ok(ServiceAddress::from_pni(Uuid::parse_str(pni)?))
} else {
Ok(ServiceAddress::new_aci(Uuid::parse_str(value)?))
Ok(ServiceAddress::from_aci(Uuid::parse_str(value)?))
}
.map_err(|e| {
tracing::error!("Parsing ServiceAddress from {:?}", addr);
Expand All @@ -109,9 +132,9 @@ impl TryFrom<&str> for ServiceAddress {

fn try_from(value: &str) -> Result<Self, Self::Error> {
if let Some(pni) = value.strip_prefix("PNI:") {
Ok(ServiceAddress::new_pni(Uuid::parse_str(pni)?))
Ok(ServiceAddress::from_pni(Uuid::parse_str(pni)?))
} else {
Ok(ServiceAddress::new_aci(Uuid::parse_str(value)?))
Ok(ServiceAddress::from_aci(Uuid::parse_str(value)?))
}
.map_err(|e| {
tracing::error!("Parsing ServiceAddress from '{}'", value);
Expand All @@ -125,9 +148,9 @@ impl TryFrom<&[u8]> for ServiceAddress {

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if let Some(pni) = value.strip_prefix(b"PNI:") {
Ok(ServiceAddress::new_pni(Uuid::from_slice(pni)?))
Ok(ServiceAddress::from_pni(Uuid::from_slice(pni)?))
} else {
Ok(ServiceAddress::new_aci(Uuid::from_slice(value)?))
Ok(ServiceAddress::from_aci(Uuid::from_slice(value)?))
}
.map_err(|e| {
tracing::error!("Parsing ServiceAddress from {:?}", value);
Expand Down
Loading