From 5ee45a1eabee4c67206e6ea39b709382b0b51636 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Mon, 10 Jun 2024 14:17:39 +0200 Subject: [PATCH] [PM-8301] Create bitwarden-core with macro and uniffi (#824) Extract the `require` macro and common uniffi logic to a new crate called `bitwarden-core` since it's required for extracting other bitwarden functionality. --- .github/workflows/publish-rust-crates.yml | 11 +++++ Cargo.lock | 14 ++++++- Cargo.toml | 1 + crates/bitwarden-core/Cargo.toml | 26 ++++++++++++ crates/bitwarden-core/src/error.rs | 19 +++++++++ crates/bitwarden-core/src/lib.rs | 7 ++++ crates/bitwarden-core/src/uniffi_support.rs | 41 +++++++++++++++++++ crates/bitwarden-core/uniffi.toml | 9 ++++ crates/bitwarden-crypto/Cargo.toml | 1 - crates/bitwarden-exporters/Cargo.toml | 3 -- crates/bitwarden-uniffi/Cargo.toml | 1 + crates/bitwarden-uniffi/src/uniffi_support.rs | 4 +- crates/bitwarden/Cargo.toml | 3 +- crates/bitwarden/src/admin_console/policy.rs | 3 +- .../bitwarden/src/auth/login/access_token.rs | 3 +- crates/bitwarden/src/auth/login/api_key.rs | 3 +- .../bitwarden/src/auth/login/auth_request.rs | 3 +- crates/bitwarden/src/auth/login/password.rs | 3 +- crates/bitwarden/src/error.rs | 18 +------- .../src/platform/get_user_api_key.rs | 3 +- .../src/secrets_manager/projects/delete.rs | 6 +-- .../projects/project_response.rs | 6 +-- .../src/secrets_manager/secrets/delete.rs | 6 +-- .../src/secrets_manager/secrets/list.rs | 3 +- .../secrets/secret_response.rs | 6 +-- .../src/secrets_manager/secrets/sync.rs | 7 +--- crates/bitwarden/src/tool/exporters/mod.rs | 3 +- crates/bitwarden/src/tool/send.rs | 3 +- crates/bitwarden/src/uniffi_support.rs | 33 ++------------- crates/bitwarden/src/vault/cipher/cipher.rs | 3 +- crates/bitwarden/src/vault/cipher/field.rs | 3 +- .../bitwarden/src/vault/cipher/linked_id.rs | 3 +- crates/bitwarden/src/vault/cipher/login.rs | 3 +- .../bitwarden/src/vault/cipher/secure_note.rs | 3 +- crates/bitwarden/src/vault/collection.rs | 3 +- crates/bitwarden/src/vault/domain.rs | 3 +- crates/bitwarden/src/vault/folder.rs | 3 +- crates/bitwarden/src/vault/sync.rs | 3 +- crates/bitwarden/uniffi.toml | 6 +-- .../bitwarden/myapplication/MainActivity.kt | 8 ++-- 40 files changed, 190 insertions(+), 100 deletions(-) create mode 100644 crates/bitwarden-core/Cargo.toml create mode 100644 crates/bitwarden-core/src/error.rs create mode 100644 crates/bitwarden-core/src/lib.rs create mode 100644 crates/bitwarden-core/src/uniffi_support.rs create mode 100644 crates/bitwarden-core/uniffi.toml diff --git a/.github/workflows/publish-rust-crates.yml b/.github/workflows/publish-rust-crates.yml index 399810d9b..013fcd254 100644 --- a/.github/workflows/publish-rust-crates.yml +++ b/.github/workflows/publish-rust-crates.yml @@ -29,6 +29,11 @@ on: required: true default: true type: boolean + publish_bitwarden-core: + description: "Publish bitwarden-core crate" + required: true + default: true + type: boolean publish_bitwarden-crypto: description: "Publish bitwarden-crypto crate" required: true @@ -81,6 +86,7 @@ jobs: PUBLISH_BITWARDEN: ${{ github.event.inputs.publish_bitwarden }} PUBLISH_BITWARDEN_API_API: ${{ github.event.inputs.publish_bitwarden-api-api }} PUBLISH_BITWARDEN_API_IDENTITY: ${{ github.event.inputs.publish_bitwarden-api-identity }} + PUBLISH_BITWARDEN_CORE: ${{ github.event.inputs.publish_bitwarden-core }} PUBLISH_BITWARDEN_CRYPTO: ${{ github.event.inputs.publish_bitwarden-crypto }} PUBLISH_BITWARDEN_CLI: ${{ github.event.inputs.publish_bitwarden-cli }} PUBLISH_BITWARDEN_GENERATORS: ${{ github.event.inputs.publish_bitwarden-generators }} @@ -111,6 +117,11 @@ jobs: PACKAGES_LIST="$PACKAGES_LIST bitwarden-api-identity" fi + if [[ "$PUBLISH_BITWARDEN_CORE" == "true" ]]; then + PACKAGES_COMMAND="$PACKAGES_COMMAND -p bitwarden-core" + PACKAGES_LIST="$PACKAGES_LIST bitwarden-core" + fi + if [[ "$PUBLISH_BITWARDEN_CRYPTO" == "true" ]]; then PACKAGES_COMMAND="$PACKAGES_COMMAND -p bitwarden-crypto" PACKAGES_LIST="$PACKAGES_LIST bitwarden-crypto" diff --git a/Cargo.lock b/Cargo.lock index 6864413c1..a78c2d18e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -369,9 +369,10 @@ name = "bitwarden" version = "0.5.0" dependencies = [ "async-trait", - "base64 0.22.1", + "base64 0.21.7", "bitwarden-api-api", "bitwarden-api-identity", + "bitwarden-core", "bitwarden-crypto", "bitwarden-exporters", "bitwarden-generators", @@ -448,6 +449,16 @@ dependencies = [ "supports-color", ] +[[package]] +name = "bitwarden-core" +version = "0.5.0" +dependencies = [ + "chrono", + "thiserror", + "uniffi", + "uuid", +] + [[package]] name = "bitwarden-crypto" version = "0.5.0" @@ -554,6 +565,7 @@ dependencies = [ "async-lock", "async-trait", "bitwarden", + "bitwarden-core", "bitwarden-crypto", "bitwarden-generators", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 1f32654dc..2da09446c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ bitwarden = { path = "crates/bitwarden", version = "=0.5.0" } bitwarden-api-api = { path = "crates/bitwarden-api-api", version = "=0.5.0" } bitwarden-api-identity = { path = "crates/bitwarden-api-identity", version = "=0.5.0" } bitwarden-cli = { path = "crates/bitwarden-cli", version = "=0.5.0" } +bitwarden-core = { path = "crates/bitwarden-core", version = "=0.5.0" } bitwarden-crypto = { path = "crates/bitwarden-crypto", version = "=0.5.0" } bitwarden-exporters = { path = "crates/bitwarden-exporters", version = "=0.5.0" } bitwarden-generators = { path = "crates/bitwarden-generators", version = "=0.5.0" } diff --git a/crates/bitwarden-core/Cargo.toml b/crates/bitwarden-core/Cargo.toml new file mode 100644 index 000000000..5f755020c --- /dev/null +++ b/crates/bitwarden-core/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "bitwarden-core" +description = """ +Internal crate for the bitwarden crate. Do not use. +""" + +version.workspace = true +authors.workspace = true +edition.workspace = true +rust-version.workspace = true +homepage.workspace = true +repository.workspace = true +license-file.workspace = true +keywords.workspace = true + +[features] +uniffi = ["dep:uniffi"] + +[dependencies] +chrono = { version = ">=0.4.26, <0.5", default-features = false } +uniffi = { version = "=0.27.2", optional = true } +uuid = { version = ">=1.3.3, <2.0", features = ["serde"] } +thiserror = ">=1.0.40, <2.0" + +[lints] +workspace = true diff --git a/crates/bitwarden-core/src/error.rs b/crates/bitwarden-core/src/error.rs new file mode 100644 index 000000000..e2d890ecb --- /dev/null +++ b/crates/bitwarden-core/src/error.rs @@ -0,0 +1,19 @@ +use thiserror::Error; + +#[derive(Debug, Error)] +#[error("The response received was missing a required field: {0}")] +pub struct MissingFieldError(pub &'static str); + +/// This macro is used to require that a value is present or return an error otherwise. +/// It is equivalent to using `val.ok_or(Error::MissingFields)?`, but easier to use and +/// with a more descriptive error message. +/// Note that this macro will return early from the function if the value is not present. +#[macro_export] +macro_rules! require { + ($val:expr) => { + match $val { + Some(val) => val, + None => return Err($crate::MissingFieldError(stringify!($val)).into()), + } + }; +} diff --git a/crates/bitwarden-core/src/lib.rs b/crates/bitwarden-core/src/lib.rs new file mode 100644 index 000000000..1e62111b5 --- /dev/null +++ b/crates/bitwarden-core/src/lib.rs @@ -0,0 +1,7 @@ +#[cfg(feature = "uniffi")] +uniffi::setup_scaffolding!(); +#[cfg(feature = "uniffi")] +mod uniffi_support; + +mod error; +pub use error::MissingFieldError; diff --git a/crates/bitwarden-core/src/uniffi_support.rs b/crates/bitwarden-core/src/uniffi_support.rs new file mode 100644 index 000000000..320df6cb5 --- /dev/null +++ b/crates/bitwarden-core/src/uniffi_support.rs @@ -0,0 +1,41 @@ +use uuid::Uuid; + +use crate::UniffiCustomTypeConverter; + +type DateTime = chrono::DateTime; +uniffi::custom_type!(DateTime, std::time::SystemTime); + +impl UniffiCustomTypeConverter for chrono::DateTime { + type Builtin = std::time::SystemTime; + + fn into_custom(val: Self::Builtin) -> uniffi::Result { + Ok(Self::from(val)) + } + + fn from_custom(obj: Self) -> Self::Builtin { + obj.into() + } +} + +uniffi::custom_type!(Uuid, String); + +impl UniffiCustomTypeConverter for Uuid { + type Builtin = String; + + fn into_custom(val: Self::Builtin) -> uniffi::Result { + Uuid::parse_str(val.as_str()).map_err(|e| e.into()) + } + + fn from_custom(obj: Self) -> Self::Builtin { + obj.to_string() + } +} + +// Uniffi doesn't emit unused types, this is a dummy record to ensure that the custom type +// converters are emitted +#[allow(dead_code)] +#[derive(uniffi::Record)] +struct UniffiConverterDummyRecord { + uuid: Uuid, + date: DateTime, +} diff --git a/crates/bitwarden-core/uniffi.toml b/crates/bitwarden-core/uniffi.toml new file mode 100644 index 000000000..7a804ef1d --- /dev/null +++ b/crates/bitwarden-core/uniffi.toml @@ -0,0 +1,9 @@ +[bindings.kotlin] +package_name = "com.bitwarden.core" +generate_immutable_records = true +android = true + +[bindings.swift] +ffi_module_name = "BitwardenCoreFFI" +module_name = "BitwardenCore" +generate_immutable_records = true diff --git a/crates/bitwarden-crypto/Cargo.toml b/crates/bitwarden-crypto/Cargo.toml index b15770ced..e03a8d5c1 100644 --- a/crates/bitwarden-crypto/Cargo.toml +++ b/crates/bitwarden-crypto/Cargo.toml @@ -18,7 +18,6 @@ default = [] uniffi = ["dep:uniffi"] # Uniffi bindings no-memory-hardening = [] # Disable memory hardening features -test = [] # Test methods [dependencies] aes = { version = ">=0.8.2, <0.9", features = ["zeroize"] } diff --git a/crates/bitwarden-exporters/Cargo.toml b/crates/bitwarden-exporters/Cargo.toml index 96097c628..b607879ba 100644 --- a/crates/bitwarden-exporters/Cargo.toml +++ b/crates/bitwarden-exporters/Cargo.toml @@ -28,8 +28,5 @@ serde_json = ">=1.0.96, <2.0" thiserror = ">=1.0.40, <2.0" uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4"] } -[dev-dependencies] -bitwarden-crypto = { workspace = true, features = ["test"] } - [lints] workspace = true diff --git a/crates/bitwarden-uniffi/Cargo.toml b/crates/bitwarden-uniffi/Cargo.toml index 62f9e5806..92cd1f3e9 100644 --- a/crates/bitwarden-uniffi/Cargo.toml +++ b/crates/bitwarden-uniffi/Cargo.toml @@ -21,6 +21,7 @@ bench = false async-lock = "3.3.0" async-trait = "0.1.80" bitwarden = { workspace = true, features = ["internal", "uniffi"] } +bitwarden-core = { workspace = true, features = ["uniffi"] } bitwarden-crypto = { workspace = true, features = ["uniffi"] } bitwarden-generators = { workspace = true, features = ["uniffi"] } chrono = { version = ">=0.4.26, <0.5", features = [ diff --git a/crates/bitwarden-uniffi/src/uniffi_support.rs b/crates/bitwarden-uniffi/src/uniffi_support.rs index a3422d752..74f484869 100644 --- a/crates/bitwarden-uniffi/src/uniffi_support.rs +++ b/crates/bitwarden-uniffi/src/uniffi_support.rs @@ -3,7 +3,7 @@ use uuid::Uuid; // Forward the type definitions to the main bitwarden crate type DateTime = chrono::DateTime; -uniffi::ffi_converter_forward!(DateTime, bitwarden::UniFfiTag, crate::UniFfiTag); +uniffi::ffi_converter_forward!(DateTime, bitwarden_core::UniFfiTag, crate::UniFfiTag); uniffi::ffi_converter_forward!(EncString, bitwarden::UniFfiTag, crate::UniFfiTag); uniffi::ffi_converter_forward!(AsymmetricEncString, bitwarden::UniFfiTag, crate::UniFfiTag); -uniffi::ffi_converter_forward!(Uuid, bitwarden::UniFfiTag, crate::UniFfiTag); +uniffi::ffi_converter_forward!(Uuid, bitwarden_core::UniFfiTag, crate::UniFfiTag); diff --git a/crates/bitwarden/Cargo.toml b/crates/bitwarden/Cargo.toml index 8bfafc462..1bde11717 100644 --- a/crates/bitwarden/Cargo.toml +++ b/crates/bitwarden/Cargo.toml @@ -24,6 +24,7 @@ no-memory-hardening = [ "bitwarden-crypto/no-memory-hardening", ] # Disable memory hardening features uniffi = [ + "bitwarden-core/uniffi", "bitwarden-crypto/uniffi", "bitwarden-generators/uniffi", "dep:uniffi", @@ -39,6 +40,7 @@ async-trait = ">=0.1.80, <0.2" base64 = ">=0.21.2, <0.23" bitwarden-api-api = { workspace = true } bitwarden-api-identity = { workspace = true } +bitwarden-core = { workspace = true } bitwarden-crypto = { workspace = true } bitwarden-exporters = { workspace = true, optional = true } bitwarden-generators = { workspace = true, optional = true } @@ -95,7 +97,6 @@ reqwest = { version = ">=0.12, <0.13", features = [ security-framework = { version = "=2.10" } [dev-dependencies] -bitwarden-crypto = { workspace = true, features = ["test"] } rand_chacha = "0.3.1" tokio = { version = "1.36.0", features = ["rt", "macros"] } wiremock = "0.6.0" diff --git a/crates/bitwarden/src/admin_console/policy.rs b/crates/bitwarden/src/admin_console/policy.rs index d8ed0b761..6c0a2ec65 100644 --- a/crates/bitwarden/src/admin_console/policy.rs +++ b/crates/bitwarden/src/admin_console/policy.rs @@ -1,12 +1,13 @@ use std::collections::HashMap; use bitwarden_api_api::models::PolicyResponseModel; +use bitwarden_core::require; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; use uuid::Uuid; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] pub struct Policy { diff --git a/crates/bitwarden/src/auth/login/access_token.rs b/crates/bitwarden/src/auth/login/access_token.rs index 9366e631f..a376eb0c3 100644 --- a/crates/bitwarden/src/auth/login/access_token.rs +++ b/crates/bitwarden/src/auth/login/access_token.rs @@ -1,6 +1,7 @@ use std::path::{Path, PathBuf}; use base64::{engine::general_purpose::STANDARD, Engine}; +use bitwarden_core::require; use bitwarden_crypto::{EncString, KeyDecryptable, SymmetricCryptoKey}; use chrono::Utc; use schemars::JsonSchema; @@ -14,7 +15,7 @@ use crate::{ AccessToken, JWTToken, }, client::{LoginMethod, ServiceAccountLoginMethod}, - error::{require, Error, Result}, + error::{Error, Result}, secrets_manager::state::{self, ClientState}, Client, }; diff --git a/crates/bitwarden/src/auth/login/api_key.rs b/crates/bitwarden/src/auth/login/api_key.rs index 8b83e3a38..3882931ba 100644 --- a/crates/bitwarden/src/auth/login/api_key.rs +++ b/crates/bitwarden/src/auth/login/api_key.rs @@ -1,3 +1,4 @@ +use bitwarden_core::require; use bitwarden_crypto::{EncString, MasterKey}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -9,7 +10,7 @@ use crate::{ JWTToken, }, client::{LoginMethod, UserLoginMethod}, - error::{require, Result}, + error::Result, Client, }; diff --git a/crates/bitwarden/src/auth/login/auth_request.rs b/crates/bitwarden/src/auth/login/auth_request.rs index fb449e5d4..6a8005b6d 100644 --- a/crates/bitwarden/src/auth/login/auth_request.rs +++ b/crates/bitwarden/src/auth/login/auth_request.rs @@ -2,6 +2,7 @@ use bitwarden_api_api::{ apis::auth_requests_api::{auth_requests_id_response_get, auth_requests_post}, models::{AuthRequestCreateRequestModel, AuthRequestType}, }; +use bitwarden_core::require; use bitwarden_crypto::Kdf; use uuid::Uuid; @@ -11,7 +12,7 @@ use crate::{ auth_request::new_auth_request, }, client::{LoginMethod, UserLoginMethod}, - error::{require, Result}, + error::Result, mobile::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest}, Client, }; diff --git a/crates/bitwarden/src/auth/login/password.rs b/crates/bitwarden/src/auth/login/password.rs index e0cb67dbe..4960b498b 100644 --- a/crates/bitwarden/src/auth/login/password.rs +++ b/crates/bitwarden/src/auth/login/password.rs @@ -22,9 +22,10 @@ pub(crate) async fn login_password( client: &mut Client, input: &PasswordLoginRequest, ) -> Result { + use bitwarden_core::require; use bitwarden_crypto::{EncString, HashPurpose, MasterKey}; - use crate::{client::UserLoginMethod, error::require}; + use crate::client::UserLoginMethod; info!("password logging in"); diff --git a/crates/bitwarden/src/error.rs b/crates/bitwarden/src/error.rs index bc06a7591..9f264b465 100644 --- a/crates/bitwarden/src/error.rs +++ b/crates/bitwarden/src/error.rs @@ -15,6 +15,8 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum Error { + #[error(transparent)] + MissingFieldError(#[from] bitwarden_core::MissingFieldError), #[error("The client is not authenticated or the session has expired")] NotAuthenticated, @@ -26,8 +28,6 @@ pub enum Error { #[error("The response received was invalid and could not be processed")] InvalidResponse, - #[error("The response received was missing some of the required fields: {0}")] - MissingFields(&'static str), #[error("Cryptography error, {0}")] Crypto(#[from] bitwarden_crypto::CryptoError), @@ -154,18 +154,4 @@ macro_rules! impl_bitwarden_error { impl_bitwarden_error!(ApiError); impl_bitwarden_error!(IdentityError); -/// This macro is used to require that a value is present or return an error otherwise. -/// It is equivalent to using `val.ok_or(Error::MissingFields)?`, but easier to use and -/// with a more descriptive error message. -/// Note that this macro will return early from the function if the value is not present. -macro_rules! require { - ($val:expr) => { - match $val { - Some(val) => val, - None => return Err($crate::error::Error::MissingFields(stringify!($val))), - } - }; -} -pub(crate) use require; - pub type Result = std::result::Result; diff --git a/crates/bitwarden/src/platform/get_user_api_key.rs b/crates/bitwarden/src/platform/get_user_api_key.rs index 3e408d926..207721f44 100644 --- a/crates/bitwarden/src/platform/get_user_api_key.rs +++ b/crates/bitwarden/src/platform/get_user_api_key.rs @@ -2,6 +2,7 @@ use bitwarden_api_api::{ apis::accounts_api::accounts_api_key_post, models::{ApiKeyResponseModel, SecretVerificationRequestModel}, }; +use bitwarden_core::require; use bitwarden_crypto::{HashPurpose, MasterKey}; use log::{debug, info}; use schemars::JsonSchema; @@ -10,7 +11,7 @@ use serde::{Deserialize, Serialize}; use super::SecretVerificationRequest; use crate::{ client::{LoginMethod, UserLoginMethod}, - error::{require, Error, Result}, + error::{Error, Result}, Client, }; diff --git a/crates/bitwarden/src/secrets_manager/projects/delete.rs b/crates/bitwarden/src/secrets_manager/projects/delete.rs index 05c808c7e..bfc6e2d82 100644 --- a/crates/bitwarden/src/secrets_manager/projects/delete.rs +++ b/crates/bitwarden/src/secrets_manager/projects/delete.rs @@ -1,14 +1,12 @@ use bitwarden_api_api::models::{ BulkDeleteResponseModel, BulkDeleteResponseModelListResponseModel, }; +use bitwarden_core::require; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::{ - client::Client, - error::{require, Result}, -}; +use crate::{client::Client, error::Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase", deny_unknown_fields)] diff --git a/crates/bitwarden/src/secrets_manager/projects/project_response.rs b/crates/bitwarden/src/secrets_manager/projects/project_response.rs index 82e98dff0..e8cac63fd 100644 --- a/crates/bitwarden/src/secrets_manager/projects/project_response.rs +++ b/crates/bitwarden/src/secrets_manager/projects/project_response.rs @@ -1,14 +1,12 @@ use bitwarden_api_api::models::ProjectResponseModel; +use bitwarden_core::require; use bitwarden_crypto::{CryptoError, EncString, KeyDecryptable}; use chrono::{DateTime, Utc}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::{ - client::encryption_settings::EncryptionSettings, - error::{require, Result}, -}; +use crate::{client::encryption_settings::EncryptionSettings, error::Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase", deny_unknown_fields)] diff --git a/crates/bitwarden/src/secrets_manager/secrets/delete.rs b/crates/bitwarden/src/secrets_manager/secrets/delete.rs index f3fe264e1..1c54e4169 100644 --- a/crates/bitwarden/src/secrets_manager/secrets/delete.rs +++ b/crates/bitwarden/src/secrets_manager/secrets/delete.rs @@ -1,14 +1,12 @@ use bitwarden_api_api::models::{ BulkDeleteResponseModel, BulkDeleteResponseModelListResponseModel, }; +use bitwarden_core::require; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::{ - client::Client, - error::{require, Result}, -}; +use crate::{client::Client, error::Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase", deny_unknown_fields)] diff --git a/crates/bitwarden/src/secrets_manager/secrets/list.rs b/crates/bitwarden/src/secrets_manager/secrets/list.rs index 6cfa85516..a5d263253 100644 --- a/crates/bitwarden/src/secrets_manager/secrets/list.rs +++ b/crates/bitwarden/src/secrets_manager/secrets/list.rs @@ -1,6 +1,7 @@ use bitwarden_api_api::models::{ SecretWithProjectsListResponseModel, SecretsWithProjectsInnerSecret, }; +use bitwarden_core::require; use bitwarden_crypto::{CryptoError, EncString, KeyDecryptable}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -8,7 +9,7 @@ use uuid::Uuid; use crate::{ client::{encryption_settings::EncryptionSettings, Client}, - error::{require, Result}, + error::Result, }; #[derive(Serialize, Deserialize, Debug, JsonSchema)] diff --git a/crates/bitwarden/src/secrets_manager/secrets/secret_response.rs b/crates/bitwarden/src/secrets_manager/secrets/secret_response.rs index 3b89629a4..4f306078d 100644 --- a/crates/bitwarden/src/secrets_manager/secrets/secret_response.rs +++ b/crates/bitwarden/src/secrets_manager/secrets/secret_response.rs @@ -1,16 +1,14 @@ use bitwarden_api_api::models::{ BaseSecretResponseModel, BaseSecretResponseModelListResponseModel, SecretResponseModel, }; +use bitwarden_core::require; use bitwarden_crypto::{CryptoError, EncString, KeyDecryptable}; use chrono::{DateTime, Utc}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::{ - client::encryption_settings::EncryptionSettings, - error::{require, Result}, -}; +use crate::{client::encryption_settings::EncryptionSettings, error::Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase", deny_unknown_fields)] diff --git a/crates/bitwarden/src/secrets_manager/secrets/sync.rs b/crates/bitwarden/src/secrets_manager/secrets/sync.rs index e2546022d..565f25ecb 100644 --- a/crates/bitwarden/src/secrets_manager/secrets/sync.rs +++ b/crates/bitwarden/src/secrets_manager/secrets/sync.rs @@ -1,15 +1,12 @@ use bitwarden_api_api::models::SecretsSyncResponseModel; +use bitwarden_core::require; use chrono::{DateTime, Utc}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; use super::SecretResponse; -use crate::{ - client::encryption_settings::EncryptionSettings, - error::{require, Result}, - Client, -}; +use crate::{client::encryption_settings::EncryptionSettings, error::Result, Client}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase", deny_unknown_fields)] diff --git a/crates/bitwarden/src/tool/exporters/mod.rs b/crates/bitwarden/src/tool/exporters/mod.rs index 359cab8c5..b52b91813 100644 --- a/crates/bitwarden/src/tool/exporters/mod.rs +++ b/crates/bitwarden/src/tool/exporters/mod.rs @@ -1,10 +1,11 @@ +use bitwarden_core::require; use bitwarden_crypto::KeyDecryptable; use bitwarden_exporters::export; use schemars::JsonSchema; use crate::{ client::{LoginMethod, UserLoginMethod}, - error::{require, Error, Result}, + error::{Error, Result}, vault::{ login::LoginUriView, Cipher, CipherType, CipherView, Collection, FieldView, Folder, FolderView, SecureNoteType, diff --git a/crates/bitwarden/src/tool/send.rs b/crates/bitwarden/src/tool/send.rs index 2ce83dd0a..ce99396f1 100644 --- a/crates/bitwarden/src/tool/send.rs +++ b/crates/bitwarden/src/tool/send.rs @@ -3,6 +3,7 @@ use base64::{ Engine, }; use bitwarden_api_api::models::{SendFileModel, SendResponseModel, SendTextModel}; +use bitwarden_core::require; use bitwarden_crypto::{ derive_shareable_key, generate_random_bytes, CryptoError, EncString, KeyDecryptable, KeyEncryptable, SymmetricCryptoKey, @@ -14,7 +15,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr}; use uuid::Uuid; use zeroize::Zeroizing; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; const SEND_ITERATIONS: u32 = 100_000; diff --git a/crates/bitwarden/src/uniffi_support.rs b/crates/bitwarden/src/uniffi_support.rs index b23a9cbef..562393307 100644 --- a/crates/bitwarden/src/uniffi_support.rs +++ b/crates/bitwarden/src/uniffi_support.rs @@ -3,7 +3,9 @@ use std::num::NonZeroU32; use bitwarden_crypto::{AsymmetricEncString, EncString}; use uuid::Uuid; -use crate::UniffiCustomTypeConverter; +type DateTime = chrono::DateTime; +uniffi::ffi_converter_forward!(DateTime, bitwarden_core::UniFfiTag, crate::UniFfiTag); +uniffi::ffi_converter_forward!(Uuid, bitwarden_core::UniFfiTag, crate::UniFfiTag); uniffi::ffi_converter_forward!(NonZeroU32, bitwarden_crypto::UniFfiTag, crate::UniFfiTag); uniffi::ffi_converter_forward!(EncString, bitwarden_crypto::UniFfiTag, crate::UniFfiTag); @@ -12,32 +14,3 @@ uniffi::ffi_converter_forward!( bitwarden_crypto::UniFfiTag, crate::UniFfiTag ); - -type DateTime = chrono::DateTime; -uniffi::custom_type!(DateTime, std::time::SystemTime); - -impl UniffiCustomTypeConverter for chrono::DateTime { - type Builtin = std::time::SystemTime; - - fn into_custom(val: Self::Builtin) -> uniffi::Result { - Ok(Self::from(val)) - } - - fn from_custom(obj: Self) -> Self::Builtin { - obj.into() - } -} - -uniffi::custom_type!(Uuid, String); - -impl UniffiCustomTypeConverter for Uuid { - type Builtin = String; - - fn into_custom(val: Self::Builtin) -> uniffi::Result { - Uuid::parse_str(val.as_str()).map_err(|e| e.into()) - } - - fn from_custom(obj: Self) -> Self::Builtin { - obj.to_string() - } -} diff --git a/crates/bitwarden/src/vault/cipher/cipher.rs b/crates/bitwarden/src/vault/cipher/cipher.rs index 3b688df32..72965a297 100644 --- a/crates/bitwarden/src/vault/cipher/cipher.rs +++ b/crates/bitwarden/src/vault/cipher/cipher.rs @@ -1,4 +1,5 @@ use bitwarden_api_api::models::CipherDetailsResponseModel; +use bitwarden_core::require; use bitwarden_crypto::{ CryptoError, EncString, KeyContainer, KeyDecryptable, KeyEncryptable, LocateKey, SymmetricCryptoKey, @@ -17,7 +18,7 @@ use super::{ #[cfg(feature = "uniffi")] use crate::{client::encryption_settings::EncryptionSettings, vault::Fido2CredentialView}; use crate::{ - error::{require, Error, Result}, + error::{Error, Result}, vault::{password_history, Fido2CredentialFullView}, }; diff --git a/crates/bitwarden/src/vault/cipher/field.rs b/crates/bitwarden/src/vault/cipher/field.rs index 8a7b1d5f5..2fd15eb0b 100644 --- a/crates/bitwarden/src/vault/cipher/field.rs +++ b/crates/bitwarden/src/vault/cipher/field.rs @@ -1,4 +1,5 @@ use bitwarden_api_api::models::CipherFieldModel; +use bitwarden_core::require; use bitwarden_crypto::{ CryptoError, EncString, KeyDecryptable, KeyEncryptable, SymmetricCryptoKey, }; @@ -7,7 +8,7 @@ use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; use super::linked_id::LinkedIdType; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; #[derive(Clone, Copy, Serialize_repr, Deserialize_repr, Debug, JsonSchema)] #[repr(u8)] diff --git a/crates/bitwarden/src/vault/cipher/linked_id.rs b/crates/bitwarden/src/vault/cipher/linked_id.rs index 373fe9f96..4d1826ec8 100644 --- a/crates/bitwarden/src/vault/cipher/linked_id.rs +++ b/crates/bitwarden/src/vault/cipher/linked_id.rs @@ -1,3 +1,4 @@ +use bitwarden_core::MissingFieldError; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; @@ -112,7 +113,7 @@ impl TryFrom for LinkedIdType { 416 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::FirstName)), 417 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::LastName)), 418 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::FullName)), - _ => Err(Error::MissingFields("LinkedIdType")), + _ => Err(MissingFieldError("LinkedIdType").into()), } } } diff --git a/crates/bitwarden/src/vault/cipher/login.rs b/crates/bitwarden/src/vault/cipher/login.rs index 47dc71dc5..e065ecfbb 100644 --- a/crates/bitwarden/src/vault/cipher/login.rs +++ b/crates/bitwarden/src/vault/cipher/login.rs @@ -1,5 +1,6 @@ use base64::{engine::general_purpose::STANDARD, Engine}; use bitwarden_api_api::models::{CipherLoginModel, CipherLoginUriModel}; +use bitwarden_core::require; use bitwarden_crypto::{ CryptoError, EncString, KeyDecryptable, KeyEncryptable, SymmetricCryptoKey, }; @@ -8,7 +9,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; #[derive(Clone, Copy, Serialize_repr, Deserialize_repr, Debug, JsonSchema)] #[repr(u8)] diff --git a/crates/bitwarden/src/vault/cipher/secure_note.rs b/crates/bitwarden/src/vault/cipher/secure_note.rs index be4fc6689..f74997adc 100644 --- a/crates/bitwarden/src/vault/cipher/secure_note.rs +++ b/crates/bitwarden/src/vault/cipher/secure_note.rs @@ -1,10 +1,11 @@ use bitwarden_api_api::models::CipherSecureNoteModel; +use bitwarden_core::require; use bitwarden_crypto::{CryptoError, KeyDecryptable, KeyEncryptable, SymmetricCryptoKey}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; #[derive(Clone, Copy, Serialize_repr, Deserialize_repr, Debug, JsonSchema)] #[repr(u8)] diff --git a/crates/bitwarden/src/vault/collection.rs b/crates/bitwarden/src/vault/collection.rs index cfa17b946..0ffe2618f 100644 --- a/crates/bitwarden/src/vault/collection.rs +++ b/crates/bitwarden/src/vault/collection.rs @@ -1,4 +1,5 @@ use bitwarden_api_api::models::CollectionDetailsResponseModel; +use bitwarden_core::require; use bitwarden_crypto::{ CryptoError, EncString, KeyContainer, KeyDecryptable, LocateKey, SymmetricCryptoKey, }; @@ -6,7 +7,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase", deny_unknown_fields)] diff --git a/crates/bitwarden/src/vault/domain.rs b/crates/bitwarden/src/vault/domain.rs index 482cb1f59..9e070f25a 100644 --- a/crates/bitwarden/src/vault/domain.rs +++ b/crates/bitwarden/src/vault/domain.rs @@ -1,7 +1,8 @@ +use bitwarden_core::require; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] pub struct GlobalDomains { diff --git a/crates/bitwarden/src/vault/folder.rs b/crates/bitwarden/src/vault/folder.rs index 09bf8868e..449860dea 100644 --- a/crates/bitwarden/src/vault/folder.rs +++ b/crates/bitwarden/src/vault/folder.rs @@ -1,4 +1,5 @@ use bitwarden_api_api::models::FolderResponseModel; +use bitwarden_core::require; use bitwarden_crypto::{ CryptoError, EncString, KeyDecryptable, KeyEncryptable, SymmetricCryptoKey, }; @@ -7,7 +8,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::error::{require, Error, Result}; +use crate::error::{Error, Result}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase")] diff --git a/crates/bitwarden/src/vault/sync.rs b/crates/bitwarden/src/vault/sync.rs index bd9d38104..79f29d870 100644 --- a/crates/bitwarden/src/vault/sync.rs +++ b/crates/bitwarden/src/vault/sync.rs @@ -1,6 +1,7 @@ use bitwarden_api_api::models::{ DomainsResponseModel, ProfileOrganizationResponseModel, ProfileResponseModel, SyncResponseModel, }; +use bitwarden_core::require; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -9,7 +10,7 @@ use super::domain::GlobalDomains; use crate::{ admin_console::Policy, client::{encryption_settings::EncryptionSettings, Client}, - error::{require, Error, Result}, + error::{Error, Result}, vault::{Cipher, Collection, Folder}, }; diff --git a/crates/bitwarden/uniffi.toml b/crates/bitwarden/uniffi.toml index 7a804ef1d..c5fdba5c2 100644 --- a/crates/bitwarden/uniffi.toml +++ b/crates/bitwarden/uniffi.toml @@ -1,9 +1,9 @@ [bindings.kotlin] -package_name = "com.bitwarden.core" +package_name = "com.bitwarden.bitwarden" generate_immutable_records = true android = true [bindings.swift] -ffi_module_name = "BitwardenCoreFFI" -module_name = "BitwardenCore" +ffi_module_name = "BitwardenBitwardenFFI" +module_name = "BitwardenBitwarden" generate_immutable_records = true diff --git a/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt b/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt index f51d0309e..d304da8a1 100644 --- a/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt +++ b/languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt @@ -23,10 +23,10 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import androidx.fragment.app.FragmentActivity import com.bitwarden.core.DateTime -import com.bitwarden.core.Folder -import com.bitwarden.core.InitOrgCryptoRequest -import com.bitwarden.core.InitUserCryptoMethod -import com.bitwarden.core.InitUserCryptoRequest +import com.bitwarden.bitwarden.Folder +import com.bitwarden.bitwarden.InitOrgCryptoRequest +import com.bitwarden.bitwarden.InitUserCryptoMethod +import com.bitwarden.bitwarden.InitUserCryptoRequest import com.bitwarden.core.Uuid import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.Kdf