Skip to content

Commit

Permalink
fix fmt/clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Mar 21, 2024
1 parent 529d531 commit cae78e1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/account_sdk/src/wasm_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod webauthn;
pub mod webauthn;
6 changes: 3 additions & 3 deletions crates/account_sdk/src/wasm_tests/webauthn.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use wasm_bindgen_test::*;

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

// Caused by:
// 0: failed to parse code section
Expand All @@ -11,4 +11,4 @@ use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 1);
}
}
2 changes: 1 addition & 1 deletion crates/account_sdk/src/webauthn_signer/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub enum SignError {
#[error("Signer error: {0}")]
Signer(EcdsaSignError),
#[error("Device error: {0}")]
Device(DeviceError)
Device(DeviceError),
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand Down
52 changes: 32 additions & 20 deletions crates/account_sdk/src/webauthn_signer/signers/device.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
use std::result::Result;
use async_trait::async_trait;
use futures::channel::oneshot;
use std::result::Result;
use wasm_bindgen_futures::spawn_local;
use wasm_webauthn::*;

use crate::webauthn_signer::{account::SignError, credential::{self, AuthenticatorAssertionResponse, AuthenticatorData}};
use crate::webauthn_signer::{
account::SignError,
credential::{AuthenticatorAssertionResponse, AuthenticatorData},
};

use super::Signer;

#[derive(Debug, thiserror::Error)]
pub enum DeviceError {
#[error("Invalid args")]
InvalidArgs,
#[error("Create credential error: {0}")]
CreateCredential(String),
#[error("Get assertion error: {0}")]
GetAssertion(String),
#[error("Channel error: {0}")]
Channel(String)
Channel(String),
}

#[derive(Debug, Clone)]
pub struct DeviceSigner {
pub rp_id: String,
pub credential_id: Vec<u8>
pub credential_id: Vec<u8>,
}

impl DeviceSigner {
pub fn new(rp_id: String, credential_id: Vec<u8>) -> Self {
Self {
rp_id,
credential_id
credential_id,
}
}

pub async fn register(rp_id: String, user_name: String, challenge: &[u8]) -> Result<Self, SignError> {
let MakeCredentialResponse {
credential
} = Self::create_credential(rp_id.clone(), user_name, challenge).await?;
pub async fn register(
rp_id: String,
user_name: String,
challenge: &[u8],
) -> Result<Self, SignError> {
let MakeCredentialResponse { credential } =
Self::create_credential(rp_id.clone(), user_name, challenge).await?;

Ok(Self {
rp_id,
credential_id: credential.id.0
credential_id: credential.id.0,
})
}

async fn create_credential(rp_id: String, user_name: String, challenge: &[u8]) -> Result<MakeCredentialResponse, SignError> {
async fn create_credential(
rp_id: String,
user_name: String,
challenge: &[u8],
) -> Result<MakeCredentialResponse, SignError> {
let (tx, rx) = oneshot::channel();
let rp_id = rp_id.to_owned();
let challenge = challenge.to_vec();
Expand All @@ -58,13 +66,13 @@ impl DeviceSigner {
.uv(UserVerificationRequirement::Required)
.build()
.expect("invalid args")
.make_credential().await;
.make_credential()
.await;


match result {
Ok(credential) => {
let _ = tx.send(Ok(credential));
},
}
Err(e) => {
let _ = tx.send(Err(DeviceError::CreateCredential(e.to_string())));
}
Expand All @@ -73,7 +81,9 @@ impl DeviceSigner {

match rx.await {
Ok(result) => result.map_err(SignError::Device),
Err(_) => Err(SignError::Device(DeviceError::Channel("receiver dropped".to_string())))
Err(_) => Err(SignError::Device(DeviceError::Channel(
"credential receiver dropped".to_string(),
))),
}
}

Expand All @@ -82,7 +92,7 @@ impl DeviceSigner {
let credential_id = self.credential_id.clone();
let rp_id = self.rp_id.to_owned();
let challenge = challenge.to_vec();

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

Expand All @@ -99,7 +109,7 @@ impl DeviceSigner {
match result {
Ok(assertion) => {
let _ = tx.send(Ok(assertion));
},
}
Err(e) => {
let _ = tx.send(Err(DeviceError::GetAssertion(e.to_string())));
}
Expand All @@ -108,7 +118,9 @@ impl DeviceSigner {

match rx.await {
Ok(result) => result.map_err(SignError::Device),
Err(_) => Err(SignError::Device(DeviceError::Channel("receiver dropped".to_string())))
Err(_) => Err(SignError::Device(DeviceError::Channel(
"assertion receiver dropped".to_string(),
))),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/account_sdk/src/webauthn_signer/signers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::credential::AuthenticatorAssertionResponse;
use super::account::SignError;
use super::credential::AuthenticatorAssertionResponse;
use async_trait::async_trait;

pub mod device;
Expand Down
5 changes: 4 additions & 1 deletion crates/account_sdk/src/webauthn_signer/signers/p256r1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::webauthn_signer::{account::SignError, credential::{AuthenticatorData, CliendData}};
use crate::webauthn_signer::{
account::SignError,
credential::{AuthenticatorData, CliendData},
};
use async_trait::async_trait;
use p256::{
ecdsa::{signature::Signer as P256Signer, Signature, SigningKey, VerifyingKey},
Expand Down

0 comments on commit cae78e1

Please sign in to comment.