Skip to content

Commit

Permalink
Merge pull request #38 from cartridge-gg/fix-webauthn-set
Browse files Browse the repository at this point in the history
Auth check `set_webauthn_pub_key`
  • Loading branch information
broody authored Feb 22, 2024
2 parents 8ae811a + 63be1e9 commit f886075
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
26 changes: 14 additions & 12 deletions crates/account_sdk/src/tests/webauthn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use starknet::{
use crate::abigen::account::WebauthnPubKey;
use crate::abigen::account::WebauthnSignature;
use crate::{
tests::runners::devnet_runner::DevnetRunner,
tests::runners::katana_runner::KatanaRunner,
webauthn_signer::{cairo_args::VerifyWebauthnSignerArgs, P256r1Signer},
};

Expand All @@ -19,7 +19,7 @@ async fn test_set_webauthn_public_key() {
let origin = "localhost".to_string();
let signer = P256r1Signer::random(origin.clone());

let data = utils::WebauthnTestData::<DevnetRunner>::new(private_key, signer).await;
let data = utils::WebauthnTestData::<KatanaRunner>::new(private_key, signer).await;
let reader = data.account_reader();

let public_key = reader
Expand All @@ -29,11 +29,9 @@ async fn test_set_webauthn_public_key() {
.await
.unwrap();

match public_key {
Option::Some(_) => panic!("Public key already set"),
Option::None => (),
}
assert!(public_key.is_none(), "Public key already set");

let target_key = data.webauthn_public_key();
data.set_webauthn_public_key().await;

let public_key = reader
Expand All @@ -43,10 +41,14 @@ async fn test_set_webauthn_public_key() {
.await
.unwrap();

match public_key {
Option::Some(_) => (),
Option::None => panic!("Public key not set"),
}
assert!(
public_key
== Some(WebauthnPubKey {
x: target_key.0.into(),
y: target_key.1.into(),
}),
"Public key mismatch"
)
}

#[tokio::test]
Expand All @@ -55,7 +57,7 @@ async fn test_verify_webauthn_explicit() {
let origin = "localhost".to_string();
let signer = P256r1Signer::random(origin.clone());

let data = utils::WebauthnTestData::<DevnetRunner>::new(private_key, signer).await;
let data = utils::WebauthnTestData::<KatanaRunner>::new(private_key, signer).await;
data.set_webauthn_public_key().await;
let reader = data.account_reader();

Expand Down Expand Up @@ -93,7 +95,7 @@ async fn test_verify_webauthn_execute() {
let origin = "localhost".to_string();
let signer = P256r1Signer::random(origin.clone());

let data = utils::WebauthnTestData::<DevnetRunner>::new(private_key, signer).await;
let data = utils::WebauthnTestData::<KatanaRunner>::new(private_key, signer).await;
data.set_webauthn_public_key().await;

let webauthn_executor = data.webauthn_executor().await;
Expand Down
11 changes: 11 additions & 0 deletions crates/webauthn/auth/src/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mod webauthn_component {
use core::result::ResultTrait;
use starknet::info::{TxInfo, get_tx_info, get_block_timestamp};
use starknet::account::Call;
use starknet::get_caller_address;
use starknet::get_contract_address;
use core::ecdsa::check_ecdsa_signature;
use starknet::secp256r1::{Secp256r1Point, Secp256r1Impl};
use webauthn_auth::webauthn::verify;
Expand All @@ -40,6 +42,7 @@ mod webauthn_component {

mod Errors {
const INVALID_SIGNATURE: felt252 = 'Account: invalid signature';
const UNAUTHORIZED: felt252 = 'Account: unauthorized';
}

#[embeddable_as(Webauthn)]
Expand All @@ -49,6 +52,8 @@ mod webauthn_component {
fn set_webauthn_pub_key(
ref self: ComponentState<TContractState>, public_key: WebauthnPubKey,
) {
assert_only_self();

self.public_key.write(Option::Some(public_key));
}
fn get_webauthn_pub_key(self: @ComponentState<TContractState>,) -> Option<WebauthnPubKey> {
Expand Down Expand Up @@ -95,4 +100,10 @@ mod webauthn_component {
}
}
}

fn assert_only_self() {
let caller = get_caller_address();
let self = get_contract_address();
assert(self == caller, Errors::UNAUTHORIZED);
}
}

0 comments on commit f886075

Please sign in to comment.