Skip to content

Commit

Permalink
Merge pull request #300 from Schmiddiii/decrypt-device-info
Browse files Browse the repository at this point in the history
Add AccountManager::linked_devices
  • Loading branch information
rubdos authored Jun 7, 2024
2 parents 26c036e + e873d07 commit 1e04a65
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)?)

Check warning on line 879 in libsignal-service/src/account_manager.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> libsignal-service/src/account_manager.rs:879:35 | 879 | Ok(crate::decrypt_device_name(&aci.private_key(), &name)?) | ^^^^^^^^^^^^^^^^^^ help: change this to: `aci.private_key()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default

Check warning on line 879 in libsignal-service/src/account_manager.rs

View workflow job for this annotation

GitHub Actions / clippy

question mark operator is useless here

warning: question mark operator is useless here --> libsignal-service/src/account_manager.rs:879:5 | 879 | Ok(crate::decrypt_device_name(&aci.private_key(), &name)?) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `crate::decrypt_device_name(&aci.private_key(), &name)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark = note: `#[warn(clippy::needless_question_mark)]` on by default
}

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

0 comments on commit 1e04a65

Please sign in to comment.