Skip to content

Commit

Permalink
Add AccountManager::linked_devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmiddiii committed Jun 4, 2024
1 parent 26c036e commit e873d07
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use aes::cipher::{KeyIvInit, StreamCipher as _};
use hmac::digest::Output;
use hmac::{Hmac, Mac};
use libsignal_protocol::{
kem, GenericSignedPreKey, IdentityKey, IdentityKeyStore, KeyPair,
KyberPreKeyRecord, PrivateKey, ProtocolStore, PublicKey, SenderKeyStore,
SignedPreKeyRecord,
kem, GenericSignedPreKey, IdentityKey, IdentityKeyPair, IdentityKeyStore,
KeyPair, KyberPreKeyRecord, PrivateKey, ProtocolStore, PublicKey,
SenderKeyStore, SignedPreKeyRecord,
};
use prost::Message;
use serde::{Deserialize, Serialize};
Expand All @@ -28,7 +28,7 @@ use crate::proto::sync_message::PniChangeNumber;
use crate::proto::{DeviceName, SyncMessage};
use crate::provisioning::generate_registration_id;
use crate::push_service::{
AvatarWrite, DeviceActivationRequest, RecaptchaAttributes,
AvatarWrite, DeviceActivationRequest, DeviceInfo, RecaptchaAttributes,
RegistrationMethod, ServiceIdType, VerifyAccountResponse,
DEFAULT_DEVICE_ID,
};
Expand Down Expand Up @@ -339,6 +339,35 @@ impl<Service: PushService> AccountManager<Service> {
Ok(())
}

pub async fn linked_devices(
&mut self,
aci_identity_store: &dyn IdentityKeyStore,
) -> Result<Vec<DeviceInfo>, ServiceError> {
let device_infos = self.service.devices().await?;
let aci_identity_keypair =
aci_identity_store.get_identity_key_pair().await?;

device_infos
.into_iter()
.map(|i| {
Ok(DeviceInfo {
id: i.id,
name: i
.name
.map(|s| {
decrypt_device_name_from_device_info(
&s,
&aci_identity_keypair,
)
})
.transpose()?,
created: i.created,
last_seen: i.last_seen,
})
})
.collect()
}

pub async fn register_account<
R: rand::Rng + rand::CryptoRng,
Aci: PreKeysStore + IdentityKeyStore,
Expand Down Expand Up @@ -841,6 +870,15 @@ pub fn encrypt_device_name<R: rand::Rng + rand::CryptoRng>(
Ok(device_name)
}

fn decrypt_device_name_from_device_info(
string: &str,
aci: &IdentityKeyPair,
) -> Result<String, ServiceError> {
let data = BASE64_RELAXED.decode(string)?;
let name = DeviceName::decode(&*data)?;
Ok(crate::decrypt_device_name(&aci.private_key(), &name)?)
}

pub fn decrypt_device_name(
private_key: &PrivateKey,
device_name: &DeviceName,
Expand Down

0 comments on commit e873d07

Please sign in to comment.