From ff654585ae102d5333bae6140f44eed2c739f3a4 Mon Sep 17 00:00:00 2001 From: Markus Meissner Date: Wed, 16 Oct 2024 23:57:23 +0200 Subject: [PATCH] apply checks and fixes --- nitrokeyapp/__init__.py | 2 - nitrokeyapp/__main__.py | 4 +- nitrokeyapp/common_ui.py | 2 +- nitrokeyapp/device_data.py | 4 +- nitrokeyapp/device_manager.py | 2 +- nitrokeyapp/gui.py | 30 +++-------- nitrokeyapp/logger.py | 8 +-- nitrokeyapp/nk3_button.py | 14 ++--- nitrokeyapp/overview_tab/__init__.py | 8 +-- nitrokeyapp/secrets_tab/__init__.py | 78 +++++++--------------------- nitrokeyapp/secrets_tab/data.py | 4 +- nitrokeyapp/secrets_tab/ui.py | 3 +- nitrokeyapp/secrets_tab/worker.py | 24 +++------ 13 files changed, 47 insertions(+), 136 deletions(-) diff --git a/nitrokeyapp/__init__.py b/nitrokeyapp/__init__.py index 771da238..b7886a05 100644 --- a/nitrokeyapp/__init__.py +++ b/nitrokeyapp/__init__.py @@ -1,8 +1,6 @@ """Experimental Nitrokey GUI Application - based on the Nitrokey Python SDK""" import importlib.metadata -import pathlib -import sys try: __version__ = importlib.metadata.version(__name__) diff --git a/nitrokeyapp/__main__.py b/nitrokeyapp/__main__.py index 1114eb6c..562dd649 100644 --- a/nitrokeyapp/__main__.py +++ b/nitrokeyapp/__main__.py @@ -11,9 +11,7 @@ @contextmanager def exception_handler( - hook: Callable[ - [Type[BaseException], BaseException, Optional[TracebackType]], Any - ], + hook: Callable[[Type[BaseException], BaseException, Optional[TracebackType]], Any], ) -> Generator[None, None, None]: old_hook = sys.excepthook sys.excepthook = hook diff --git a/nitrokeyapp/common_ui.py b/nitrokeyapp/common_ui.py index bc7bb992..fedfb83d 100644 --- a/nitrokeyapp/common_ui.py +++ b/nitrokeyapp/common_ui.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -from PySide6.QtCore import Signal, QObject +from PySide6.QtCore import QObject, Signal from nitrokeyapp.information_box import InfoUi from nitrokeyapp.progress_box import ProgressUi diff --git a/nitrokeyapp/device_data.py b/nitrokeyapp/device_data.py index 4bca4a7a..fc345994 100644 --- a/nitrokeyapp/device_data.py +++ b/nitrokeyapp/device_data.py @@ -88,9 +88,7 @@ def open(self) -> NK3: return device else: # TODO: improve error handling - raise RuntimeError( - f"Failed to open device {self.uuid} at {self.path}" - ) + raise RuntimeError(f"Failed to open device {self.uuid} at {self.path}") def update( self, diff --git a/nitrokeyapp/device_manager.py b/nitrokeyapp/device_manager.py index a8c5dedb..53f6ad81 100644 --- a/nitrokeyapp/device_manager.py +++ b/nitrokeyapp/device_manager.py @@ -1,5 +1,5 @@ import logging -from typing import List, Iterator +from typing import Iterator, List from nitrokeyapp.device_data import DeviceData diff --git a/nitrokeyapp/gui.py b/nitrokeyapp/gui.py index f0d4c3c8..08142955 100644 --- a/nitrokeyapp/gui.py +++ b/nitrokeyapp/gui.py @@ -97,33 +97,19 @@ def __init__(self, qt_app: QtWidgets.QApplication, log_file: str): view.common_ui.touch.stop.connect(self.touch_dialog.stop) view.common_ui.info.info.connect(self.info_box.set_status) - view.common_ui.info.error.connect( - self.info_box.set_error_status - ) - view.common_ui.info.pin_cached.connect( - self.info_box.set_pin_icon - ) - view.common_ui.info.pin_cleared.connect( - self.info_box.unset_pin_icon - ) - self.info_box.pin_pressed.connect( - view.common_ui.info.pin_pressed - ) + view.common_ui.info.error.connect(self.info_box.set_error_status) + view.common_ui.info.pin_cached.connect(self.info_box.set_pin_icon) + view.common_ui.info.pin_cleared.connect(self.info_box.unset_pin_icon) + self.info_box.pin_pressed.connect(view.common_ui.info.pin_pressed) view.common_ui.prompt.confirm.connect(self.prompt_box.confirm) - self.prompt_box.confirmed.connect( - view.common_ui.prompt.confirmed - ) + self.prompt_box.confirmed.connect(view.common_ui.prompt.confirmed) view.common_ui.progress.start.connect(self.progress_box.show) view.common_ui.progress.stop.connect(self.progress_box.hide) - view.common_ui.progress.progress.connect( - self.progress_box.update - ) + view.common_ui.progress.progress.connect(self.progress_box.update) - view.common_ui.gui.refresh_devices.connect( - self.refresh_devices - ) + view.common_ui.gui.refresh_devices.connect(self.refresh_devices) # main window widgets self.home_button = self.ui.btn_home @@ -204,7 +190,7 @@ def update_devices(self) -> None: """update device button view based on `self.device_manager` contents""" for widget in self.device_buttons: - widget.setParent(None) + widget.setParent(None) # type: ignore [call-overload] widget.destroy() self.device_buttons.clear() diff --git a/nitrokeyapp/logger.py b/nitrokeyapp/logger.py index d5009685..95f0658f 100644 --- a/nitrokeyapp/logger.py +++ b/nitrokeyapp/logger.py @@ -18,9 +18,7 @@ @contextmanager def init_logging() -> Generator[str, None, None]: - log_file = NamedTemporaryFile( - prefix="nitrokey-app2.", suffix=".log", delete=False - ) + log_file = NamedTemporaryFile(prefix="nitrokey-app2.", suffix=".log", delete=False) log_format = "%(relativeCreated)-8d %(levelname)6s %(name)10s %(message)s" try: @@ -33,9 +31,7 @@ def init_logging() -> Generator[str, None, None]: if log_to_console: handlers.append(console_handler) # type: ignore - logging.basicConfig( - format=log_format, level=logging.DEBUG, handlers=handlers - ) + logging.basicConfig(format=log_format, level=logging.DEBUG, handlers=handlers) yield log_file.name finally: diff --git a/nitrokeyapp/nk3_button.py b/nitrokeyapp/nk3_button.py index dcc121c8..54c4d975 100644 --- a/nitrokeyapp/nk3_button.py +++ b/nitrokeyapp/nk3_button.py @@ -1,4 +1,4 @@ -from typing import Optional, Callable +from typing import Callable, Optional from PySide6 import QtCore, QtGui, QtWidgets @@ -73,15 +73,11 @@ def stop_touch(self) -> None: self.effect.setStrength(0) def fold(self) -> None: - self.setText( - self.data.uuid_prefix if not self.data.is_bootloader else "BL" - ) + self.setText(self.data.uuid_prefix if not self.data.is_bootloader else "BL") self.setMinimumWidth(58) self.setMaximumWidth(58) self.setIconSize(QtCore.QSize(40, 40)) - self.setToolButtonStyle( - QtCore.Qt.ToolButtonStyle.ToolButtonTextUnderIcon - ) + self.setToolButtonStyle(QtCore.Qt.ToolButtonStyle.ToolButtonTextUnderIcon) def unfold(self) -> None: self.setChecked(False) @@ -89,6 +85,4 @@ def unfold(self) -> None: self.setMinimumWidth(178) self.setMaximumWidth(178) self.setIconSize(QtCore.QSize(32, 32)) - self.setToolButtonStyle( - QtCore.Qt.ToolButtonStyle.ToolButtonTextBesideIcon - ) + self.setToolButtonStyle(QtCore.Qt.ToolButtonStyle.ToolButtonTextBesideIcon) diff --git a/nitrokeyapp/overview_tab/__init__.py b/nitrokeyapp/overview_tab/__init__.py index d627f403..1b57ad45 100644 --- a/nitrokeyapp/overview_tab/__init__.py +++ b/nitrokeyapp/overview_tab/__init__.py @@ -192,16 +192,12 @@ def more_options(self) -> None: self.hide_more_options() def show_more_options(self) -> None: - self.ui.btn_more_options.setIcon( - QtUtilsMixIn.get_qicon("down_arrow.svg") - ) + self.ui.btn_more_options.setIcon(QtUtilsMixIn.get_qicon("down_arrow.svg")) oSize = self.ui.frame_more_options.sizeHint() self.ui.frame_more_options.setFixedHeight(oSize.height()) def hide_more_options(self) -> None: - self.ui.btn_more_options.setIcon( - QtUtilsMixIn.get_qicon("right_arrow.svg") - ) + self.ui.btn_more_options.setIcon(QtUtilsMixIn.get_qicon("right_arrow.svg")) self.ui.frame_more_options.setFixedHeight(0) @Slot(bool) diff --git a/nitrokeyapp/secrets_tab/__init__.py b/nitrokeyapp/secrets_tab/__init__.py index e568f2bc..5179b261 100644 --- a/nitrokeyapp/secrets_tab/__init__.py +++ b/nitrokeyapp/secrets_tab/__init__.py @@ -81,22 +81,14 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: self.trigger_check_device.connect(self._worker.check_device) self.trigger_delete_credential.connect(self._worker.delete_credential) self.trigger_generate_otp.connect(self._worker.generate_otp) - self.trigger_refresh_credentials.connect( - self._worker.refresh_credentials - ) + self.trigger_refresh_credentials.connect(self._worker.refresh_credentials) self.trigger_get_credential.connect(self._worker.get_credential) self.trigger_edit_credential.connect(self._worker.edit_credential) - self._worker.pin_cache.pin_cleared.connect( - self.common_ui.info.pin_cleared - ) - self._worker.pin_cache.pin_cleared.connect( - lambda: self.uncheck_checkbox(True) - ) + self._worker.pin_cache.pin_cleared.connect(self.common_ui.info.pin_cleared) + self._worker.pin_cache.pin_cleared.connect(lambda: self.uncheck_checkbox(True)) - self._worker.pin_cache.pin_cached.connect( - self.common_ui.info.pin_cached - ) + self._worker.pin_cache.pin_cached.connect(self.common_ui.info.pin_cached) self.common_ui.info.pin_pressed.connect(self._worker.pin_cache.clear) self._worker.credential_added.connect(self.credential_added) @@ -107,12 +99,8 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: self._worker.otp_generated.connect(self.otp_generated) self._worker.uncheck_checkbox.connect(self.uncheck_checkbox) - self._worker.received_credential.connect( - self.handle_receive_credential - ) - self.next_credential_receiver: Optional[ - Callable[[Credential], None] - ] = None + self._worker.received_credential.connect(self.handle_receive_credential) + self.next_credential_receiver: Optional[Callable[[Credential], None]] = None self.data: Optional[DeviceData] = None self.active_credential: Optional[Credential] = None @@ -145,9 +133,7 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: lambda: self.act_copy_line_edit(self.ui.password) ) - self.action_password_show = self.ui.password.addAction( - icon_visibility, loc - ) + self.action_password_show = self.ui.password.addAction(icon_visibility, loc) self.action_password_show.triggered.connect(self.act_password_show) self.action_comment_copy = self.ui.comment.addAction(icon_copy, loc) @@ -195,16 +181,12 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: self.ui.username.textChanged.connect(self.check_credential) self.ui.password.textChanged.connect(self.check_credential) self.ui.otp.textChanged.connect(self.check_credential) - self.ui.select_algorithm.currentIndexChanged.connect( - self.check_credential - ) + self.ui.select_algorithm.currentIndexChanged.connect(self.check_credential) self.ui.comment.textChanged.connect(self.check_credential) self.ui.btn_refresh.pressed.connect(self.refresh_credential_list) self.ui.is_protected.stateChanged.connect(self.refresh_credential_list) - self.ui.secrets_list.currentItemChanged.connect( - self.credential_changed - ) + self.ui.secrets_list.currentItemChanged.connect(self.credential_changed) self.ui.secrets_list.itemClicked.connect(self.credential_clicked) self.ui.btn_delete.pressed.connect(self.delete_credential) @@ -299,10 +281,7 @@ def credentials_listed(self, credentials: list[Credential]) -> None: active_item = None for credential in credentials: item = self.add_credential(credential) - if ( - self.active_credential - and credential.id == self.active_credential.id - ): + if self.active_credential and credential.id == self.active_credential.id: active_item = item self.ui.secrets_list.sortItems() if active_item: @@ -399,9 +378,7 @@ def show_credential(self, credential: Credential) -> None: self.action_username_copy.setEnabled(False) if credential.password: - self.ui.password.setText( - credential.password.decode(errors="replace") - ) + self.ui.password.setText(credential.password.decode(errors="replace")) self.action_password_copy.setEnabled(True) self.action_password_show.setEnabled(True) else: @@ -410,9 +387,7 @@ def show_credential(self, credential: Credential) -> None: self.action_password_show.setEnabled(False) if credential.comment: - self.ui.comment.setText( - credential.comment.decode(errors="replace") - ) + self.ui.comment.setText(credential.comment.decode(errors="replace")) self.action_comment_copy.setEnabled(True) else: self.ui.comment.clear() @@ -439,9 +414,7 @@ def show_credential(self, credential: Credential) -> None: self.ui.otp.show() self.ui.otp.setReadOnly(True) - self.ui.algorithm.setText( - str(credential.otp or credential.other) + ":" - ) + self.ui.algorithm.setText(str(credential.otp or credential.other) + ":") self.ui.otp.setPlaceholderText("") if credential.otp: @@ -493,16 +466,12 @@ def edit_credential(self, credential: Credential) -> None: self.ui.username.clear() if credential.password: - self.ui.password.setText( - credential.password.decode(errors="replace") - ) + self.ui.password.setText(credential.password.decode(errors="replace")) else: self.ui.password.clear() if credential.comment: - self.ui.comment.setText( - credential.comment.decode(errors="replace") - ) + self.ui.comment.setText(credential.comment.decode(errors="replace")) else: self.ui.comment.clear() self.ui.name.setReadOnly(False) @@ -544,9 +513,7 @@ def edit_credential(self, credential: Credential) -> None: else: self.ui.algorithm_show.show() self.ui.algorithm_edit.hide() - self.ui.algorithm.setText( - str(credential.otp or credential.other) + ":" - ) + self.ui.algorithm.setText(str(credential.otp or credential.other) + ":") self.action_otp_edit.setVisible(False) self.ui.otp.setPlaceholderText("") @@ -697,15 +664,11 @@ def check_credential(self) -> None: self.common_ui.info.info.emit( "The HMAC-Secret is not 32 chars long" ) - tool_Tip = ( - tool_Tip + "\n- The HMAC-Secret is not 32 chars long" - ) + tool_Tip = tool_Tip + "\n- The HMAC-Secret is not 32 chars long" else: self.hide_hmac_view() - if algo != "None" and len(check_secret) != len( - check_secret.encode() - ): + if algo != "None" and len(check_secret) != len(check_secret.encode()): can_save = False self.common_ui.info.info.emit("Invalid character in Secret") tool_Tip = tool_Tip + "\n- Invalid character in Secret" @@ -792,10 +755,7 @@ def show_hmac_view(self) -> None: def hide_hmac_view(self) -> None: - if ( - self.active_credential is None - and self.ui.name_label.text() == "HmacSlot2" - ): + if self.active_credential is None and self.ui.name_label.text() == "HmacSlot2": self.ui.name_label.clear() self.ui.name_label.hide() self.ui.name.clear() diff --git a/nitrokeyapp/secrets_tab/data.py b/nitrokeyapp/secrets_tab/data.py index e2ff75ee..01c591e0 100644 --- a/nitrokeyapp/secrets_tab/data.py +++ b/nitrokeyapp/secrets_tab/data.py @@ -116,9 +116,7 @@ def list(cls, secrets: SecretsApp) -> list["Credential"]: credentials.append(cls.from_list_item(item)) return credentials - def extend_with_password_safe_entry( - self, item: PasswordSafeEntry - ) -> "Credential": + def extend_with_password_safe_entry(self, item: PasswordSafeEntry) -> "Credential": if item.login: self.login = item.login if item.password: diff --git a/nitrokeyapp/secrets_tab/ui.py b/nitrokeyapp/secrets_tab/ui.py index e583c6ac..8aa67293 100644 --- a/nitrokeyapp/secrets_tab/ui.py +++ b/nitrokeyapp/secrets_tab/ui.py @@ -20,8 +20,7 @@ def query(self, attempts: int) -> None: pin, ok = QInputDialog.getText( self.parent_widget, "Enter Passwords PIN", - "Please enter the Passwords PIN (remaining retries: " - f"{attempts}):", + "Please enter the Passwords PIN (remaining retries: " f"{attempts}):", QLineEdit.EchoMode.Password, ) if ok and pin: diff --git a/nitrokeyapp/secrets_tab/worker.py b/nitrokeyapp/secrets_tab/worker.py index 2c2d8120..446500ae 100644 --- a/nitrokeyapp/secrets_tab/worker.py +++ b/nitrokeyapp/secrets_tab/worker.py @@ -251,9 +251,7 @@ def edit_credential(self, successful: bool = True) -> None: else: # new secret, new id -> create new, delete old if self.old_cred_id != self.credential.id: - self.add_credential( - self.credential, self.secret, self.old_cred_id - ) + self.add_credential(self.credential, self.secret, self.old_cred_id) # new secret, same id -> rename old, create new, delete renamed-old else: @@ -313,9 +311,7 @@ def temp_rename_credential(self, from_cred_id: bytes) -> bytes: with self.data.open() as device: secrets = SecretsApp(device) with self.touch_prompt(): - secrets.update_credential( - cred_id=from_cred_id, new_name=new_cred_id - ) + secrets.update_credential(cred_id=from_cred_id, new_name=new_cred_id) return new_cred_id @@ -546,9 +542,7 @@ def generate_otp(self) -> None: try: with self.touch_prompt(): - otp = secrets.calculate( - self.credential.id, challenge - ).decode() + otp = secrets.calculate(self.credential.id, challenge).decode() except SecretsAppException as e: self.trigger_exception(e) return @@ -582,9 +576,7 @@ def run(self) -> None: verify_pin_job = VerifyPinJob( self.common_ui, self.pin_cache, self.pin_ui, self.data ) - verify_pin_job.pin_verified.connect( - self.list_protected_credentials - ) + verify_pin_job.pin_verified.connect(self.list_protected_credentials) self.spawn(verify_pin_job) else: with self.data.open() as device: @@ -698,9 +690,7 @@ def add_credential( self.run(job) @Slot(DeviceData, Credential) - def delete_credential( - self, data: DeviceData, credential: Credential - ) -> None: + def delete_credential(self, data: DeviceData, credential: Credential) -> None: job = DeleteCredentialJob( self.common_ui, self.pin_cache, @@ -724,9 +714,7 @@ def generate_otp(self, data: DeviceData, credential: Credential) -> None: self.run(job) @Slot(DeviceData, bool) - def refresh_credentials( - self, data: DeviceData, pin_protected: bool - ) -> None: + def refresh_credentials(self, data: DeviceData, pin_protected: bool) -> None: job = ListCredentialsJob( self.common_ui, self.pin_cache,