From f67728ad4e88ed718a2e610a57b51dbf0d14aa2a Mon Sep 17 00:00:00 2001 From: jj-so Date: Tue, 12 Mar 2024 08:31:45 +0100 Subject: [PATCH] check imput in secrets_tab --- nitrokeyapp/secrets_tab/__init__.py | 107 ++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 21 deletions(-) diff --git a/nitrokeyapp/secrets_tab/__init__.py b/nitrokeyapp/secrets_tab/__init__.py index fa0a75c6..cb215b96 100644 --- a/nitrokeyapp/secrets_tab/__init__.py +++ b/nitrokeyapp/secrets_tab/__init__.py @@ -183,8 +183,11 @@ def __init__(self, info_box: InfoBox, parent: Optional[QWidget] = None) -> None: self.ui.btn_edit.pressed.connect(self.prepare_edit_credential) self.ui.name.textChanged.connect(self.check_credential) + 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.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) @@ -367,13 +370,14 @@ def show_credential(self, credential: Credential) -> None: self.ui.name.hide() self.ui.name_label.show() + self.ui.name.setText(credential.name) self.ui.name_label.setText(credential.name) if credential.login: self.ui.username.setText(credential.login.decode(errors="replace")) self.action_username_copy.setEnabled(True) else: - self.ui.username.setText("") + self.ui.username.clear() self.action_username_copy.setEnabled(False) if credential.password: @@ -381,7 +385,7 @@ def show_credential(self, credential: Credential) -> None: self.action_password_copy.setEnabled(True) self.action_password_show.setEnabled(True) else: - self.ui.password.setText("") + self.ui.password.clear() self.action_password_copy.setEnabled(False) self.action_password_show.setEnabled(False) @@ -389,7 +393,7 @@ def show_credential(self, credential: Credential) -> None: self.ui.comment.setText(credential.comment.decode(errors="replace")) self.action_comment_copy.setEnabled(True) else: - self.ui.comment.setText("") + self.ui.comment.clear() self.action_comment_copy.setEnabled(False) self.ui.name.setReadOnly(True) @@ -462,17 +466,17 @@ def edit_credential(self, credential: Credential) -> None: if credential.login: self.ui.username.setText(credential.login.decode(errors="replace")) else: - self.ui.username.setText("") + self.ui.username.clear() if credential.password: self.ui.password.setText(credential.password.decode(errors="replace")) else: - self.ui.password.setText("") + self.ui.password.clear() if credential.comment: self.ui.comment.setText(credential.comment.decode(errors="replace")) else: - self.ui.comment.setText("") + self.ui.comment.clear() self.ui.name.setReadOnly(False) self.ui.username.setReadOnly(False) self.ui.password.setReadOnly(False) @@ -488,6 +492,7 @@ def edit_credential(self, credential: Credential) -> None: self.ui.algorithm_tab.show() self.ui.algorithm_tab.setCurrentIndex(0) + self.ui.select_algorithm.setMaxCount(3) self.ui.algorithm_show.hide() self.ui.algorithm_edit.show() self.ui.select_algorithm.show() @@ -517,7 +522,7 @@ def edit_credential(self, credential: Credential) -> None: # no otp there, just offer it as in add else: - self.ui.otp.setText("") + self.ui.otp.clear() self.ui.otp.setReadOnly(False) self.ui.otp.setPlaceholderText("") self.ui.select_algorithm.setCurrentText(str(credential.otp)) @@ -536,10 +541,9 @@ def act_enable_otp_edit(self) -> None: self.active_credential.new_secret = True self.ui.otp.setReadOnly(False) - self.ui.select_algorithm.setMaxCount(3) self.ui.select_algorithm.setEnabled(True) self.ui.otp.setPlaceholderText("") - self.ui.otp.setText("") + self.ui.otp.clear() self.check_credential() @@ -562,12 +566,13 @@ def add_new_credential(self) -> None: self.ui.name.show() self.ui.name_label.hide() - self.ui.name.setText("") + self.ui.name.clear() - self.ui.otp.setText("") - self.ui.username.setText("") - self.ui.password.setText("") - self.ui.comment.setText("") + self.ui.otp.clear() + self.ui.otp.setPlaceholderText("") + self.ui.username.clear() + self.ui.password.clear() + self.ui.comment.clear() self.ui.name.setReadOnly(False) self.ui.otp.setReadOnly(False) @@ -606,29 +611,83 @@ def add_new_credential(self) -> None: @Slot() def check_credential(self) -> None: + self.info_box.hide_status() + + tool_Tip = "Credeantial cannot be saved:" can_save = True + check_secret = self.ui.otp.text() - otp_secret = self.ui.otp.text() + name_len = len(str.encode(self.ui.name.text())) + username_len = len(str.encode(self.ui.username.text())) + password_len = len(str.encode(self.ui.password.text())) + comment_len = len(str.encode(self.ui.comment.text())) algo = self.ui.select_algorithm.currentText() + + if len(self.ui.name.text()) < 3: + can_save = False + if len(self.ui.name.text()) == 0: + self.info_box.set_status("Enter a Credential Name") + tool_Tip = tool_Tip + "\n- Enter a Credential Name" + if len(self.ui.name.text()) >= 1 and len(self.ui.name.text()) < 3: + self.info_box.set_status("Credential Name is too short") + tool_Tip = tool_Tip + "\n- Credential Name is too short" + if name_len >= 128: + can_save = False + self.info_box.set_status("Credential Name is too long") + tool_Tip = tool_Tip + "\n- Credential Name is too long" + + if username_len >= 128: + can_save = False + self.info_box.set_status("Username is too long") + tool_Tip = tool_Tip + "\n- Username is too long" + + if password_len >= 128: + can_save = False + self.info_box.set_status("Password is too long") + tool_Tip = tool_Tip + "\n- Password is too long" + + if comment_len >= 128: + can_save = False + self.info_box.set_status("Comment is too long") + tool_Tip = tool_Tip + "\n- Comment is too long" + if self.ui.select_algorithm.isEnabled(): + if algo == "None": + self.ui.otp.setReadOnly(True) + self.ui.otp.setPlaceholderText("