Skip to content

Commit

Permalink
Allow for devices to register as a keyserver
Browse files Browse the repository at this point in the history
Summary:
Previously, all devices were being registered as
a "client" regardless.

https://linear.app/comm/issue/ENG-4521

Depends on D8627

Test Plan:
Integration tests

Register keyserver locally, and ensure it displays as a "keyserver" deviceType.

Reviewers: bartek, varun

Reviewed By: bartek

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D8642
  • Loading branch information
jonringer-comm committed Aug 14, 2023
1 parent 333bde1 commit c996605
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub async fn login_user(
}),
onetime_content_prekeys: content_one_time_keys,
onetime_notif_prekeys: notif_one_time_keys,
device_type: DeviceType::Keyserver.into(),
}),
};

Expand Down
4 changes: 2 additions & 2 deletions keyserver/addons/rust-node-addon/src/identity_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub mod identity_client {

use identity_client::identity_client_service_client::IdentityClientServiceClient;
use identity_client::{
AddReservedUsernamesRequest, DeviceKeyUpload, IdentityKeyInfo, PreKey,
RegistrationFinishRequest, RegistrationStartRequest,
AddReservedUsernamesRequest, DeviceKeyUpload, DeviceType, IdentityKeyInfo,
PreKey, RegistrationFinishRequest, RegistrationStartRequest,
RemoveReservedUsernameRequest,
};
use lazy_static::lazy_static;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub async fn register_user(
}),
onetime_content_prekeys: content_one_time_keys,
onetime_notif_prekeys: notif_one_time_keys,
device_type: DeviceType::Keyserver.into(),
};
let registration_start_request = Request::new(RegistrationStartRequest {
opaque_registration_request,
Expand Down
5 changes: 4 additions & 1 deletion native/native_rust_library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod identity {
use crypto_tools::generate_device_id;
use identity::identity_client_service_client::IdentityClientServiceClient;
use identity::{
DeviceKeyUpload, IdentityKeyInfo, OpaqueLoginFinishRequest,
DeviceKeyUpload, DeviceType, IdentityKeyInfo, OpaqueLoginFinishRequest,
OpaqueLoginStartRequest, PreKey, RegistrationFinishRequest,
RegistrationStartRequest, WalletLoginRequest,
};
Expand Down Expand Up @@ -231,6 +231,7 @@ async fn register_user_helper(
}),
onetime_content_prekeys: password_user_info.content_onetime_keys,
onetime_notif_prekeys: password_user_info.notif_onetime_keys,
device_type: DeviceType::Native.into(),
}),
};

Expand Down Expand Up @@ -321,6 +322,7 @@ async fn login_password_user_helper(
}),
onetime_content_prekeys: password_user_info.content_onetime_keys,
onetime_notif_prekeys: password_user_info.notif_onetime_keys,
device_type: DeviceType::Native.into(),
}),
};

Expand Down Expand Up @@ -420,6 +422,7 @@ async fn login_wallet_user_helper(
}),
onetime_content_prekeys: wallet_user_info.content_onetime_keys,
onetime_notif_prekeys: wallet_user_info.notif_onetime_keys,
device_type: DeviceType::Native.into(),
}),
};

Expand Down
4 changes: 3 additions & 1 deletion services/commtest/src/identity/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ mod proto {
}
use proto::{
identity_client_service_client::IdentityClientServiceClient, DeviceKeyUpload,
IdentityKeyInfo, PreKey, RegistrationFinishRequest, RegistrationStartRequest,
DeviceType, IdentityKeyInfo, PreKey, RegistrationFinishRequest,
RegistrationStartRequest,
};

pub struct DeviceInfo {
Expand Down Expand Up @@ -50,6 +51,7 @@ pub async fn create_device() -> DeviceInfo {
}),
onetime_content_prekeys: Vec::new(),
onetime_notif_prekeys: Vec::new(),
device_type: DeviceType::Keyserver.into(),
}),
};

Expand Down
15 changes: 15 additions & 0 deletions services/identity/src/client_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod client_proto {

use std::str::FromStr;

use crate::database::{self, Device};
use crate::error::Error as DBError;
use crate::{
client_service::client_proto::{
Expand Down Expand Up @@ -80,6 +81,7 @@ pub struct FlattenedDeviceKeyUpload {
pub notif_prekey: String,
pub notif_prekey_signature: String,
pub notif_onetime_keys: Vec<String>,
pub device_type: database::Device,
}

#[derive(derive_more::Constructor)]
Expand Down Expand Up @@ -139,6 +141,7 @@ impl IdentityClientService for ClientService {
}),
onetime_content_prekeys,
onetime_notif_prekeys,
device_type,
}),
} = message
{
Expand All @@ -160,6 +163,8 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_onetime_keys: onetime_notif_prekeys,
device_type: Device::try_from(device_type)
.map_err(handle_db_error)?,
},
};
let session_id = generate_uuid();
Expand Down Expand Up @@ -238,6 +243,7 @@ impl IdentityClientService for ClientService {
}),
onetime_content_prekeys,
onetime_notif_prekeys,
device_type,
}),
..
} = message
Expand All @@ -260,8 +266,11 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_onetime_keys: onetime_notif_prekeys,
device_type: Device::try_from(device_type)
.map_err(handle_db_error)?,
},
};

let session_id = generate_uuid();
self
.cache
Expand Down Expand Up @@ -470,6 +479,7 @@ impl IdentityClientService for ClientService {
}),
onetime_content_prekeys,
onetime_notif_prekeys,
device_type,
}),
} = message
{
Expand Down Expand Up @@ -498,6 +508,8 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_onetime_keys: onetime_notif_prekeys,
device_type: Device::try_from(device_type)
.map_err(handle_db_error)?,
},
};
let session_id = generate_uuid();
Expand Down Expand Up @@ -602,6 +614,7 @@ impl IdentityClientService for ClientService {
}),
onetime_content_prekeys,
onetime_notif_prekeys,
device_type,
}),
} = message
{
Expand All @@ -618,6 +631,8 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_onetime_keys: onetime_notif_prekeys,
device_type: Device::try_from(device_type)
.map_err(handle_db_error)?,
},
social_proof,
)
Expand Down
30 changes: 26 additions & 4 deletions services/identity/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,38 @@ impl FromStr for KeyPayload {
}
}

#[derive(Clone, Copy)]
pub enum Device {
Client,
Keyserver,
// Numeric values should match the protobuf definition
Keyserver = 0,
Native,
Web,
}

impl TryFrom<i32> for Device {
type Error = crate::error::Error;

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
0 => Ok(Device::Keyserver),
1 => Ok(Device::Native),
2 => Ok(Device::Web),
_ => Err(Error::Attribute(DBItemError {
attribute_name: USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME
.to_string(),
attribute_value: Some(AttributeValue::N(value.to_string())),
attribute_error: DBItemAttributeError::InvalidValue,
})),
}
}
}

impl Display for Device {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self {
Device::Client => write!(f, "client"),
Device::Keyserver => write!(f, "keyserver"),
Device::Native => write!(f, "native"),
Device::Web => write!(f, "web"),
}
}
}
Expand Down Expand Up @@ -1001,7 +1023,7 @@ fn create_device_info(
let mut device_info = HashMap::from([
(
USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME.to_string(),
AttributeValue::S(Device::Client.to_string()),
AttributeValue::S(flattened_device_key_upload.device_type.to_string()),
),
(
USERS_TABLE_DEVICES_MAP_KEY_PAYLOAD_ATTRIBUTE_NAME.to_string(),
Expand Down
2 changes: 2 additions & 0 deletions services/identity/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ pub enum DBItemAttributeError {
IncorrectType,
#[display(...)]
InvalidTimestamp(chrono::ParseError),
#[display(...)]
InvalidValue,
}
7 changes: 7 additions & 0 deletions shared/protos/identity_client.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,20 @@ message IdentityKeyInfo {
// One-time Prekeys are "consumed" after first use, so many need to
// be provide to avoid exhausting them.

enum DeviceType {
Keyserver = 0;
Native = 1;
Web = 2;
}

// Bundle of information needed for creating an initial message using X3DH
message DeviceKeyUpload {
IdentityKeyInfo deviceKeyInfo = 1;
PreKey contentUpload = 2;
PreKey notifUpload = 3;
repeated string onetimeContentPrekeys = 4;
repeated string onetimeNotifPrekeys = 5;
DeviceType deviceType = 6;
}

// Request for registering a new user
Expand Down

0 comments on commit c996605

Please sign in to comment.