Skip to content

Commit

Permalink
allow non-uuid identities
Browse files Browse the repository at this point in the history
  • Loading branch information
kkohbrok committed Mar 26, 2024
1 parent f611ba3 commit 49c3770
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
5 changes: 4 additions & 1 deletion api_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use minimal_ds_types::{
requests::{
DeleteClientRequest, DeleteGroupRequest, FetchKeyPackageRequest, FetchMessagesRequest,
},
AuthToken, ClientCredentials, DsClientId, DsGroupId,
AuthToken, ClientCredentials,
};
use mls_assist::messages::{AssistedMessageError, AssistedMessageOut};
use openmls::{
Expand All @@ -40,6 +40,9 @@ use openmls::{
use requests::{MinimalDsMessageOut, MinimalDsResponseIn, RegisterClientRequestOut};
use reqwest::{Client, Url};

// Re-export types
pub use minimal_ds_types::{DsClientId, DsGroupId};

pub mod errors;
pub mod requests;

Expand Down
28 changes: 11 additions & 17 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ impl Deref for TlsUuid {
TlsDeserializeBytes,
)]
pub struct DsClientId {
id: TlsUuid,
id: [u8; 32],
}

impl DsClientId {
pub fn new() -> Self {
Self {
id: Uuid::new_v4().into(),
}
pub fn new(bytes: &[u8]) -> Result<Self, DsClientIdError> {
bytes.try_into()
}

pub fn from_serialized_credential(
Expand All @@ -99,40 +97,36 @@ impl DsClientId {
Self::try_from(identity.as_slice())
}

pub fn as_bytes(&self) -> &[u8; 16] {
self.id.as_bytes()
pub fn as_bytes(&self) -> &[u8; 32] {
&self.id
}
}

impl std::fmt::Display for DsClientId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.id.deref())
write!(f, "{:?}", self.id)
}
}

#[derive(Debug, Error)]
pub enum DsClientIdError {
#[error("Invalid Credential: {0}")]
InvalidCredential(#[from] tls_codec::Error),
#[error(transparent)]
InvalidUuid(#[from] uuid::Error),
#[error("Too many bytes in the input. Expected 32 bytes.")]
TooManyBytes,
}

impl TryFrom<&[u8]> for DsClientId {
type Error = DsClientIdError;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let id = Uuid::from_slice(bytes)?.into();
let id = bytes
.try_into()
.map_err(|_| DsClientIdError::TooManyBytes)?;
Ok(Self { id })
}
}

impl From<Uuid> for DsClientId {
fn from(id: Uuid) -> Self {
Self { id: id.into() }
}
}

#[cfg(feature = "rusqlite")]
impl ToSql for DsClientId {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
Expand Down

0 comments on commit 49c3770

Please sign in to comment.