bitwarden_core/client/
client_settings.rs

1use std::fmt;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6/// Basic client behavior settings. These settings specify the various targets and behavior of the
7/// Bitwarden Client. They are optional and uneditable once the client is initialized.
8///
9/// Defaults to
10///
11/// ```
12/// # use bitwarden_core::{ClientSettings, DeviceType};
13/// let settings = ClientSettings {
14///     identity_url: "https://identity.bitwarden.com".to_string(),
15///     api_url: "https://api.bitwarden.com".to_string(),
16///     user_agent: "Bitwarden Rust-SDK".to_string(),
17///     device_type: DeviceType::SDK,
18///     bitwarden_client_version: None,
19/// };
20/// let default = ClientSettings::default();
21/// ```
22#[derive(Serialize, Deserialize, Debug, JsonSchema)]
23#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
24#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
25#[cfg_attr(
26    feature = "wasm",
27    derive(tsify::Tsify),
28    tsify(into_wasm_abi, from_wasm_abi)
29)]
30pub struct ClientSettings {
31    /// The identity url of the targeted Bitwarden instance. Defaults to `https://identity.bitwarden.com`
32    pub identity_url: String,
33    /// The api url of the targeted Bitwarden instance. Defaults to `https://api.bitwarden.com`
34    pub api_url: String,
35    /// The user_agent to sent to Bitwarden. Defaults to `Bitwarden Rust-SDK`
36    pub user_agent: String,
37    /// Device type to send to Bitwarden. Defaults to SDK
38    pub device_type: DeviceType,
39    /// Bitwarden Client Version to send to Bitwarden.
40    pub bitwarden_client_version: Option<String>,
41}
42
43impl Default for ClientSettings {
44    fn default() -> Self {
45        Self {
46            identity_url: "https://identity.bitwarden.com".into(),
47            api_url: "https://api.bitwarden.com".into(),
48            user_agent: "Bitwarden Rust-SDK".into(),
49            device_type: DeviceType::SDK,
50            bitwarden_client_version: None,
51        }
52    }
53}
54
55#[allow(non_camel_case_types, missing_docs)]
56#[derive(Serialize, Deserialize, Copy, Clone, Debug, JsonSchema)]
57#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
58#[cfg_attr(
59    feature = "wasm",
60    derive(tsify::Tsify),
61    tsify(into_wasm_abi, from_wasm_abi)
62)]
63pub enum DeviceType {
64    Android = 0,
65    iOS = 1,
66    ChromeExtension = 2,
67    FirefoxExtension = 3,
68    OperaExtension = 4,
69    EdgeExtension = 5,
70    WindowsDesktop = 6,
71    MacOsDesktop = 7,
72    LinuxDesktop = 8,
73    ChromeBrowser = 9,
74    FirefoxBrowser = 10,
75    OperaBrowser = 11,
76    EdgeBrowser = 12,
77    IEBrowser = 13,
78    UnknownBrowser = 14,
79    AndroidAmazon = 15,
80    UWP = 16,
81    SafariBrowser = 17,
82    VivaldiBrowser = 18,
83    VivaldiExtension = 19,
84    SafariExtension = 20,
85    SDK = 21,
86    Server = 22,
87    WindowsCLI = 23,
88    MacOsCLI = 24,
89    LinuxCLI = 25,
90    DuckDuckGoBrowser = 26,
91}
92
93#[derive(Copy, Clone, Debug)]
94pub(crate) enum ClientName {
95    Web,
96    Browser,
97    Desktop,
98    Mobile,
99    Cli,
100}
101
102impl From<DeviceType> for Option<ClientName> {
103    fn from(device_type: DeviceType) -> Self {
104        match device_type {
105            DeviceType::Android | DeviceType::AndroidAmazon | DeviceType::iOS => {
106                Some(ClientName::Mobile)
107            }
108
109            DeviceType::ChromeBrowser
110            | DeviceType::FirefoxBrowser
111            | DeviceType::OperaBrowser
112            | DeviceType::EdgeBrowser
113            | DeviceType::IEBrowser
114            | DeviceType::SafariBrowser
115            | DeviceType::VivaldiBrowser
116            | DeviceType::DuckDuckGoBrowser
117            | DeviceType::UnknownBrowser => Some(ClientName::Web),
118
119            DeviceType::ChromeExtension
120            | DeviceType::FirefoxExtension
121            | DeviceType::OperaExtension
122            | DeviceType::EdgeExtension
123            | DeviceType::VivaldiExtension
124            | DeviceType::SafariExtension => Some(ClientName::Browser),
125
126            DeviceType::LinuxDesktop
127            | DeviceType::MacOsDesktop
128            | DeviceType::WindowsDesktop
129            | DeviceType::UWP => Some(ClientName::Desktop),
130
131            DeviceType::WindowsCLI | DeviceType::MacOsCLI | DeviceType::LinuxCLI => {
132                Some(ClientName::Cli)
133            }
134
135            DeviceType::SDK | DeviceType::Server => None,
136        }
137    }
138}
139
140impl fmt::Display for ClientName {
141    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142        let s = match self {
143            ClientName::Web => "web",
144            ClientName::Browser => "browser",
145            ClientName::Desktop => "desktop",
146            ClientName::Mobile => "mobile",
147            ClientName::Cli => "cli",
148        };
149        write!(f, "{}", s)
150    }
151}