Skip to content

Commit 8aae271

Browse files
Update example to use JSON
1 parent deb8c10 commit 8aae271

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

libwebauthn/examples/webauthn_json_hid.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
use std::convert::TryInto;
21
use std::error::Error;
32
use std::io::{self, Write};
43
use std::time::Duration;
54

65
use libwebauthn::UvUpdate;
7-
use rand::{thread_rng, Rng};
86
use text_io::read;
97
use tokio::sync::broadcast::Receiver;
108
use tracing_subscriber::{self, EnvFilter};
119

1210
use libwebauthn::ops::webauthn::{
13-
GetAssertionRequest, GetAssertionRequestExtensions, MakeCredentialRequest, RelyingPartyId,
14-
UserVerificationRequirement, WebAuthnIDL as _,
11+
GetAssertionRequest, MakeCredentialRequest, RelyingPartyId, WebAuthnIDL as _,
1512
};
1613
use libwebauthn::pin::PinRequestReason;
17-
use libwebauthn::proto::ctap2::Ctap2PublicKeyCredentialDescriptor;
1814
use libwebauthn::transport::hid::list_devices;
1915
use libwebauthn::transport::{Channel as _, Device};
2016
use libwebauthn::webauthn::{Error as WebAuthnError, WebAuthn};
@@ -136,17 +132,18 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
136132
.unwrap();
137133
println!("WebAuthn MakeCredential response: {:?}", response);
138134

139-
let challenge: [u8; 32] = thread_rng().gen();
140-
let credential: Ctap2PublicKeyCredentialDescriptor =
141-
(&response.authenticator_data).try_into().unwrap();
142-
let get_assertion = GetAssertionRequest {
143-
relying_party_id: "example.org".to_owned(),
144-
hash: Vec::from(challenge),
145-
allow: vec![credential],
146-
user_verification: UserVerificationRequirement::Discouraged,
147-
extensions: GetAssertionRequestExtensions::default(),
148-
timeout: TIMEOUT,
149-
};
135+
let request_json = r#"
136+
{
137+
"challenge": "Y3JlZGVudGlhbHMtZm9yLWxpbnV4L2xpYndlYmF1dGhu",
138+
"timeout": 30000,
139+
"rpId": "example.org",
140+
"userVerification": "discouraged"
141+
}
142+
"#;
143+
let get_assertion: GetAssertionRequest =
144+
GetAssertionRequest::from_json(&rpid, request_json)
145+
.expect("Failed to parse request JSON");
146+
println!("WebAuthn GetAssertion request: {:?}", get_assertion);
150147

151148
let response = loop {
152149
match channel.webauthn_get_assertion(&get_assertion).await {

0 commit comments

Comments
 (0)