Skip to content

Commit

Permalink
feat: parse ClientId as an URI
Browse files Browse the repository at this point in the history
  • Loading branch information
beltram committed Jan 5, 2024
1 parent 7496165 commit 28a9bbb
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions x509_credential/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use openmls_traits::{
pub struct CertificateKeyPair(pub SignatureKeyPair);

impl CertificateKeyPair {
/// Constructs the `CertificateKeyPair` from a private key and a der encoded certificate chain
/// Constructs the `CertificateKeyPair` from a private key and a der encoded
/// certificate chain
pub fn new(sk: Vec<u8>, cert_chain: Vec<Vec<u8>>) -> Result<Self, CryptoError> {
if cert_chain.len() < 2 {
return Err(CryptoError::IncompleteCertificateChain);
Expand Down Expand Up @@ -105,8 +106,6 @@ pub trait X509Ext {
fn identity(&self) -> Result<Vec<u8>, CryptoError>;
}

const CLIENT_ID_PREFIX: &str = "im:wireapp=";

impl X509Ext for Certificate {
fn is_valid(&self) -> Result<(), CryptoError> {
if !self.is_time_valid()? {
Expand Down Expand Up @@ -214,11 +213,14 @@ impl X509Ext for Certificate {
}
}

/// Turn 'wireapp://[email protected]' into
/// '647a4b64-b64c-44e7-8c5a-9e2c2652f65c:[email protected]'
fn try_to_qualified_wire_client_id(client_id: &str) -> Option<Vec<u8>> {
const COLON: u8 = 58;
const WIRE_URI_SCHEME: &str = "wireapp://";

let client_id = client_id.strip_prefix(CLIENT_ID_PREFIX)?;
let (user_id, rest) = client_id.split_once('/')?;
let client_id = client_id.strip_prefix(WIRE_URI_SCHEME)?;
let (user_id, rest) = client_id.split_once('!')?;
let user_id = to_hyphenated_user_id(user_id)?;

let client_id = [&user_id[..], &[COLON], rest.as_bytes()].concat();
Expand All @@ -237,3 +239,14 @@ fn to_hyphenated_user_id(user_id: &str) -> Option<[u8; uuid::fmt::Hyphenated::LE

Some(buf)
}

#[test]
fn to_qualified_wire_client_id_should_work() {
let input = "wireapp://[email protected]";
let output = try_to_qualified_wire_client_id(input).unwrap();
let output = std::str::from_utf8(&output).unwrap();
assert_eq!(
output,
"647a4b64-b64c-44e7-8c5a-9e2c2652f65c:[email protected]"
);
}

0 comments on commit 28a9bbb

Please sign in to comment.