Public method to check if sleeping is allowed (#674)

This commit is contained in:
kaczmarczyck
2024-01-09 21:04:47 +01:00
committed by GitHub
parent ba0d717d88
commit 298db9ea99
3 changed files with 22 additions and 0 deletions

View File

@@ -140,6 +140,12 @@ impl<E: Env> ClientPin<E> {
} }
} }
/// Checks if a PIN UV token is in use.
pub fn has_token(&mut self, env: &mut E) -> bool {
self.update_timeouts(env);
self.pin_uv_auth_token_state.is_in_use()
}
/// Gets a reference to the PIN protocol of the given version. /// Gets a reference to the PIN protocol of the given version.
fn get_pin_protocol(&self, pin_uv_auth_protocol: PinUvAuthProtocol) -> &PinProtocol<E> { fn get_pin_protocol(&self, pin_uv_auth_protocol: PinUvAuthProtocol) -> &PinProtocol<E> {
match pin_uv_auth_protocol { match pin_uv_auth_protocol {
@@ -1507,9 +1513,11 @@ mod test {
let mut env = TestEnv::default(); let mut env = TestEnv::default();
let mut client_pin = ClientPin::<TestEnv>::new(&mut env); let mut client_pin = ClientPin::<TestEnv>::new(&mut env);
let message = [0xAA]; let message = [0xAA];
assert!(!client_pin.has_token(&mut env));
client_pin client_pin
.pin_uv_auth_token_state .pin_uv_auth_token_state
.begin_using_pin_uv_auth_token(&mut env); .begin_using_pin_uv_auth_token(&mut env);
assert!(client_pin.has_token(&mut env));
let pin_uv_auth_token_v1 = client_pin let pin_uv_auth_token_v1 = client_pin
.get_pin_protocol(PinUvAuthProtocol::V1) .get_pin_protocol(PinUvAuthProtocol::V1)
@@ -1655,6 +1663,7 @@ mod test {
.has_permissions_rp_id("example.com"), .has_permissions_rp_id("example.com"),
Ok(()) Ok(())
); );
assert!(client_pin.has_token(&mut env));
env.clock().advance(30001); env.clock().advance(30001);
client_pin.update_timeouts(&mut env); client_pin.update_timeouts(&mut env);
@@ -1672,6 +1681,7 @@ mod test {
.has_permissions_rp_id("example.com"), .has_permissions_rp_id("example.com"),
Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID)
); );
assert!(!client_pin.has_token(&mut env));
} }
#[test] #[test]

View File

@@ -602,6 +602,12 @@ impl<E: Env> CtapState<E> {
self.stateful_command_permission.clear_old_channels(channel); self.stateful_command_permission.clear_old_channels(channel);
} }
/// Checks if the application has any timers running.
pub fn can_sleep(&mut self, env: &mut E) -> bool {
!self.client_pin.has_token(env)
&& self.stateful_command_permission.get_command(env).is_err()
}
pub fn process_command( pub fn process_command(
&mut self, &mut self,
env: &mut E, env: &mut E,

View File

@@ -116,6 +116,10 @@ impl<E: Env> Ctap<E> {
self.hid.should_wink(&mut self.env) self.hid.should_wink(&mut self.env)
} }
pub fn can_sleep(&mut self) -> bool {
!self.should_wink() && self.state.can_sleep(&mut self.env)
}
#[cfg(feature = "with_ctap1")] #[cfg(feature = "with_ctap1")]
pub fn u2f_grant_user_presence(&mut self) { pub fn u2f_grant_user_presence(&mut self) {
self.state.u2f_grant_user_presence(&mut self.env) self.state.u2f_grant_user_presence(&mut self.env)
@@ -201,6 +205,7 @@ mod test {
fn test_hard_reset() { fn test_hard_reset() {
let env = TestEnv::default(); let env = TestEnv::default();
let mut ctap = Ctap::<TestEnv>::new(env); let mut ctap = Ctap::<TestEnv>::new(env);
assert!(!ctap.can_sleep());
// Send Init, receive Init response. // Send Init, receive Init response.
let mut init_response = ctap.process_hid_packet(&init_packet(), Transport::MainHid); let mut init_response = ctap.process_hid_packet(&init_packet(), Transport::MainHid);
@@ -223,6 +228,7 @@ mod test {
let mut env = TestEnv::default(); let mut env = TestEnv::default();
env.set_boots_after_soft_reset(true); env.set_boots_after_soft_reset(true);
let mut ctap = Ctap::<TestEnv>::new(env); let mut ctap = Ctap::<TestEnv>::new(env);
assert!(ctap.can_sleep());
// Send Init, receive Init response. // Send Init, receive Init response.
let mut init_response = ctap.process_hid_packet(&init_packet(), Transport::MainHid); let mut init_response = ctap.process_hid_packet(&init_packet(), Transport::MainHid);