From 5c59e809c2d9980eb8396cba42004e460001daa3 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Wed, 2 Mar 2022 17:42:30 +0100 Subject: [PATCH] Make user feedback fields private --- src/ctap/hid/mod.rs | 6 +++++- src/ctap/mod.rs | 12 +++++++++++- src/lib.rs | 5 +++++ src/main.rs | 9 ++++----- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/ctap/hid/mod.rs b/src/ctap/hid/mod.rs index 3daf2f9..e6ff8f5 100644 --- a/src/ctap/hid/mod.rs +++ b/src/ctap/hid/mod.rs @@ -71,7 +71,7 @@ pub struct CtapHid { // In packets, the ID encoding is Big Endian to match what is used throughout CTAP (with the // u32::to/from_be_bytes methods). allocated_cids: usize, - pub wink_permission: TimedPermission, + pub(crate) wink_permission: TimedPermission, } #[allow(dead_code)] @@ -399,6 +399,10 @@ impl CtapHid { .unwrap() } + pub fn should_wink(&self, now: ClockValue) -> bool { + self.wink_permission.is_granted(now) + } + #[cfg(feature = "with_ctap1")] fn ctap1_error_message( cid: ChannelID, diff --git a/src/ctap/mod.rs b/src/ctap/mod.rs index b2c0e58..dba8ed9 100644 --- a/src/ctap/mod.rs +++ b/src/ctap/mod.rs @@ -334,7 +334,7 @@ pub struct CtapState { persistent_store: PersistentStore, client_pin: ClientPin, #[cfg(feature = "with_ctap1")] - pub u2f_up_state: U2fUserPresenceState, + pub(crate) u2f_up_state: U2fUserPresenceState, // The state initializes to Reset and its timeout, and never goes back to Reset. stateful_command_permission: StatefulPermission, large_blobs: LargeBlobs, @@ -1332,6 +1332,16 @@ impl CtapState { auth_data.extend(&signature_counter); Ok(auth_data) } + + #[cfg(feature = "with_ctap1")] + pub fn u2f_grant_user_presence(&mut self, now: ClockValue) { + self.u2f_up_state.grant_up(now) + } + + #[cfg(feature = "with_ctap1")] + pub fn u2f_needs_user_presence(&mut self, now: ClockValue) -> bool { + self.u2f_up_state.is_up_needed(now) + } } #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index df4b2a6..5826f29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,4 +57,9 @@ impl Ctap { self.hid .process_hid_packet(&mut self.env, packet, now, &mut self.state) } + + pub fn update_timeouts(&mut self, now: ClockValue) { + self.state.update_timeouts(now); + self.hid.wink_permission = self.hid.wink_permission.check_expiration(now); + } } diff --git a/src/main.rs b/src/main.rs index 4e49a65..d8d67ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,7 +103,7 @@ fn main() { #[cfg(feature = "with_ctap1")] { if button_touched.get() { - ctap.state().u2f_up_state.grant_up(now); + ctap.state().u2f_grant_user_presence(now); } // Cleanup button callbacks. We miss button presses while processing though. // Heavy computation mostly follows a registered touch luckily. Unregistering @@ -117,8 +117,7 @@ fn main() { // These calls are making sure that even for long inactivity, wrapping clock values // don't cause problems with timers. - ctap.state().update_timeouts(now); - ctap.hid().wink_permission = ctap.hid().wink_permission.check_expiration(now); + ctap.update_timeouts(now); if has_packet { let reply = ctap.process_hid_packet(&pkt_request, now); @@ -161,14 +160,14 @@ fn main() { last_led_increment = now; } - if ctap.hid().wink_permission.is_granted(now) { + if ctap.hid().should_wink(now) { wink_leds(led_counter); } else { #[cfg(not(feature = "with_ctap1"))] switch_off_leds(); #[cfg(feature = "with_ctap1")] { - if ctap.state().u2f_up_state.is_up_needed(now) { + if ctap.state().u2f_needs_user_presence(now) { // Flash the LEDs with an almost regular pattern. The inaccuracy comes from // delay caused by processing and sending of packets. blink_leds(led_counter);