Skip to content

Commit

Permalink
Add more docs to bitwarden_core
Browse files Browse the repository at this point in the history
  • Loading branch information
justindbaur committed Nov 26, 2024
1 parent 130ac6f commit c5bf1c7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/bitwarden-core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ pub struct Client {
}

impl Client {
/// Constructs a new `Client` with the given `settings_input`.
///
/// # Examples
/// ```rust
/// use bitwarden_core::{Client, ClientSettings, DeviceType};
///
/// let client = Client::new(Some(ClientSettings {
/// identity_url: "https://identity.bitwarden.com".to_owned(),
/// api_url: "https://api.bitwarden.com".to_owned(),
/// user_agent: "Bitwarden Rust-SDK".to_owned(),
/// device_type: DeviceType::ChromeBrowser,
/// }));
/// ```
pub fn new(settings_input: Option<ClientSettings>) -> Self {
let settings = settings_input.unwrap_or_default();

Expand Down
10 changes: 10 additions & 0 deletions crates/bitwarden-core/src/client/encryption_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ pub enum EncryptionSettingsError {
MissingPrivateKey,
}

/// A struct containing the core keys of a Bitwarden user.
#[derive(Clone)]
pub struct EncryptionSettings {
/// The users symmetric key, stored on the server after being
/// encrypted with another key.
user_key: SymmetricCryptoKey,
/// The users private key, stored on the server
/// encrypted with the users symmetric key.
pub(crate) private_key: Option<AsymmetricCryptoKey>,
/// A map of the users organization keys with the key being
/// the ID of the organization and the value being the symmetric
/// key. This map may be empty if the user is not a part of
/// any organizations. These keys are stored on the server
/// encrypted with the users private key.
org_keys: HashMap<Uuid, SymmetricCryptoKey>,
}

Expand Down
2 changes: 2 additions & 0 deletions crates/bitwarden-core/src/client/flags.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// A struct containing fields of all known feature flags and their values.
#[derive(Debug, Default, Clone, serde::Deserialize)]
pub struct Flags {
/// A `bool` indicating whether or not cipher key encryption is enabled.
#[serde(default, rename = "enableCipherKeyEncryption")]
pub enable_cipher_key_encryption: bool,
}
Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden-core/src/client/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub(crate) struct Tokens {
pub(crate) refresh_token: Option<String>,
}

/// A struct contains various internal actions. Everything on this type
/// should be considered unstable and subject to change at any time. Use
/// with caution.
#[derive(Debug)]
pub struct InternalClient {
pub(crate) tokens: RwLock<Tokens>,
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden-core/src/client/test_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use crate::{
};

impl Client {
/// Creates a client initialized with a hard coded test account.
/// Useful for examples.
pub async fn test_account() -> Self {
Self::init_test_account(test_bitwarden_com_account()).await
}

Check warning on line 19 in crates/bitwarden-core/src/client/test_accounts.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/client/test_accounts.rs#L17-L19

Added lines #L17 - L19 were not covered by tests

pub async fn init_test_account(account: TestAccount) -> Self {
let client = Client::new(None);

Expand Down
40 changes: 40 additions & 0 deletions crates/bitwarden-core/src/platform/client_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,54 @@ use super::{
};
use crate::{error::Result, Client};

/// A struct containing platform utilities.
pub struct ClientPlatform<'a> {
pub(crate) client: &'a Client,
}

impl<'a> ClientPlatform<'a> {
/// Will generate a fingerprint based on the `input`. Given the same `input` This
/// method will result in the exact same output.
///
/// # Examples
/// ```rust
/// use bitwarden_core::{Client, platform::FingerprintRequest};
///
/// async fn test() {
/// let client = Client::test_account().await;
/// let fingerprint_response = client.platform()
/// .fingerprint(&FingerprintRequest {
/// fingerprint_material: "my_material".to_owned(),
/// public_key: "...public key...".to_owned(),
/// })
/// .unwrap();
///
/// println!("{}", fingerprint_response.fingerprint);
/// }
/// ```
pub fn fingerprint(&self, input: &FingerprintRequest) -> Result<FingerprintResponse> {
generate_fingerprint(input)
}

/// Will generate a fingerprint based on the given `fingerprint_material`
/// and the users public key. Given the same `fingerprint_material` and
/// the same user. This method will result in the exact same output.
///
/// The returned fingerprint is a string of 5 words seperated by hyphens.
///
/// # Examples
/// ```rust
/// use bitwarden_core::Client;
///
/// async fn test() {
/// let client = Client::test_account().await;
/// let fingerprint = client.platform()
/// .user_fingerprint("my_material".to_owned())
/// .unwrap();
///
/// assert_eq!(fingerprint, "dreamland-desecrate-corrosive-ecard-retry");
/// }
/// ```
pub fn user_fingerprint(self, fingerprint_material: String) -> Result<String> {
generate_user_fingerprint(self.client, fingerprint_material)
}
Expand All @@ -27,6 +66,7 @@ impl<'a> ClientPlatform<'a> {
}

impl<'a> Client {
/// Retrieves a [`ClientPlatform`] for accessing Platform APIs.
pub fn platform(&'a self) -> ClientPlatform<'a> {
ClientPlatform { client: self }
}
Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden-core/src/platform/generate_fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};

use crate::error::Result;

/// A struct containing the parts for making a fingerprint request.
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
Expand All @@ -16,9 +17,11 @@ pub struct FingerprintRequest {
pub public_key: String,
}

/// A struct containing the details of a successful request to generate a fingerprint.
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FingerprintResponse {
/// A `String` containing 5 words seperated by hyphens.
pub fingerprint: String,
}

Expand Down

0 comments on commit c5bf1c7

Please sign in to comment.