-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.rs
95 lines (82 loc) · 3.35 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::sync::{Arc, RwLock};
use bitwarden_crypto::KeyStore;
use reqwest::header::{self, HeaderValue};
use super::internal::InternalClient;
#[cfg(feature = "internal")]
use crate::client::flags::Flags;
use crate::client::{
client_settings::ClientSettings,
data_store::DataStoreMap,
internal::{ApiConfigurations, Tokens},
};
/// The main struct to interact with the Bitwarden SDK.
#[derive(Debug, Clone)]
pub struct Client {
// Important: The [`Client`] struct requires its `Clone` implementation to return an owned
// reference to the same instance. This is required to properly use the FFI API, where we can't
// just use normal Rust references effectively. For this to happen, any mutable state needs
// to be behind an Arc, ideally as part of the existing [`InternalClient`] struct.
#[doc(hidden)]
pub internal: Arc<InternalClient>,
}
impl Client {
pub fn new(settings_input: Option<ClientSettings>) -> Self {
let settings = settings_input.unwrap_or_default();
fn new_client_builder() -> reqwest::ClientBuilder {
#[allow(unused_mut)]
let mut client_builder = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))]
{
use rustls::ClientConfig;
use rustls_platform_verifier::ConfigVerifierExt;
client_builder =
client_builder.use_preconfigured_tls(ClientConfig::with_platform_verifier());
}
client_builder
}
let external_client = new_client_builder().build().expect("Build should not fail");
let mut headers = header::HeaderMap::new();
headers.append(
"Device-Type",
HeaderValue::from_str(&(settings.device_type as u8).to_string())
.expect("All numbers are valid ASCII"),
);
let client_builder = new_client_builder().default_headers(headers);
let client = client_builder.build().expect("Build should not fail");
let identity = bitwarden_api_identity::apis::configuration::Configuration {
base_path: settings.identity_url,
user_agent: Some(settings.user_agent.clone()),
client: client.clone(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
api_key: None,
};
let api = bitwarden_api_api::apis::configuration::Configuration {
base_path: settings.api_url,
user_agent: Some(settings.user_agent),
client,
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
api_key: None,
};
Self {
internal: Arc::new(InternalClient {
tokens: RwLock::new(Tokens::default()),
login_method: RwLock::new(None),
#[cfg(feature = "internal")]
flags: RwLock::new(Flags::default()),
__api_configurations: RwLock::new(Arc::new(ApiConfigurations {
identity,
api,
device_type: settings.device_type,
})),
external_client,
key_store: KeyStore::default(),
#[cfg(feature = "internal")]
data_store_map: RwLock::new(DataStoreMap::default()),
}),
}
}
}