Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bitwarden_license/bitwarden-sm/src/client_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mod tests {
api_url: format!("http://{}/api", server.address()),
user_agent: "Bitwarden Rust-SDK [TEST]".into(),
device_type: DeviceType::SDK,
bitwarden_client_version: None,
};

(server, Client::new(Some(settings)))
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-auth/src/send_access/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ mod tests {
api_url: format!("http://{}/api", mock_server.address()),
user_agent: "Bitwarden Rust-SDK [TEST]".into(),
device_type: DeviceType::SDK,
bitwarden_client_version: None,
};
let core_client = CoreClient::new(Some(settings));
core_client.auth_new().send_access()
Expand Down
18 changes: 17 additions & 1 deletion crates/bitwarden-core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::internal::InternalClient;
#[cfg(feature = "internal")]
use crate::client::flags::Flags;
use crate::client::{
client_settings::ClientSettings,
client_settings::{ClientName, ClientSettings},
internal::{ApiConfigurations, ClientManagedTokens, SdkManagedTokens, Tokens},
};

Expand Down Expand Up @@ -72,6 +72,22 @@ impl Client {
HeaderValue::from_str(&(settings.device_type as u8).to_string())
.expect("All numbers are valid ASCII"),
);

if let Some(client_type) = Into::<Option<ClientName>>::into(settings.device_type) {
headers.append(
"Bitwarden-Client-Name",
HeaderValue::from_str(&client_type.to_string())
.expect("All ASCII strings are valid header values"),
);
}

if let Some(version) = &settings.bitwarden_client_version {
headers.append(
"Bitwarden-Client-Version",
HeaderValue::from_str(version).expect("Version should be a valid header value"),
);
}

let client_builder = new_client_builder().default_headers(headers);

let client = client_builder.build().expect("Build should not fail");
Expand Down
67 changes: 67 additions & 0 deletions crates/bitwarden-core/src/client/client_settings.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand All @@ -13,6 +15,7 @@ use serde::{Deserialize, Serialize};
/// api_url: "https://api.bitwarden.com".to_string(),
/// user_agent: "Bitwarden Rust-SDK".to_string(),
/// device_type: DeviceType::SDK,
/// bitwarden_client_version: None,
/// };
/// let default = ClientSettings::default();
/// ```
Expand All @@ -33,6 +36,8 @@ pub struct ClientSettings {
pub user_agent: String,
/// Device type to send to Bitwarden. Defaults to SDK
pub device_type: DeviceType,
/// Bitwarden Client Version to send to Bitwarden.
pub bitwarden_client_version: Option<String>,
}

impl Default for ClientSettings {
Expand All @@ -42,6 +47,7 @@ impl Default for ClientSettings {
api_url: "https://api.bitwarden.com".into(),
user_agent: "Bitwarden Rust-SDK".into(),
device_type: DeviceType::SDK,
bitwarden_client_version: None,
}
}
}
Expand Down Expand Up @@ -81,4 +87,65 @@ pub enum DeviceType {
WindowsCLI = 23,
MacOsCLI = 24,
LinuxCLI = 25,
DuckDuckGoBrowser = 26,
}

#[derive(Copy, Clone, Debug)]
pub(crate) enum ClientName {
Web,
Browser,
Desktop,
Mobile,
Cli,
}

impl From<DeviceType> for Option<ClientName> {
fn from(device_type: DeviceType) -> Self {
match device_type {
DeviceType::Android | DeviceType::AndroidAmazon | DeviceType::iOS => {
Some(ClientName::Mobile)
}

DeviceType::ChromeBrowser
| DeviceType::FirefoxBrowser
| DeviceType::OperaBrowser
| DeviceType::EdgeBrowser
| DeviceType::IEBrowser
| DeviceType::SafariBrowser
| DeviceType::VivaldiBrowser
| DeviceType::DuckDuckGoBrowser
| DeviceType::UnknownBrowser => Some(ClientName::Web),

DeviceType::ChromeExtension
| DeviceType::FirefoxExtension
| DeviceType::OperaExtension
| DeviceType::EdgeExtension
| DeviceType::VivaldiExtension
| DeviceType::SafariExtension => Some(ClientName::Browser),

DeviceType::LinuxDesktop
| DeviceType::MacOsDesktop
| DeviceType::WindowsDesktop
| DeviceType::UWP => Some(ClientName::Desktop),

DeviceType::WindowsCLI | DeviceType::MacOsCLI | DeviceType::LinuxCLI => {
Some(ClientName::Cli)
}

DeviceType::SDK | DeviceType::Server => None,
}
}
}

impl fmt::Display for ClientName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
ClientName::Web => "web",
ClientName::Browser => "browser",
ClientName::Desktop => "desktop",
ClientName::Mobile => "mobile",
ClientName::Cli => "cli",
};
write!(f, "{}", s)
}
}