Skip to content

Commit a572262

Browse files
author
Martin Sirringhaus
committed
Add logging-possiblity to send_msg()-functiongroup
1 parent b21f8fa commit a572262

File tree

17 files changed

+243
-79
lines changed

17 files changed

+243
-79
lines changed

examples/ctap2.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn main() {
4848
opts.optflag("s", "hmac_secret", "With hmac-secret");
4949
opts.optflag("h", "help", "print this help menu");
5050
opts.optflag("f", "fallback", "Use CTAP1 fallback implementation");
51+
opts.optflag("l", "logging", "Active request/response logging");
5152
let matches = match opts.parse(&args[1..]) {
5253
Ok(m) => m,
5354
Err(f) => panic!("{}", f.to_string()),
@@ -81,6 +82,7 @@ fn main() {
8182
let mut chall_bytes = [0u8; 32];
8283
thread_rng().fill_bytes(&mut chall_bytes);
8384

85+
let do_logging = matches.opt_present("logging");
8486
let (status_tx, status_rx) = channel::<StatusUpdate>();
8587
thread::spawn(move || loop {
8688
match status_rx.recv() {
@@ -136,6 +138,13 @@ fn main() {
136138
Ok(StatusUpdate::SelectResultNotice(_, _)) => {
137139
panic!("Unexpected select device notice")
138140
}
141+
Ok(StatusUpdate::RequestLogging(dir, msg)) => {
142+
if do_logging {
143+
println!("{dir:?} -> ");
144+
println!("{msg}");
145+
println!("--------------------------------------");
146+
}
147+
}
139148
Err(RecvError) => {
140149
println!("STATUS: end");
141150
return;

examples/ctap2_discoverable_creds.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use authenticator::{
1515
};
1616
use getopts::Options;
1717
use sha2::{Digest, Sha256};
18+
use std::io::Write;
1819
use std::sync::mpsc::{channel, RecvError};
1920
use std::{env, io, thread};
20-
use std::io::Write;
2121

2222
fn print_usage(program: &str, opts: Options) {
2323
println!("------------------------------------------------------------------------");
@@ -60,7 +60,12 @@ fn ask_user_choice(choices: &[PublicKeyCredentialUserEntity]) -> Option<usize> {
6060
}
6161
}
6262

63-
fn register_user(manager: &mut AuthenticatorService, username: &str, timeout_ms: u64) {
63+
fn register_user(
64+
manager: &mut AuthenticatorService,
65+
username: &str,
66+
timeout_ms: u64,
67+
do_logging: bool,
68+
) {
6469
println!();
6570
println!("*********************************************************************");
6671
println!("Asking a security key to register now with user: {username}");
@@ -133,6 +138,13 @@ fn register_user(manager: &mut AuthenticatorService, username: &str, timeout_ms:
133138
Ok(StatusUpdate::SelectResultNotice(_, _)) => {
134139
panic!("Unexpected select result notice")
135140
}
141+
Ok(StatusUpdate::RequestLogging(dir, msg)) => {
142+
if do_logging {
143+
println!("{dir:?} -> ");
144+
println!("{msg}");
145+
println!("--------------------------------------");
146+
}
147+
}
136148
Err(RecvError) => {
137149
println!("STATUS: end");
138150
return;
@@ -216,12 +228,10 @@ fn main() {
216228
"timeout in seconds",
217229
"SEC",
218230
);
219-
opts.optflag(
220-
"s",
221-
"skip_reg",
222-
"Skip registration");
231+
opts.optflag("s", "skip_reg", "Skip registration");
223232

224233
opts.optflag("h", "help", "print this help menu");
234+
opts.optflag("l", "logging", "Active request/response logging");
225235
let matches = match opts.parse(&args[1..]) {
226236
Ok(m) => m,
227237
Err(f) => panic!("{}", f.to_string()),
@@ -247,9 +257,10 @@ fn main() {
247257
}
248258
};
249259

260+
let do_logging = matches.opt_present("logging");
250261
if !matches.opt_present("skip_reg") {
251262
for username in &["A. User", "A. Nother", "Dr. Who"] {
252-
register_user(&mut manager, username, timeout_ms)
263+
register_user(&mut manager, username, timeout_ms, do_logging)
253264
}
254265
}
255266

@@ -324,6 +335,13 @@ fn main() {
324335
let idx = ask_user_choice(&users);
325336
index_sender.send(idx).expect("Failed to send choice");
326337
}
338+
Ok(StatusUpdate::RequestLogging(dir, msg)) => {
339+
if do_logging {
340+
println!("{dir:?} -> ");
341+
println!("{msg}");
342+
println!("--------------------------------------");
343+
}
344+
}
327345
Err(RecvError) => {
328346
println!("STATUS: end");
329347
return;
@@ -368,7 +386,13 @@ fn main() {
368386
println!("Found credentials:");
369387
println!(
370388
"{:?}",
371-
assertion_object.assertion.user.clone().unwrap().name.unwrap() // Unwrapping here, as these shouldn't fail
389+
assertion_object
390+
.assertion
391+
.user
392+
.clone()
393+
.unwrap()
394+
.name
395+
.unwrap() // Unwrapping here, as these shouldn't fail
372396
);
373397
println!("-----------------------------------------------------------------");
374398
println!("Done.");

examples/interactive_management.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ fn handle_bio_enrollments(
573573
}
574574
}
575575

576-
fn interactive_status_callback(status_rx: Receiver<StatusUpdate>) {
576+
fn interactive_status_callback(status_rx: Receiver<StatusUpdate>, do_logging: bool) {
577577
let mut tx = None;
578578
let mut auth_info = None;
579579
loop {
@@ -730,6 +730,13 @@ fn interactive_status_callback(status_rx: Receiver<StatusUpdate>) {
730730
Ok(StatusUpdate::SelectResultNotice(_, _)) => {
731731
panic!("Unexpected select device notice")
732732
}
733+
Ok(StatusUpdate::RequestLogging(dir, msg)) => {
734+
if do_logging {
735+
println!("{dir:?} -> ");
736+
println!("{msg}");
737+
println!("--------------------------------------");
738+
}
739+
}
733740
Err(RecvError) => {
734741
println!("STATUS: end");
735742
return;
@@ -752,6 +759,7 @@ fn main() {
752759
"SEC",
753760
);
754761
opts.optflag("h", "help", "print this help menu");
762+
opts.optflag("l", "logging", "Active request/response logging");
755763
let matches = match opts.parse(&args[1..]) {
756764
Ok(m) => m,
757765
Err(f) => panic!("{}", f.to_string()),
@@ -777,8 +785,9 @@ fn main() {
777785
}
778786
};
779787

788+
let do_logging = matches.opt_present("logging");
780789
let (status_tx, status_rx) = channel::<StatusUpdate>();
781-
thread::spawn(move || interactive_status_callback(status_rx));
790+
thread::spawn(move || interactive_status_callback(status_rx, do_logging));
782791

783792
let (manage_tx, manage_rx) = channel();
784793
let state_callback =

examples/set_pin.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn main() {
3333
Ok(m) => m,
3434
Err(f) => panic!("{}", f.to_string()),
3535
};
36+
opts.optflag("l", "logging", "Active request/response logging");
3637
if matches.opt_present("help") {
3738
print_usage(&program, opts);
3839
return;
@@ -62,6 +63,7 @@ fn main() {
6263
return;
6364
}
6465

66+
let do_logging = matches.opt_present("logging");
6567
let (status_tx, status_rx) = channel::<StatusUpdate>();
6668
thread::spawn(move || loop {
6769
match status_rx.recv() {
@@ -117,6 +119,13 @@ fn main() {
117119
Ok(StatusUpdate::SelectResultNotice(_, _)) => {
118120
panic!("Unexpected select device notice")
119121
}
122+
Ok(StatusUpdate::RequestLogging(dir, msg)) => {
123+
if do_logging {
124+
println!("{dir:?} -> ");
125+
println!("{msg}");
126+
println!("--------------------------------------");
127+
}
128+
}
120129
Err(RecvError) => {
121130
println!("STATUS: end");
122131
return;

examples/test_exclude_list.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn main() {
4444
Ok(m) => m,
4545
Err(f) => panic!("{}", f.to_string()),
4646
};
47+
opts.optflag("l", "logging", "Active request/response logging");
4748
if matches.opt_present("help") {
4849
print_usage(&program, opts);
4950
return;
@@ -76,6 +77,7 @@ fn main() {
7677
challenge.update(challenge_str.as_bytes());
7778
let chall_bytes = challenge.finalize().into();
7879

80+
let do_logging = matches.opt_present("logging");
7981
let (status_tx, status_rx) = channel::<StatusUpdate>();
8082
thread::spawn(move || loop {
8183
match status_rx.recv() {
@@ -131,6 +133,13 @@ fn main() {
131133
Ok(StatusUpdate::SelectResultNotice(_, _)) => {
132134
panic!("Unexpected select device notice")
133135
}
136+
Ok(StatusUpdate::RequestLogging(dir, msg)) => {
137+
if do_logging {
138+
println!("{dir:?} -> ");
139+
println!("{msg}");
140+
println!("--------------------------------------");
141+
}
142+
}
134143
Err(RecvError) => {
135144
println!("STATUS: end");
136145
return;

src/authenticatorservice.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use crate::ctap2::server::{
1111
use crate::errors::*;
1212
use crate::manager::Manager;
1313
use crate::statecallback::StateCallback;
14+
use serde::Serialize;
1415
use std::sync::{mpsc::Sender, Arc, Mutex};
1516

16-
#[derive(Debug, Clone)]
17+
#[derive(Debug, Clone, Serialize)]
1718
pub struct RegisterArgs {
1819
pub client_data_hash: [u8; 32],
1920
pub relying_party: RelyingParty,
@@ -28,7 +29,7 @@ pub struct RegisterArgs {
2829
pub use_ctap1_fallback: bool,
2930
}
3031

31-
#[derive(Debug, Clone)]
32+
#[derive(Debug, Clone, Serialize)]
3233
pub struct SignArgs {
3334
pub client_data_hash: [u8; 32],
3435
pub origin: String,

src/ctap2/commands/get_assertion.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl RequestCtap2 for GetAssertion {
416416
let msg = GetNextAssertion;
417417
// We already have one, so skipping 0
418418
for _ in 1..number_of_credentials {
419-
let assertion = dev.send_cbor(&msg)?;
419+
let assertion = dev.send_cbor(&msg, None)?;
420420
results.push(GetAssertionResult {
421421
assertion: assertion.into(),
422422
attachment: AuthenticatorAttachment::Unknown,
@@ -631,6 +631,7 @@ pub mod test {
631631
use crate::transport::{FidoDevice, FidoDeviceIO, FidoProtocol};
632632
use crate::u2ftypes::U2FDeviceInfo;
633633
use rand::{thread_rng, RngCore};
634+
use std::sync::mpsc::channel;
634635

635636
#[test]
636637
fn test_get_assertion_ctap2() {
@@ -788,7 +789,7 @@ pub mod test {
788789
attachment: AuthenticatorAttachment::Unknown,
789790
extensions: Default::default(),
790791
}];
791-
let response = device.send_cbor(&assertion).unwrap();
792+
let response = device.send_cbor(&assertion, None).unwrap();
792793
assert_eq!(response, expected);
793794
}
794795

@@ -869,6 +870,7 @@ pub mod test {
869870
device.set_cid(cid);
870871

871872
// ctap1 request
873+
let (tx, _rx) = channel();
872874
fill_device_ctap1(
873875
&mut device,
874876
cid,
@@ -880,6 +882,7 @@ pub mod test {
880882
&assertion.allow_list,
881883
&assertion.rp,
882884
&assertion.client_data_hash,
885+
&tx,
883886
)
884887
.expect("Did not find a key_handle, even though it should have");
885888
assertion.allow_list = vec![key_handle];
@@ -892,7 +895,7 @@ pub mod test {
892895
// Pre-flighting is not done automatically
893896
fill_device_ctap1(&mut device, cid, U2F_REQUEST_USER_PRESENCE, SW_NO_ERROR);
894897

895-
let response = device.send_ctap1(&assertion).unwrap();
898+
let response = device.send_ctap1(&assertion, None).unwrap();
896899

897900
// Check if response is correct
898901
let expected_auth_data = AuthenticatorData {
@@ -958,12 +961,14 @@ pub mod test {
958961

959962
device.set_cid(cid);
960963

964+
let (tx, _rx) = channel();
961965
assert_matches!(
962966
do_credential_list_filtering_ctap1(
963967
&mut device,
964968
&assertion.allow_list,
965969
&assertion.rp,
966970
&assertion.client_data_hash,
971+
&tx,
967972
),
968973
None
969974
);
@@ -981,12 +986,14 @@ pub mod test {
981986
for allow_list in [vec![], vec![too_long_key_handle.clone(); 5]] {
982987
assertion.allow_list = allow_list;
983988

989+
let (tx, _rx) = channel();
984990
assert_matches!(
985991
do_credential_list_filtering_ctap1(
986992
&mut device,
987993
&assertion.allow_list,
988994
&assertion.rp,
989995
&assertion.client_data_hash,
996+
&tx,
990997
),
991998
None
992999
);
@@ -1017,11 +1024,13 @@ pub mod test {
10171024
U2F_CHECK_IS_REGISTERED,
10181025
SW_CONDITIONS_NOT_SATISFIED,
10191026
);
1027+
let (tx, _rx) = channel();
10201028
let key_handle = do_credential_list_filtering_ctap1(
10211029
&mut device,
10221030
&assertion.allow_list,
10231031
&assertion.rp,
10241032
&assertion.client_data_hash,
1033+
&tx,
10251034
)
10261035
.expect("Did not find a key_handle, even though it should have");
10271036
assertion.allow_list = vec![key_handle];
@@ -1034,7 +1043,7 @@ pub mod test {
10341043
// Pre-flighting is not done automatically
10351044
fill_device_ctap1(&mut device, cid, U2F_REQUEST_USER_PRESENCE, SW_NO_ERROR);
10361045

1037-
let response = device.send_ctap1(&assertion).unwrap();
1046+
let response = device.send_ctap1(&assertion, None).unwrap();
10381047

10391048
// Check if response is correct
10401049
let expected_auth_data = AuthenticatorData {
@@ -1299,12 +1308,14 @@ pub mod test {
12991308
msg.extend(&GET_ASSERTION_SAMPLE_RESPONSE_CTAP2[293..]);
13001309
device.add_read(&msg, 0);
13011310

1311+
let (tx, _rx) = channel();
13021312
assert_matches!(
13031313
do_credential_list_filtering_ctap2(
13041314
&mut device,
13051315
&assertion.allow_list,
13061316
&assertion.rp,
13071317
None,
1318+
&tx,
13081319
),
13091320
Ok(..)
13101321
);

src/ctap2/commands/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub(crate) fn repackage_pin_errors<D: FidoDevice>(
160160
let cmd = GetPinRetries::new();
161161
// Treat any error as if the device returned a valid response without a pinRetries
162162
// field.
163-
let resp = dev.send_cbor(&cmd).unwrap_or_default();
163+
let resp = dev.send_cbor(&cmd, None).unwrap_or_default();
164164
AuthenticatorError::PinError(PinError::InvalidPin(resp.pin_retries))
165165
}
166166
HIDError::Command(CommandError::StatusCode(StatusCode::PinAuthBlocked, _)) => {
@@ -183,7 +183,7 @@ pub(crate) fn repackage_pin_errors<D: FidoDevice>(
183183
let cmd = GetUvRetries::new();
184184
// Treat any error as if the device returned a valid response without a uvRetries
185185
// field.
186-
let resp = dev.send_cbor(&cmd).unwrap_or_default();
186+
let resp = dev.send_cbor(&cmd, None).unwrap_or_default();
187187
AuthenticatorError::PinError(PinError::InvalidUv(resp.uv_retries))
188188
}
189189
HIDError::Command(CommandError::StatusCode(StatusCode::UvBlocked, _)) => {

src/ctap2/commands/reset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub mod tests {
7979
msg.extend(add); // + maybe additional data
8080
device.add_read(&msg, 0);
8181

82-
device.send_cbor(&Reset {})
82+
device.send_cbor(&Reset {}, None)
8383
}
8484

8585
#[test]

0 commit comments

Comments
 (0)