Skip to content

Commit

Permalink
Added register to device signer
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Mar 20, 2024
1 parent d50c983 commit 5bb90e4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ u256-literal = "1"
url = "2"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.42"
wasm-webauthn = "0.1.0"
wasm-webauthn = { git = "https://github.com/broody/wasm-webauthn" }
webauthn-rs-proto = "0.4"
account-sdk = { path = "crates/account_sdk" }
tokio = { version = "1", features = ["macros", "time"] }
1 change: 1 addition & 0 deletions crates/account_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ wasm-webauthn.workspace = true
tokio.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-test = "0.3.34"
js-sys = "0.3.69"
web-sys = "0.3.69"
9 changes: 3 additions & 6 deletions crates/account_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ pub mod abigen;
pub mod felt_ser;
pub mod session_token;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
pub mod tests;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
pub fn alert(s: &str);
}
#[cfg(target_arch = "wasm32")]
pub mod wasm_tests;
1 change: 1 addition & 0 deletions crates/account_sdk/src/wasm_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod webauthn;

Check warning on line 1 in crates/account_sdk/src/wasm_tests/mod.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/wasm_tests/mod.rs
14 changes: 14 additions & 0 deletions crates/account_sdk/src/wasm_tests/webauthn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use wasm_bindgen_test::*;

Check warning on line 1 in crates/account_sdk/src/wasm_tests/webauthn.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/wasm_tests/webauthn.rs

// FIXME: Currently wasm tests are failing with error below.
//
// Error: failed to deserialize wasm module

// Caused by:
// 0: failed to parse code section
// 1: locals exceed maximum (at offset 738889)
// error: test failed, to rerun pass `--lib`
#[wasm_bindgen_test]
fn pass() {

Check warning on line 12 in crates/account_sdk/src/wasm_tests/webauthn.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/wasm_tests/webauthn.rs
assert_eq!(1, 1);
}
60 changes: 48 additions & 12 deletions crates/account_sdk/src/webauthn_signer/signers/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use super::Signer;

#[derive(Debug, Clone)]

Check warning on line 10 in crates/account_sdk/src/webauthn_signer/signers/device.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/webauthn_signer/signers/device.rs
pub struct DeviceSigner {
rp_id: String,
credential_id: Vec<u8>
pub rp_id: String,
pub credential_id: Vec<u8>
}

impl DeviceSigner {
Expand All @@ -20,19 +20,47 @@ impl DeviceSigner {
credential_id
}
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl Signer for DeviceSigner {
async fn sign(&self, challenge: &[u8]) -> AuthenticatorAssertionResponse {
let (sender, receiver) = oneshot::channel();
pub async fn register(rp_id: String, user_name: String, challenge: &[u8]) -> Self {

Check warning on line 24 in crates/account_sdk/src/webauthn_signer/signers/device.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/webauthn_signer/signers/device.rs
let MakeCredentialResponse {
credential
} = Self::create_credential(rp_id.clone(), user_name, challenge).await;

Self {
rp_id,
credential_id: credential.id.0

Check warning on line 31 in crates/account_sdk/src/webauthn_signer/signers/device.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/webauthn_signer/signers/device.rs
}
}

async fn create_credential(rp_id: String, user_name: String, challenge: &[u8]) -> MakeCredentialResponse {

Check warning on line 35 in crates/account_sdk/src/webauthn_signer/signers/device.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/webauthn_signer/signers/device.rs
let (tx, rx) = oneshot::channel();
let rp_id = rp_id.to_owned();
let challenge = challenge.to_vec();

spawn_local(async move {
let results: MakeCredentialResponse = MakeCredentialArgsBuilder::default()
.rp_id(Some(rp_id))
.challenge(challenge)

Check warning on line 43 in crates/account_sdk/src/webauthn_signer/signers/device.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/webauthn_signer/signers/device.rs
.user_name(Some(user_name))
.uv(UserVerificationRequirement::Required)
.build().expect("invalid args")
.make_credential().await
.expect("make credential");

tx.send(results).expect("receiver dropped")
});

rx.await.expect("receiver dropped")
}

async fn get_assertion(&self, challenge: &[u8]) -> GetAssertionResponse {
let (tx, rx) = oneshot::channel();
let credential_id = self.credential_id.clone();

Check warning on line 58 in crates/account_sdk/src/webauthn_signer/signers/device.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/cairo-webauthn/cairo-webauthn/crates/account_sdk/src/webauthn_signer/signers/device.rs
let rp_id = self.rp_id.to_owned();
let challenge = challenge.to_vec();

spawn_local(async move {
let mut credential = Credential::from(CredentialID(credential_id));
let credential = Credential::from(CredentialID(credential_id));

let results: GetAssertionResponse = GetAssertionArgsBuilder::default()
.rp_id(Some(rp_id))
Expand All @@ -45,15 +73,23 @@ impl Signer for DeviceSigner {
.await
.expect("get assertion");

sender.send(results).expect("receiver dropped");
tx.send(results).expect("receiver dropped");
});

rx.await.expect("receiver dropped")
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl Signer for DeviceSigner {
async fn sign(&self, challenge: &[u8]) -> AuthenticatorAssertionResponse {
let GetAssertionResponse {
signature,
client_data_json,
flags,
counter,
} = receiver.await.expect("receiver dropped");
} = self.get_assertion(challenge).await;

AuthenticatorAssertionResponse {
authenticator_data: AuthenticatorData {
Expand Down

0 comments on commit 5bb90e4

Please sign in to comment.