Skip to content

Commit

Permalink
check credential, set infobox, set toolTip
Browse files Browse the repository at this point in the history
  • Loading branch information
jj-so committed Mar 18, 2024
1 parent f989ef8 commit da5fb67
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions nitrokeyapp/secrets_tab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ 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)
Expand Down Expand Up @@ -368,6 +370,7 @@ 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:
Expand Down Expand Up @@ -489,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()
Expand Down Expand Up @@ -537,7 +541,6 @@ 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("<empty>")
self.ui.otp.setText("")
Expand Down Expand Up @@ -607,33 +610,72 @@ def add_new_credential(self) -> None:

@Slot()
def check_credential(self) -> None:
can_save = True
self.info_box.hide_status()

tool_Tip = "Credeantial cannot be saved:"
can_save = True
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 == "HMAC":
self.show_hmac_view()
if len(otp_secret) != 32:
can_save = False
self.info_box.set_status("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 not is_base32(otp_secret):
can_save = False
self.info_box.set_status("Secret is not in Base32")
tool_Tip = tool_Tip + "\n- Secret is not in Base32"

if algo != "None" and len(otp_secret) < 1:
can_save = False

if len(self.ui.name.text()) < 3:
can_save = False

if len(self.ui.comment.text()) >= 96:
can_save = False
self.info_box.set_status("Comment is too long")
self.info_box.set_status("Enter a Secret")
tool_Tip = tool_Tip + "\n- Enter a Secret"

self.ui.btn_save.setEnabled(can_save)
if can_save:
tool_Tip = "Credential Save"

self.ui.btn_save.setToolTip(tool_Tip)

def act_copy_line_edit(self, obj: QLineEdit) -> None:
self.clipboard.setText(obj.text())
Expand Down

0 comments on commit da5fb67

Please sign in to comment.