示例#1
0
 def test_event(self):
     event = Event()
     timeout = Timeout(event)
     self.assertIsNone(timeout.timer)
     with timeout as e:
         self.assertIs(event, e)
         self.assertFalse(e.is_set())
     self.assertFalse(event.is_set())
示例#2
0
 def test_timer_stops(self):
     timeout = Timeout(10)
     self.assertFalse(timeout.timer.is_alive())
     with timeout as e:
         self.assertTrue(timeout.timer.is_alive())
         self.assertFalse(e.is_set())
     self.assertFalse(timeout.timer.is_alive())
     self.assertFalse(e.is_set())
示例#3
0
 def send_data_hid(self, cmd, data):
     if not isinstance(data, bytes):
         data = struct.pack("%dB" % len(data), *[ord(x) for x in data])
     with Timeout(1.0) as event:
         return self.dev.call(cmd, data, event)
示例#4
0
 def recv_raw(self, ):
     with Timeout(1.0) as t:
         cmd, payload = self.dev._dev.InternalRecv()
     return cmd, payload
示例#5
0
 def send_data(self, cmd, data):
     if type(data) != type(b""):
         data = struct.pack("%dB" % len(data), *[ord(x) for x in data])
     with Timeout(1.0) as event:
         return self.dev.call(cmd, data, event)
示例#6
0
 def send_data(self, cmd, data):
     #print('<<', hexlify(data))
     if type(data) != type(b''):
         data = struct.pack('%dB' % len(data), *[ord(x) for x in data])
     with Timeout(1.0) as event:
         return self.dev.call(cmd, data, event)
示例#7
0
 def test_timer_triggers(self):
     with Timeout(0.01) as e:
         self.assertTrue(e.wait(1.0))
     self.assertTrue(e.is_set())
示例#8
0
    def _get_assertion(self,
                       challenge,
                       rp_id,
                       timeout=120,
                       allow_credentials=None,
                       user_verification="preferred",
                       extensions=None):
        """
        Wrapper to generate assertions
        :param challenge: Websafe encoded challenge
        :param timeout: Max time a caller is willing to wait
        :param rp_id: Relying party identifier
        :param allow_credentials: List of acceptable PKCSs
        :param user_verification: Requirements for user verification
        :param extensions: Additional parameters
        :return: The assertion of type AssertionResponse and client data of
                type ClientData
        """
        # notify user that user interaction is required
        self._user_interaction.notify_user_interaction_required(
            self._user_name, self.relying_party_id)

        # set up variables
        result_queue = queue.Queue()
        issued_requests = []
        assertions = None
        assertion = None
        client_data = None
        original_sigint_handler = None

        # if no user defined abort event is set use SIGINT signal
        if not self._abort_event:
            self._abort_event = threading.Event()
            original_sigint_handler = signal.getsignal(signal.SIGINT)
            signal.signal(signal.SIGINT, self._signal_handler)

        # wait for the timeout to expire
        with Timeout(timeout) as stop_event:

            # start a thread to wait for a user defined abort signal
            abort_observer = threading.Thread(
                target=Fido2ClientWrapper._wait_for_abort,
                args=(self._abort_event, stop_event))
            abort_observer.start()

            # start a thread to discover devices
            device_listener = threading.Thread(
                target=Fido2ClientWrapper._discover_devices,
                args=(result_queue, stop_event))
            device_listener.start()

            # while there is no result
            while not (assertions and client_data) and not stop_event.is_set():

                # wait for device discovery or assertion response
                comm_obj = result_queue.get()

                # new device discovered
                if comm_obj[0] == CommunicationObject.DEVICE:
                    new_devices = comm_obj[1]

                    # send requests to all discovered devices
                    for device in new_devices:
                        thread = threading.Thread(
                            target=Fido2ClientWrapper._do_get_assertion,
                            args=(device, challenge, rp_id, allow_credentials,
                                  user_verification, extensions, result_queue,
                                  stop_event))
                        issued_requests.append(device.descriptor)
                        thread.start()

                # response received
                if comm_obj[0] == CommunicationObject.ASSERTION:
                    assertion_response = comm_obj[1]
                    assertions = assertion_response.assertions
                    client_data = assertion_response.client_data
                    issued_requests.remove(
                        assertion_response.device_descriptor)
                    if (not (assertions and client_data)
                            and assertion_response.user_interaction):
                        self._user_interaction.notify_authenticator_failed()

            # stop current threads
            stop_event.set()

            # select an assertion if necessary
            if assertions and client_data:
                stop_event.clear()
                assertion = self._select_assertion(assertions, stop_event)

            # handle user defined abort event
            if self._abort_event.is_set():
                # user canceled operation
                assertion = None
                client_data = None
                self.state = ClientState.canceled
            else:
                # terminate the thread waiting for the abort
                self._abort_event.set()
                self._abort_event.clear()

            # if the default abort event is used, reset the original SIGINT
            # handler
            if original_sigint_handler:
                signal.signal(signal.SIGINT, original_sigint_handler)
                self._abort_event = None

        return assertion, client_data