Skip to content

Commit

Permalink
Add PushService::link_device
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Dec 17, 2023
1 parent 8f88d8e commit 61113e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
60 changes: 11 additions & 49 deletions libsignal-service/src/provisioning/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ use super::{

use crate::{
account_manager::encrypt_device_name,
configuration::Endpoint,
pre_keys::{
generate_last_resort_kyber_key, generate_signed_pre_key,
KyberPreKeyEntity, PreKeysStore, SignedPreKey,
generate_last_resort_kyber_key, generate_signed_pre_key, PreKeysStore,
},
push_service::{
HttpAuth, LinkAccountAttributes, LinkCapabilities, LinkRequest,
LinkResponse, PushService, ServiceIds,
},
push_service::{HttpAuth, HttpAuthOverride, PushService, ServiceIds},
utils::serde_base64,
};

Expand Down Expand Up @@ -208,33 +209,6 @@ impl<P: PushService> LinkingManager<P> {
)
.await?;

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct LinkRequest {
verification_code: String,
account_attributes: AccountAttributes,
aci_signed_pre_key: SignedPreKey,
pni_signed_pre_key: SignedPreKey,
aci_pq_last_resort_pre_key: KyberPreKeyEntity,
pni_pq_last_resort_pre_key: KyberPreKeyEntity,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct AccountAttributes {
fetches_messages: bool,
name: String,
registration_id: u32,
pni_registration_id: u32,
capabilities: Capabilities,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct Capabilities {
pni: bool,
}

let encrypted_device_name = base64::encode(
encrypt_device_name(
csprng,
Expand All @@ -253,11 +227,11 @@ impl<P: PushService> LinkingManager<P> {

let request = LinkRequest {
verification_code: provisioning_code,
account_attributes: AccountAttributes {
account_attributes: LinkAccountAttributes {
registration_id,
pni_registration_id,
fetches_messages: true,
capabilities: Capabilities { pni: true },
capabilities: LinkCapabilities { pni: true },
name: encrypted_device_name,
},
aci_signed_pre_key: aci_signed_pre_key.try_into()?,
Expand All @@ -268,30 +242,18 @@ impl<P: PushService> LinkingManager<P> {
.try_into()?,
};

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct LinkResponse {
#[serde(rename = "uuid")]
aci: Uuid,
pni: Uuid,
device_id: u32,
}

let LinkResponse {
aci,
pni,
device_id,
} = self
.push_service
.put_json(
Endpoint::Service,
"/v1/devices/link",
&[],
HttpAuthOverride::Identified(HttpAuth {
.link_device(
&request,
HttpAuth {
username: phone_number.to_string(),
password: self.password.clone(),
}),
&request,
},
)
.await?;

Expand Down
54 changes: 53 additions & 1 deletion libsignal-service/src/push_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{
envelope::*,
groups_v2::GroupDecodingError,
pre_keys::{
KyberPreKeyEntity, PreKeyEntity, PreKeyState, SignedPreKeyEntity,
KyberPreKeyEntity, PreKeyEntity, PreKeyState, SignedPreKey,
SignedPreKeyEntity,
},
profile_cipher::ProfileCipherError,
proto::{attachment_pointer::AttachmentIdentifier, AttachmentPointer},
Expand Down Expand Up @@ -381,6 +382,42 @@ pub struct StaleDevices {
pub stale_devices: Vec<u32>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkRequest {
pub verification_code: String,
pub account_attributes: LinkAccountAttributes,
pub aci_signed_pre_key: SignedPreKey,
pub pni_signed_pre_key: SignedPreKey,
pub aci_pq_last_resort_pre_key: KyberPreKeyEntity,
pub pni_pq_last_resort_pre_key: KyberPreKeyEntity,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkAccountAttributes {
pub fetches_messages: bool,
pub name: String,
pub registration_id: u32,
pub pni_registration_id: u32,
pub capabilities: LinkCapabilities,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkCapabilities {
pub pni: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkResponse {
#[serde(rename = "uuid")]
pub aci: Uuid,
pub pni: Uuid,
pub device_id: u32,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignalServiceProfile {
Expand Down Expand Up @@ -954,6 +991,21 @@ pub trait PushService: MaybeSend {
Ok(SenderCertificate::deserialize(&cert.certificate)?)
}

async fn link_device(
&mut self,
link_request: &LinkRequest,
http_auth: HttpAuth,
) -> Result<LinkResponse, ServiceError> {
self.put_json(
Endpoint::Service,
"/v1/devices/link",
&[],
HttpAuthOverride::Identified(http_auth),
&link_request,

Check warning on line 1004 in libsignal-service/src/push_service.rs

View workflow job for this annotation

GitHub Actions / clippy

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> libsignal-service/src/push_service.rs:1004:13 | 1004 | &link_request, | ^^^^^^^^^^^^^ help: change this to: `link_request` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default
)
.await
}

async fn set_account_attributes(
&mut self,
attributes: AccountAttributes,
Expand Down

0 comments on commit 61113e5

Please sign in to comment.