bitwarden_core/client/
client.rs1use std::sync::{Arc, OnceLock, RwLock};
2
3use bitwarden_crypto::KeyStore;
4#[cfg(feature = "internal")]
5use bitwarden_state::registry::StateRegistry;
6use reqwest::header::{self, HeaderValue};
7
8use super::internal::InternalClient;
9#[cfg(feature = "internal")]
10use crate::client::flags::Flags;
11use crate::client::{
12 client_settings::{ClientName, ClientSettings},
13 internal::{ApiConfigurations, ClientManagedTokens, SdkManagedTokens, Tokens},
14};
15
16#[derive(Debug, Clone)]
18pub struct Client {
19 #[doc(hidden)]
24 pub internal: Arc<InternalClient>,
25}
26
27impl Client {
28 pub fn new(settings: Option<ClientSettings>) -> Self {
30 Self::new_internal(settings, Tokens::SdkManaged(SdkManagedTokens::default()))
31 }
32
33 pub fn new_with_client_tokens(
35 settings: Option<ClientSettings>,
36 tokens: Arc<dyn ClientManagedTokens>,
37 ) -> Self {
38 Self::new_internal(settings, Tokens::ClientManaged(tokens))
39 }
40
41 fn new_internal(settings_input: Option<ClientSettings>, tokens: Tokens) -> Self {
42 let settings = settings_input.unwrap_or_default();
43
44 let external_http_client = new_http_client_builder()
45 .build()
46 .expect("External HTTP Client build should not fail");
47
48 let headers = build_default_headers(&settings);
49
50 let bw_http_client = new_http_client_builder()
51 .default_headers(headers)
52 .build()
53 .expect("Bw HTTP Client build should not fail");
54
55 let bw_http_client = reqwest_middleware::ClientBuilder::new(bw_http_client).build();
56
57 let identity = bitwarden_api_identity::Configuration {
58 base_path: settings.identity_url,
59 user_agent: Some(settings.user_agent.clone()),
60 client: bw_http_client.clone(),
61 oauth_access_token: None,
62 };
63
64 let api = bitwarden_api_api::Configuration {
65 base_path: settings.api_url,
66 user_agent: Some(settings.user_agent),
67 client: bw_http_client,
68 oauth_access_token: None,
69 };
70
71 Self {
72 internal: Arc::new(InternalClient {
73 user_id: OnceLock::new(),
74 tokens: RwLock::new(tokens),
75 login_method: RwLock::new(None),
76 #[cfg(feature = "internal")]
77 flags: RwLock::new(Flags::default()),
78 __api_configurations: RwLock::new(ApiConfigurations::new(
79 identity,
80 api,
81 settings.device_type,
82 )),
83 external_http_client,
84 key_store: KeyStore::default(),
85 #[cfg(feature = "internal")]
86 security_state: RwLock::new(None),
87 #[cfg(feature = "internal")]
88 repository_map: StateRegistry::new(),
89 }),
90 }
91 }
92}
93
94fn new_http_client_builder() -> reqwest::ClientBuilder {
95 #[allow(unused_mut)]
96 let mut client_builder = reqwest::Client::builder();
97
98 #[cfg(not(target_arch = "wasm32"))]
99 {
100 use rustls::ClientConfig;
101 use rustls_platform_verifier::ConfigVerifierExt;
102 client_builder = client_builder.use_preconfigured_tls(
103 ClientConfig::with_platform_verifier().expect("Failed to create platform verifier"),
104 );
105
106 #[cfg(not(debug_assertions))]
108 {
109 client_builder = client_builder.https_only(true);
110 }
111 }
112
113 client_builder
114}
115
116fn build_default_headers(settings: &ClientSettings) -> header::HeaderMap {
118 let mut headers = header::HeaderMap::new();
119
120 if let Some(device_identifier) = &settings.device_identifier {
123 headers.append(
124 "Device-Identifier",
125 HeaderValue::from_str(device_identifier)
126 .expect("Device identifier should be a valid header value"),
127 );
128 }
129
130 if let Some(client_type) = Into::<Option<ClientName>>::into(settings.device_type) {
131 headers.append(
132 "Bitwarden-Client-Name",
133 HeaderValue::from_str(&client_type.to_string())
134 .expect("All ASCII strings are valid header values"),
135 );
136 }
137
138 if let Some(version) = &settings.bitwarden_client_version {
139 headers.append(
140 "Bitwarden-Client-Version",
141 HeaderValue::from_str(version).expect("Version should be a valid header value"),
142 );
143 }
144
145 if let Some(package_type) = &settings.bitwarden_package_type {
146 headers.append(
147 "Bitwarden-Package-Type",
148 HeaderValue::from_str(package_type)
149 .expect("Package type should be a valid header value"),
150 );
151 }
152
153 headers.append(
156 "Device-Type",
157 HeaderValue::from_str(&(settings.device_type as u8).to_string())
158 .expect("All numbers are valid ASCII"),
159 );
160
161 headers.append(
164 reqwest::header::USER_AGENT,
165 HeaderValue::from_str(&settings.user_agent)
166 .expect("User agent should be a valid header value"),
167 );
168
169 headers
170}