Helper: More DeviceInfo improvements

This commit is contained in:
Dain Nilsson 2024-09-13 11:42:08 +02:00
parent d8e9cf34c9
commit 5ba095777e
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8

View File

@ -265,15 +265,14 @@ class AbstractDeviceNode(RpcNode):
super().__init__() super().__init__()
self._device = device self._device = device
self._info = None self._info = None
self._data = None self._data = self._refresh_data()
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
try: try:
response = super().__call__(*args, **kwargs) response = super().__call__(*args, **kwargs)
if "device_info" in response.flags: if "device_info" in response.flags:
old_info = self._info old_info = self._info
# Refresh data, and close any open child # Refresh data
self._close_child()
self._data = self._refresh_data() self._data = self._refresh_data()
if old_info == self._info: if old_info == self._info:
# No change to DeviceInfo, further propagation not needed. # No change to DeviceInfo, further propagation not needed.
@ -297,8 +296,6 @@ class AbstractDeviceNode(RpcNode):
raise NoSuchNodeException(name) raise NoSuchNodeException(name)
def get_data(self): def get_data(self):
if not self._data:
self._data = self._refresh_data()
return self._data return self._data
def _refresh_data(self): def _refresh_data(self):
@ -322,7 +319,6 @@ class UsbDeviceNode(AbstractDeviceNode):
def _create_connection(self, conn_type): def _create_connection(self, conn_type):
connection = self._device.open_connection(conn_type) connection = self._device.open_connection(conn_type)
self._data = self._read_data(connection)
return ConnectionNode(self._device, connection, self._info) return ConnectionNode(self._device, connection, self._info)
def _refresh_data(self): def _refresh_data(self):
@ -332,7 +328,7 @@ class UsbDeviceNode(AbstractDeviceNode):
self._child._close_child() self._child._close_child()
return self._read_data(self._child._connection) return self._read_data(self._child._connection)
# New connection # No child, open new connection
for conn_type in (SmartCardConnection, OtpConnection, FidoConnection): for conn_type in (SmartCardConnection, OtpConnection, FidoConnection):
if self._supports_connection(conn_type): if self._supports_connection(conn_type):
try: try:
@ -396,10 +392,10 @@ RESTRICTED_NDEF = bytes.fromhex("001fd1011b5504") + b"yubico.com/getting-started
class ReaderDeviceNode(AbstractDeviceNode): class ReaderDeviceNode(AbstractDeviceNode):
def __init__(self, device): def __init__(self, device):
super().__init__(device)
self._observer = _ReaderObserver(device) self._observer = _ReaderObserver(device)
self._monitor = CardMonitor() self._monitor = CardMonitor()
self._monitor.addObserver(self._observer) self._monitor.addObserver(self._observer)
super().__init__(device)
def close(self): def close(self):
self._monitor.deleteObserver(self._observer) self._monitor.deleteObserver(self._observer)
@ -407,7 +403,7 @@ class ReaderDeviceNode(AbstractDeviceNode):
def get_data(self): def get_data(self):
if self._observer.needs_refresh: if self._observer.needs_refresh:
self._data = None self._data = self._refresh_data()
return super().get_data() return super().get_data()
def _read_data(self, conn): def _read_data(self, conn):
@ -418,6 +414,7 @@ class ReaderDeviceNode(AbstractDeviceNode):
if card is None: if card is None:
return dict(present=False, status="no-card") return dict(present=False, status="no-card")
try: try:
self._close_child()
with self._device.open_connection(SmartCardConnection) as conn: with self._device.open_connection(SmartCardConnection) as conn:
try: try:
data = self._read_data(conn) data = self._read_data(conn)
@ -449,7 +446,6 @@ class ReaderDeviceNode(AbstractDeviceNode):
def ccid(self): def ccid(self):
try: try:
connection = self._device.open_connection(SmartCardConnection) connection = self._device.open_connection(SmartCardConnection)
self._data = self._read_data(connection)
return ScpConnectionNode(self._device, connection, self._info) return ScpConnectionNode(self._device, connection, self._info)
except (ValueError, SmartcardException, EstablishContextException) as e: except (ValueError, SmartcardException, EstablishContextException) as e:
logger.warning("Error opening connection", exc_info=True) logger.warning("Error opening connection", exc_info=True)
@ -458,8 +454,6 @@ class ReaderDeviceNode(AbstractDeviceNode):
@child @child
def fido(self): def fido(self):
try: try:
with self._device.open_connection(SmartCardConnection) as conn:
self._data = self._read_data(conn)
connection = self._device.open_connection(FidoConnection) connection = self._device.open_connection(FidoConnection)
return ConnectionNode(self._device, connection, self._info) return ConnectionNode(self._device, connection, self._info)
except (ValueError, SmartcardException, EstablishContextException) as e: except (ValueError, SmartcardException, EstablishContextException) as e: