Skip to content

Commit

Permalink
[PM-6113] feat: return credential creation result
Browse files Browse the repository at this point in the history
  • Loading branch information
coroiu committed Mar 26, 2024
1 parent 106b615 commit 5b19cb3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
5 changes: 3 additions & 2 deletions crates/bitwarden-json/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use bitwarden::{
client::client_settings::ClientSettings,
error::Result,
platform::fido2::{
Fido2ClientCreateCredentialRequest, Fido2CredentialStore, Fido2UserInterface, VaultItem,
Fido2ClientCreateCredentialRequest, Fido2CreatedPublicKeyCredential, Fido2CredentialStore,
Fido2UserInterface, VaultItem,
},
};

Expand All @@ -27,7 +28,7 @@ impl Client {
request: Fido2ClientCreateCredentialRequest,
user_interface: impl Fido2UserInterface + Send + Sync,
credential_store: impl Fido2CredentialStore + Send,
) -> Result<VaultItem> {
) -> Result<Fido2CreatedPublicKeyCredential> {
let mut client = self.0.lock().await;

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-json/src/client.rs#L26-L32

Added lines #L26 - L32 were not covered by tests

client
Expand Down
12 changes: 9 additions & 3 deletions crates/bitwarden-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use super::fido2::{
client_create_credential::JSFido2ClientCreateCredentialRequest,
credential_store::JSFido2CredentialStore, user_interface::JSFido2UserInterface,
};
use bitwarden_json::{client::Client as JsonClient, Fido2ClientCreateCredentialRequest};
use bitwarden_json::{
client::Client as JsonClient, Fido2ClientCreateCredentialRequest,
Fido2CreatedPublicKeyCredential,
};
use js_sys::Promise;
use log::Level;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -64,7 +67,7 @@ impl BitwardenClient {
js_request: JsValue,
user_interface: JSFido2UserInterface,
credential_store: JSFido2CredentialStore,
) {
) -> JsValue {
log::info!("wasm_bindgen.client_create_credential");
log::debug!("wasm_bindgen.client_create_credential");

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L65-L72

Added lines #L65 - L72 were not covered by tests
// let request = Fido2ClientCreateCredentialRequest {
Expand All @@ -80,13 +83,16 @@ impl BitwardenClient {
origin: request.origin,
};

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L76-L84

Added lines #L76 - L84 were not covered by tests

self.0
let result = self
.0
.client_create_credential(
webauthn_request,
wrapped_user_interface,
wrapped_credential_store,
)
.await
.unwrap();

serde_wasm_bindgen::to_value(&result).unwrap()
}

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#L86-L97

Added lines #L86 - L97 were not covered by tests
}
5 changes: 3 additions & 2 deletions crates/bitwarden/src/platform/client_platform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{
fido2::{
Fido2ClientCreateCredentialRequest, Fido2CredentialStore, Fido2UserInterface, VaultItem,
Fido2ClientCreateCredentialRequest, Fido2CreatedPublicKeyCredential, Fido2CredentialStore,
Fido2UserInterface,
},
generate_fingerprint::{generate_fingerprint, generate_user_fingerprint},
FingerprintRequest, FingerprintResponse,
Expand All @@ -27,7 +28,7 @@ impl<'a> ClientPlatform<'a> {
request: Fido2ClientCreateCredentialRequest,
user_interface: impl Fido2UserInterface + Send + Sync,
credential_store: impl Fido2CredentialStore + Send,
) -> Result<VaultItem> {
) -> Result<Fido2CreatedPublicKeyCredential> {
log::debug!(
"client_platform.client_create_credential, request: {:?}",

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/client_platform.rs#L26-L33

Added lines #L26 - L33 were not covered by tests
request
Expand Down
37 changes: 5 additions & 32 deletions crates/bitwarden/src/platform/fido2/client_create_credential.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::error::Result;
use crate::error::{Error, Result};
use url::Url;

use super::{
Expand All @@ -26,11 +26,13 @@ pub struct Fido2ClientCreateCredentialRequest {
pub origin: String,
}

pub type Fido2CreatedPublicKeyCredential = CreatedPublicKeyCredential;

pub(crate) async fn client_create_credential(
request: Fido2ClientCreateCredentialRequest,
user_interface: impl Fido2UserInterface + Send + Sync,
credential_store: impl Fido2CredentialStore + Send,
) -> Result<VaultItem> {
) -> Result<CreatedPublicKeyCredential> {
log::debug!("fido2.client_create_credential, request: {:?}", request);
let context = Arc::new(Fido2Transaction::new(
Fido2Options::CreateCredential(clone_create_options(&request.options)),
Expand All @@ -47,38 +49,9 @@ pub(crate) async fn client_create_credential(
client
.register(&Url::parse(&request.origin).unwrap(), request.options, None)
.await
.unwrap();

std::result::Result::Ok(VaultItem::new("cipher_id".to_string(), "name".to_string()))
.map_err(|error| Error::Internal("Unable to create credential".into()))
}

Check warning on line 53 in crates/bitwarden/src/platform/fido2/client_create_credential.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/fido2/client_create_credential.rs#L31-L53

Added lines #L31 - L53 were not covered by tests

// let challenge = vec![0; 32];
// let options = CredentialCreationOptions {
// public_key: PublicKeyCredentialCreationOptions {
// rp: PublicKeyCredentialRpEntity {
// id: Some("bitwarden.com".to_string()),
// name: "Bitwarden".to_string(),
// },
// user: PublicKeyCredentialUserEntity {
// id: vec![].into(),
// name: "user".to_string(),
// display_name: "User".to_string(),
// },
// challenge: challenge.into(),
// pub_key_cred_params: vec![PublicKeyCredentialParameters {
// ty: PublicKeyCredentialType::PublicKey,
// alg: coset::iana::Algorithm::ES256,
// }],
// timeout: None,
// exclude_credentials: None,
// authenticator_selection: None,
// attestation: AttestationConveyancePreference::None,
// attestation_formats: None,
// hints: None,
// extensions: None,
// },
// };

fn clone_create_options(options: &CredentialCreationOptions) -> CredentialCreationOptions {
let json: String = serde_json::to_string(options).unwrap();
serde_json::from_str(&json).unwrap()
Expand Down
4 changes: 3 additions & 1 deletion crates/bitwarden/src/platform/fido2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ mod fido2;
mod transaction;
mod user_interface;

pub use client_create_credential::Fido2ClientCreateCredentialRequest;
pub use client_create_credential::{
Fido2ClientCreateCredentialRequest, Fido2CreatedPublicKeyCredential,
};

pub use credential_store::{Fido2CredentialStore, FindCredentialsParams, SaveCredentialParams};
pub use fido2::Fido2ClientGetAssertionRequest;
Expand Down
22 changes: 21 additions & 1 deletion languages/js/sdk-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,33 @@ export interface Fido2ClientCreateCredentialRequest {
origin: string;
}

export interface Fido2CreatedPublicKeyCredential {
id: string,
rawId: Uint8Array,
type: 'public-key',
response: {
clientDataJSON: Uint8Array,
authenticatorData: Uint8Array,
publicKey: Uint8Array,
publicKeyAlgorithm: number,
attestationObject: Uint8Array,
transports: string[]
},
authenticatorAttachment: string,
clientExtensionResults: {
credProps: {
rk: boolean
}
}
}

interface BitwardenSDKClient {
run_command(js_input: string): Promise<any>;
client_create_credential(
webauthn_request: Fido2ClientCreateCredentialRequest,
user_interface: Fido2UserInterface,
credential_store: Fido2CredentialStore,
): Promise<void>;
): Promise<Fido2CreatedPublicKeyCredential>;
}

function handleResponse<T>(response: { success: boolean; errorMessage?: string; data?: T }): T {
Expand Down

0 comments on commit 5b19cb3

Please sign in to comment.