-
Notifications
You must be signed in to change notification settings - Fork 19
Web IDL support 1/N: GA, MC basic parsing #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlfioEmanueleFresta
wants to merge
12
commits into
master
Choose a base branch
from
json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2a47dfe
[WIP] Web IDL support (make credentials); next: extension parsing
AlfioEmanueleFresta e27a4b0
[WIP] More progress
AlfioEmanueleFresta 386a777
Make Credential parsing seems working; added example
AlfioEmanueleFresta 3cda38e
Clean up warnings
AlfioEmanueleFresta c455aed
GetAssertion IDL implementation
AlfioEmanueleFresta 7408575
Change back CTAP2 credential model to ByteBuf
AlfioEmanueleFresta eb3763d
Fixes and test for MC
AlfioEmanueleFresta cd713ab
Minor fix to test
AlfioEmanueleFresta fbd5fb8
Rebase: Fix use of HmacOrPrf::None variant
AlfioEmanueleFresta e22fb75
Adds basic parsing tests
AlfioEmanueleFresta deb8c10
Add tests for get assertion
AlfioEmanueleFresta 8aae271
Update example to use JSON
AlfioEmanueleFresta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| use std::error::Error; | ||
| use std::io::{self, Write}; | ||
| use std::time::Duration; | ||
|
|
||
| use libwebauthn::UvUpdate; | ||
| use text_io::read; | ||
| use tokio::sync::broadcast::Receiver; | ||
| use tracing_subscriber::{self, EnvFilter}; | ||
|
|
||
| use libwebauthn::ops::webauthn::{ | ||
| GetAssertionRequest, MakeCredentialRequest, RelyingPartyId, WebAuthnIDL as _, | ||
| }; | ||
| use libwebauthn::pin::PinRequestReason; | ||
| use libwebauthn::transport::hid::list_devices; | ||
| use libwebauthn::transport::{Channel as _, Device}; | ||
| use libwebauthn::webauthn::{Error as WebAuthnError, WebAuthn}; | ||
|
|
||
| const TIMEOUT: Duration = Duration::from_secs(10); | ||
|
|
||
| fn setup_logging() { | ||
| tracing_subscriber::fmt() | ||
| .with_env_filter(EnvFilter::from_default_env()) | ||
| .without_time() | ||
| .init(); | ||
| } | ||
|
|
||
| async fn handle_updates(mut state_recv: Receiver<UvUpdate>) { | ||
| while let Ok(update) = state_recv.recv().await { | ||
| match update { | ||
| UvUpdate::PresenceRequired => println!("Please touch your device!"), | ||
| UvUpdate::UvRetry { attempts_left } => { | ||
| print!("UV failed."); | ||
| if let Some(attempts_left) = attempts_left { | ||
| print!(" You have {attempts_left} attempts left."); | ||
| } | ||
| } | ||
| UvUpdate::PinRequired(update) => { | ||
| let mut attempts_str = String::new(); | ||
| if let Some(attempts) = update.attempts_left { | ||
| attempts_str = format!(". You have {attempts} attempts left!"); | ||
| }; | ||
|
|
||
| match update.reason { | ||
| PinRequestReason::RelyingPartyRequest => println!("RP required a PIN."), | ||
| PinRequestReason::AuthenticatorPolicy => { | ||
| println!("Your device requires a PIN.") | ||
| } | ||
| PinRequestReason::FallbackFromUV => { | ||
| println!("UV failed too often and is blocked. Falling back to PIN.") | ||
| } | ||
| } | ||
| print!("PIN: Please enter the PIN for your authenticator{attempts_str}: "); | ||
| io::stdout().flush().unwrap(); | ||
| let pin_raw: String = read!("{}\n"); | ||
|
|
||
| if pin_raw.is_empty() { | ||
| println!("PIN: No PIN provided, cancelling operation."); | ||
| update.cancel(); | ||
| } else { | ||
| let _ = update.send_pin(&pin_raw); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| pub async fn main() -> Result<(), Box<dyn Error>> { | ||
| setup_logging(); | ||
|
|
||
| let devices = list_devices().await.unwrap(); | ||
| println!("Devices found: {:?}", devices); | ||
|
|
||
| for mut device in devices { | ||
| println!("Selected HID authenticator: {}", &device); | ||
| let mut channel = device.channel().await?; | ||
| channel.wink(TIMEOUT).await?; | ||
|
|
||
| // Relying | ||
| let rpid = RelyingPartyId("example.org".to_owned()); | ||
| let request_json = r#" | ||
| { | ||
| "rp": { | ||
| "id": "example.org", | ||
| "name": "Example Relying Party" | ||
| }, | ||
| "user": { | ||
| "id": "MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4MTIzNDU2Nzg", | ||
| "name": "Mario Rossi", | ||
| "displayName": "Mario Rossi" | ||
| }, | ||
| "challenge": "MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4MTIzNDU2Nzg", | ||
| "pubKeyCredParams": [ | ||
| {"type": "public-key", "alg": -7} | ||
| ], | ||
| "timeout": 60000, | ||
| "excludeCredentials": [], | ||
| "authenticatorSelection": { | ||
| "residentKey": "discouraged", | ||
| "userVerification": "preferred" | ||
| }, | ||
| "attestation": "none" | ||
| } | ||
| "#; | ||
| let make_credentials_request: MakeCredentialRequest = | ||
| MakeCredentialRequest::from_json(&rpid, request_json) | ||
| .expect("Failed to parse request JSON"); | ||
| println!( | ||
| "WebAuthn MakeCredential request: {:?}", | ||
| make_credentials_request | ||
| ); | ||
|
|
||
| let state_recv = channel.get_ux_update_receiver(); | ||
| tokio::spawn(handle_updates(state_recv)); | ||
|
|
||
| let response = loop { | ||
| match channel | ||
| .webauthn_make_credential(&make_credentials_request) | ||
| .await | ||
| { | ||
| Ok(response) => break Ok(response), | ||
| Err(WebAuthnError::Ctap(ctap_error)) => { | ||
| if ctap_error.is_retryable_user_error() { | ||
| println!("Oops, try again! Error: {}", ctap_error); | ||
| continue; | ||
| } | ||
| break Err(WebAuthnError::Ctap(ctap_error)); | ||
| } | ||
| Err(err) => break Err(err), | ||
| }; | ||
| } | ||
| .unwrap(); | ||
| println!("WebAuthn MakeCredential response: {:?}", response); | ||
|
|
||
| let request_json = r#" | ||
| { | ||
| "challenge": "Y3JlZGVudGlhbHMtZm9yLWxpbnV4L2xpYndlYmF1dGhu", | ||
| "timeout": 30000, | ||
| "rpId": "example.org", | ||
| "userVerification": "discouraged" | ||
| } | ||
| "#; | ||
| let get_assertion: GetAssertionRequest = | ||
| GetAssertionRequest::from_json(&rpid, request_json) | ||
| .expect("Failed to parse request JSON"); | ||
| println!("WebAuthn GetAssertion request: {:?}", get_assertion); | ||
|
|
||
| let response = loop { | ||
| match channel.webauthn_get_assertion(&get_assertion).await { | ||
| Ok(response) => break Ok(response), | ||
| Err(WebAuthnError::Ctap(ctap_error)) => { | ||
| if ctap_error.is_retryable_user_error() { | ||
| println!("Oops, try again! Error: {}", ctap_error); | ||
| continue; | ||
| } | ||
| break Err(WebAuthnError::Ctap(ctap_error)); | ||
| } | ||
| Err(err) => break Err(err), | ||
| }; | ||
| } | ||
| .unwrap(); | ||
| println!("WebAuthn GetAssertion response: {:?}", response); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the type specifier
let hmac_or_prf: GetAssertionHmacOrPrfInputneeded? I think it could be removed.