Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
coroiu committed Mar 1, 2024
1 parent 4c3b5f3 commit d6f209d
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions crates/bitwarden-json/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use async_lock::Mutex;
use bitwarden::client::client_settings::ClientSettings;
use bitwarden::{
client::client_settings::ClientSettings,
error::Result,
platform::{Fido2ClientGetAssertionRequest, Fido2GetAssertionUserInterface},
};

#[cfg(feature = "secrets")]
use crate::command::{ProjectsCommand, SecretsCommand};
Expand All @@ -16,6 +20,19 @@ impl Client {
Self(Mutex::new(bitwarden::Client::new(settings)))
}

pub async fn client_get_assertion(
&self,
request: Fido2ClientGetAssertionRequest,
user_interface: impl Fido2GetAssertionUserInterface,
) -> Result<String> {
let mut client = self.0.lock().await;

Check warning on line 28 in crates/bitwarden-json/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-json/src/client.rs#L23-L28

Added lines #L23 - L28 were not covered by tests

client
.platform()
.client_get_assertion(request, user_interface)
.await
}

Check warning on line 34 in crates/bitwarden-json/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-json/src/client.rs#L30-L34

Added lines #L30 - L34 were not covered by tests

pub async fn run_command(&self, input_str: &str) -> String {
const SUBCOMMANDS_TO_CLEAN: &[&str] = &["Secrets"];
let mut cmd_value: serde_json::Value = match serde_json::from_str(input_str) {
Expand Down Expand Up @@ -63,11 +80,6 @@ impl Client {
#[cfg(feature = "internal")]
Command::Fingerprint(req) => client.platform().fingerprint(&req).into_string(),

#[cfg(feature = "internal")]
Command::Fido2ClientGetAssertion(req) => {
client.platform().client_get_assertion(req).into_string()
}

#[cfg(feature = "secrets")]
Command::Secrets(cmd) => match cmd {
SecretsCommand::Get(req) => client.secrets().get(&req).await.into_string(),
Expand Down
10 changes: 1 addition & 9 deletions crates/bitwarden-json/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use bitwarden::{
#[cfg(feature = "internal")]
use bitwarden::{
auth::login::{ApiKeyLoginRequest, PasswordLoginRequest},
platform::{
Fido2ClientGetAssertionRequest, FingerprintRequest, SecretVerificationRequest, SyncRequest,
},
platform::{FingerprintRequest, SecretVerificationRequest, SyncRequest},
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -73,12 +71,6 @@ pub enum Command {
/// Returns: [SyncResponse](bitwarden::platform::SyncResponse)
Sync(SyncRequest),

#[cfg(feature = "internal")]
/// > Requires Authentication
///
/// Returns: ?
Fido2ClientGetAssertion(Fido2ClientGetAssertionRequest),

#[cfg(feature = "secrets")]
Secrets(SecretsCommand),
#[cfg(feature = "secrets")]
Expand Down
5 changes: 5 additions & 0 deletions crates/bitwarden-json/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
pub mod client;
pub mod command;
pub mod response;

pub use bitwarden::{
error::Result,
platform::{Fido2ClientGetAssertionRequest, Fido2GetAssertionUserInterface, VaultItem},
};
62 changes: 59 additions & 3 deletions crates/bitwarden-wasm/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
extern crate console_error_panic_hook;
use std::rc::Rc;
use std::{fmt::Result, process::Output, rc::Rc};

use bitwarden_json::client::Client as JsonClient;
use js_sys::Promise;
use bitwarden_json::{
client::Client as JsonClient, Fido2ClientGetAssertionRequest, Fido2GetAssertionUserInterface,
VaultItem,
};
use js_sys::{Object, Promise};
use log::Level;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::future_to_promise;
Expand All @@ -26,6 +29,36 @@ fn convert_level(level: LogLevel) -> Level {
}
}

#[wasm_bindgen]

Check warning on line 32 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L32

Added line #L32 was not covered by tests
extern "C" {
pub type JSFido2GetAssertionUserInterface;

#[wasm_bindgen(structural, method)]
pub fn pick_credential(

Check warning on line 37 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L37

Added line #L37 was not covered by tests
this: &JSFido2GetAssertionUserInterface,
cipher_ids: Vec<String>,
rp_id: String,
) -> Promise;
}

impl Fido2GetAssertionUserInterface for JSFido2GetAssertionUserInterface {
async fn pick_credential(
&self,
cipher_ids: Vec<String>,
rp_id: &str,
) -> bitwarden_json::Result<VaultItem> {
log::debug!("JSFido2GetAssertionUserInterface.pick_credential");
let picked_id_promise = self.pick_credential(cipher_ids.clone(), rp_id.to_string());

Check warning on line 51 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L45-L51

Added lines #L45 - L51 were not covered by tests

let picked_id = wasm_bindgen_futures::JsFuture::from(picked_id_promise).await;

Check warning on line 53 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L53

Added line #L53 was not covered by tests

Ok(VaultItem::new(
picked_id.unwrap().as_string().unwrap(),
"name".to_string(),
))
}

Check warning on line 59 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L55-L59

Added lines #L55 - L59 were not covered by tests
}

// Rc<...> is to avoid needing to take ownership of the Client during our async run_command
// function https://github.com/rustwasm/wasm-bindgen/issues/2195#issuecomment-799588401
#[wasm_bindgen]
Expand Down Expand Up @@ -53,4 +86,27 @@ impl BitwardenClient {
Ok(result.into())
})
}

#[wasm_bindgen]
pub async fn client_get_assertion(
&mut self,
param: String,
user_interface: JSFido2GetAssertionUserInterface,
) {
log::info!("wasm_bindgen.client_get_assertion");
log::debug!("wasm_bindgen.client_get_assertion");

Check warning on line 97 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L91-L97

Added lines #L91 - L97 were not covered by tests
// let rc = self.0.clone();
// future_to_promise(async move {
// let result = rc.run_command(&js_input).await;
// Ok(result.into())
// })
let request = Fido2ClientGetAssertionRequest {
webauthn_json: param,
};

self.0
.client_get_assertion(request, user_interface)
.await
.unwrap();
}

Check warning on line 111 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L103-L111

Added lines #L103 - L111 were not covered by tests
}
1 change: 1 addition & 0 deletions crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ sha1 = ">=0.10.5, <0.11"
sha2 = ">=0.10.6, <0.11"
thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.26.1", optional = true, features = ["tokio"] }
url = "2.5.0"
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }
zxcvbn = ">= 2.2.2, <3.0"

Expand Down
10 changes: 8 additions & 2 deletions crates/bitwarden/src/platform/client_platform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
client_get_assertion,
fido2::Fido2GetAssertionUserInterface,
generate_fingerprint::{generate_fingerprint, generate_user_fingerprint},
Fido2ClientGetAssertionRequest, FingerprintRequest, FingerprintResponse,
};
Expand All @@ -18,8 +19,13 @@ impl<'a> ClientPlatform<'a> {
generate_user_fingerprint(self.client, fingerprint_material)
}

pub fn client_get_assertion(&self, request: Fido2ClientGetAssertionRequest) -> Result<String> {
client_get_assertion(request)
pub async fn client_get_assertion(
&self,
request: Fido2ClientGetAssertionRequest,
user_interface: impl Fido2GetAssertionUserInterface,
) -> Result<String> {
log::debug!("client_platform.client_get_assertion");
client_get_assertion(request, user_interface).await
}

Check warning on line 29 in crates/bitwarden/src/platform/client_platform.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/client_platform.rs#L22-L29

Added lines #L22 - L29 were not covered by tests
}

Expand Down
Loading

0 comments on commit d6f209d

Please sign in to comment.