Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bitwarden/sdk into ps/cargo-workspace
Browse files Browse the repository at this point in the history
# Conflicts:
#	crates/bitwarden/Cargo.toml
  • Loading branch information
Hinton committed Mar 14, 2024
2 parents 1bde476 + dd440ba commit b0977b5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/bitwarden-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ keywords.workspace = true
[features]
default = []

mobile = ["uniffi"]
mobile = ["dep:uniffi"] # Mobile-specific features

[dependencies]
aes = { version = ">=0.8.2, <0.9", features = ["zeroize"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-generators/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ license-file.workspace = true
keywords.workspace = true

[features]
mobile = ["uniffi"] # Mobile-specific features
mobile = ["dep:uniffi"] # Mobile-specific features

[dependencies]
bitwarden-crypto = { workspace = true }
Expand Down
11 changes: 7 additions & 4 deletions crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ license-file.workspace = true
default = ["secrets"]

secrets = [] # Secrets manager API
internal = [] # Internal testing methods
internal = [
"dep:bitwarden-exporters",
"dep:bitwarden-generators",
] # Internal testing methods
mobile = [
"uniffi",
"internal",
"dep:uniffi",
"bitwarden-crypto/mobile",
"bitwarden-generators/mobile",
] # Mobile-specific features
Expand All @@ -32,8 +35,8 @@ base64 = ">=0.21.2, <0.22"
bitwarden-api-api = { workspace = true }
bitwarden-api-identity = { workspace = true }
bitwarden-crypto = { workspace = true }
bitwarden-exporters = { workspace = true }
bitwarden-generators = { workspace = true }
bitwarden-exporters = { workspace = true, optional = true }
bitwarden-generators = { workspace = true, optional = true }
chrono = { version = ">=0.4.26, <0.5", features = [
"clock",
"serde",
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{borrow::Cow, fmt::Debug};

use bitwarden_api_api::apis::Error as ApiError;
use bitwarden_api_identity::apis::Error as IdentityError;
#[cfg(feature = "internal")]
use bitwarden_exporters::ExportError;
#[cfg(feature = "internal")]
use bitwarden_generators::{PassphraseError, PasswordError, UsernameError};
use reqwest::StatusCode;
use thiserror::Error;
Expand Down Expand Up @@ -52,13 +54,17 @@ pub enum Error {
InvalidStateFile,

// Generators
#[cfg(feature = "internal")]
#[error(transparent)]
UsernameError(#[from] UsernameError),
#[cfg(feature = "internal")]
#[error(transparent)]
PassphraseError(#[from] PassphraseError),
#[cfg(feature = "internal")]
#[error(transparent)]
PasswordError(#[from] PasswordError),

#[cfg(feature = "internal")]
#[error(transparent)]
ExportError(#[from] ExportError),

Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub use client::Client;
#[doc = include_str!("../README.md")]
mod readme {}

#[cfg(feature = "internal")]
pub mod generators {
pub use bitwarden_generators::{
PassphraseGeneratorRequest, PasswordGeneratorRequest, UsernameGeneratorRequest,
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden/src/tool/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ mod tests {
uris: None,
totp: None,
autofill_on_page_load: None,
fido2_credentials: None,
}),
id: "fd411a1a-fec8-4070-985d-0e6560860e69".parse().ok(),
organization_id: None,
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden/src/vault/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ mod tests {
uris: None,
totp: None,
autofill_on_page_load: None,
fido2_credentials: None,
}),
id: "fd411a1a-fec8-4070-985d-0e6560860e69".parse().ok(),
organization_id: None,
Expand Down
56 changes: 56 additions & 0 deletions crates/bitwarden/src/vault/cipher/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ pub struct LoginUriView {
pub r#match: Option<UriMatchType>,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "mobile", derive(uniffi::Record))]
pub struct Fido2Credential {
pub credential_id: EncString,
pub key_type: EncString,
pub key_algorithm: EncString,
pub key_curve: EncString,
pub key_value: EncString,
pub rp_id: EncString,
pub user_handle: Option<EncString>,
pub user_name: Option<EncString>,
pub counter: EncString,
pub rp_name: Option<EncString>,
pub user_display_name: Option<EncString>,
pub discoverable: EncString,
pub creation_date: DateTime<Utc>,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "mobile", derive(uniffi::Record))]
Expand All @@ -49,6 +68,8 @@ pub struct Login {
pub uris: Option<Vec<LoginUri>>,
pub totp: Option<EncString>,
pub autofill_on_page_load: Option<bool>,

pub fido2_credentials: Option<Vec<Fido2Credential>>,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand All @@ -62,6 +83,9 @@ pub struct LoginView {
pub uris: Option<Vec<LoginUriView>>,
pub totp: Option<String>,
pub autofill_on_page_load: Option<bool>,

// TODO: Remove this once the SDK supports state
pub fido2_credentials: Option<Vec<Fido2Credential>>,
}

impl KeyEncryptable<SymmetricCryptoKey, LoginUri> for LoginUriView {
Expand All @@ -82,6 +106,7 @@ impl KeyEncryptable<SymmetricCryptoKey, Login> for LoginView {
uris: self.uris.encrypt_with_key(key)?,
totp: self.totp.encrypt_with_key(key)?,
autofill_on_page_load: self.autofill_on_page_load,
fido2_credentials: self.fido2_credentials,
})
}
}
Expand All @@ -104,6 +129,7 @@ impl KeyDecryptable<SymmetricCryptoKey, LoginView> for Login {
uris: self.uris.decrypt_with_key(key).ok().flatten(),
totp: self.totp.decrypt_with_key(key).ok().flatten(),
autofill_on_page_load: self.autofill_on_page_load,
fido2_credentials: self.fido2_credentials.clone(),
})
}
}
Expand All @@ -125,6 +151,10 @@ impl TryFrom<CipherLoginModel> for Login {
.transpose()?,
totp: EncString::try_from_optional(login.totp)?,
autofill_on_page_load: login.autofill_on_page_load,
fido2_credentials: login
.fido2_credentials
.map(|v| v.into_iter().map(|c| c.try_into()).collect())
.transpose()?,
})
}
}
Expand Down Expand Up @@ -152,3 +182,29 @@ impl From<bitwarden_api_api::models::UriMatchType> for UriMatchType {
}
}
}

impl TryFrom<bitwarden_api_api::models::CipherFido2CredentialModel> for Fido2Credential {
type Error = Error;

fn try_from(value: bitwarden_api_api::models::CipherFido2CredentialModel) -> Result<Self> {
Ok(Self {
credential_id: value.credential_id.ok_or(Error::MissingFields)?.parse()?,
key_type: value.key_type.ok_or(Error::MissingFields)?.parse()?,
key_algorithm: value.key_algorithm.ok_or(Error::MissingFields)?.parse()?,
key_curve: value.key_curve.ok_or(Error::MissingFields)?.parse()?,
key_value: value.key_value.ok_or(Error::MissingFields)?.parse()?,
rp_id: value.rp_id.ok_or(Error::MissingFields)?.parse()?,
user_handle: EncString::try_from_optional(value.user_handle)
.ok()
.flatten(),
user_name: EncString::try_from_optional(value.user_name).ok().flatten(),
counter: value.counter.ok_or(Error::MissingFields)?.parse()?,
rp_name: EncString::try_from_optional(value.rp_name).ok().flatten(),
user_display_name: EncString::try_from_optional(value.user_display_name)
.ok()
.flatten(),
discoverable: value.discoverable.ok_or(Error::MissingFields)?.parse()?,
creation_date: value.creation_date.parse().unwrap(),
})
}
}

0 comments on commit b0977b5

Please sign in to comment.