Skip to content

Commit 6a181be

Browse files
Fix typing, format issues
1 parent 76dd671 commit 6a181be

3 files changed

Lines changed: 50 additions & 48 deletions

File tree

nitrokeyapp/secrets_tab/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,9 @@ def backup_credentials(self) -> None:
319319
@Slot(str)
320320
def save_credential_backup(self, credential_list_formatted: str) -> None:
321321
from PySide6.QtWidgets import QFileDialog
322+
322323
path, _ = QFileDialog.getSaveFileName(
323-
self,
324-
"Save Credential Backup",
325-
"credential_backup.json",
326-
"JSON Files (*.json)",
324+
self, "Save Credential Backup", "credential_backup.json", "JSON Files (*.json)"
327325
)
328326
if path:
329327
with open(path, "w") as f:
@@ -334,11 +332,9 @@ def restore_credentials(self) -> None:
334332
if not self.data:
335333
return
336334
from PySide6.QtWidgets import QFileDialog
335+
337336
path, _ = QFileDialog.getOpenFileName(
338-
self,
339-
"Open Credential Backup",
340-
"",
341-
"JSON Files (*.json)",
337+
self, "Open Credential Backup", "", "JSON Files (*.json)"
342338
)
343339
if not path:
344340
return

nitrokeyapp/secrets_tab/data.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from base64 import b64encode, b64decode
1+
from base64 import b64decode, b64encode
22
from dataclasses import dataclass
33
from datetime import datetime
44
from enum import Enum, auto, unique
@@ -75,6 +75,7 @@ def _kind_from_raw(kind: RawKind) -> Optional[Kind]:
7575
else:
7676
raise RuntimeError(f"Unexpected credential kind: {kind}")
7777

78+
7879
@dataclass
7980
class Credential:
8081
id: bytes
@@ -129,39 +130,45 @@ def extend_with_password_safe_entry(self, item: PasswordSafeEntry) -> "Credentia
129130
def serialize_credential(self) -> dict:
130131
if not self.loaded:
131132
raise RuntimeError("Cannot serialize credential which is not loaded yet")
132-
credential_dict={
133-
"id" : b64encode(self.id).decode(),
134-
"kind" : self.otp.raw_kind().value if self.otp else self.other.raw_kind().value if self.other else RawKind.NotSet.value,
135-
"login" : b64encode(self.login).decode() if self.login else "",
136-
"password" : b64encode(self.password).decode() if self.password else "",
137-
"comment" : b64encode(self.comment).decode() if self.comment else "",
138-
"protected" : self.protected,
139-
"touch_required" : self.touch_required
133+
credential_dict = {
134+
"id": b64encode(self.id).decode(),
135+
"kind": self.otp.raw_kind().value
136+
if self.otp
137+
else self.other.raw_kind().value
138+
if self.other
139+
else RawKind.NotSet.value,
140+
"login": b64encode(self.login).decode() if self.login else "",
141+
"password": b64encode(self.password).decode() if self.password else "",
142+
"comment": b64encode(self.comment).decode() if self.comment else "",
143+
"protected": self.protected,
144+
"touch_required": self.touch_required,
140145
}
141146
return credential_dict
142147

143148
@classmethod
144149
def deserialize_credential(cls, credential_dict: dict) -> "Credential":
145-
login=credential_dict.get("login", "")
146-
password=credential_dict.get("password", "")
147-
comment=credential_dict.get("comment", "")
150+
id = credential_dict.get("id", "")
151+
login = credential_dict.get("login", "")
152+
password = credential_dict.get("password", "")
153+
comment = credential_dict.get("comment", "")
148154
credential = cls(
149-
id=b64decode(credential_dict.get("id")),
150-
login=b64decode(login) if login else None
151-
password=b64decode(password) if password else None
152-
comment=b64decode(comment) if comment else None
155+
id=id,
156+
login=b64decode(login) if login else None,
157+
password=b64decode(password) if password else None,
158+
comment=b64decode(comment) if comment else None,
153159
protected=credential_dict.get("protected", False),
154-
touch_required=credential_dict.get("touch_required", False)
160+
touch_required=credential_dict.get("touch_required", False),
155161
)
156162

157-
kind= _kind_from_raw(RawKind(credential_dict.get("kind")))
163+
kind = _kind_from_raw(RawKind(credential_dict.get("kind")))
158164
if isinstance(kind, OtpKind):
159165
credential.otp = kind
160166
elif isinstance(kind, OtherKind):
161167
credential.other = kind
162168

163169
return credential
164170

171+
165172
@dataclass
166173
class OtpData:
167174
otp: str

nitrokeyapp/secrets_tab/worker.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import json
12
import logging
23
from dataclasses import dataclass
34
from datetime import datetime
45
from typing import Dict, Optional
5-
import json
66

77
from nitrokey.nk3 import NK3
88
from nitrokey.nk3.secrets_app import SecretsApp, SecretsAppException
@@ -620,6 +620,7 @@ def get_credential(self) -> None:
620620
cred = self.credential.extend_with_password_safe_entry(pse)
621621
self.received_credential.emit(cred)
622622

623+
623624
class BackupCredentialJob(Job):
624625
credential_bkp = Signal(str)
625626

@@ -629,14 +630,14 @@ def __init__(
629630
pin_cache: PinCache,
630631
pin_ui: PinUi,
631632
data: DeviceData,
632-
pin_protected: bool
633+
pin_protected: bool,
633634
) -> None:
634635
super().__init__(common_ui)
635636

636637
self.pin_cache = pin_cache
637638
self.pin_ui = pin_ui
638639
self.data = data
639-
self.pin_protected=pin_protected
640+
self.pin_protected = pin_protected
640641
self.credential_bkp.connect(lambda _: self.finished.emit())
641642

642643
def run(self) -> None:
@@ -657,12 +658,12 @@ def get_credential_backup(self) -> None:
657658
return
658659
secrets = SecretsApp(device)
659660

660-
credential_list=Credential.list(secrets=secrets)
661-
credential_list_serialized=[]
661+
credential_list = Credential.list(secrets=secrets)
662+
credential_list_serialized = []
662663
for credential in credential_list:
663664
try:
664665
if credential.protected:
665-
pin= self.pin_cache.get(self.data)
666+
pin = self.pin_cache.get(self.data)
666667
if pin:
667668
secrets.verify_pin_raw(pin)
668669
pse = secrets.get_credential(credential.id)
@@ -676,9 +677,10 @@ def get_credential_backup(self) -> None:
676677
except SecretsAppException as e:
677678
self.trigger_exception(e)
678679

679-
credential_list_formatted=json.dumps(credential_list_serialized, indent=4)
680+
credential_list_formatted = json.dumps(credential_list_serialized, indent=4)
680681
self.credential_bkp.emit(credential_list_formatted)
681682

683+
682684
class RestoreCredentialJob(Job):
683685
credential_restore = Signal()
684686

@@ -688,14 +690,14 @@ def __init__(
688690
pin_cache: PinCache,
689691
pin_ui: PinUi,
690692
data: DeviceData,
691-
credential_backup: str
693+
credential_backup: str,
692694
) -> None:
693695
super().__init__(common_ui)
694696

695697
self.pin_cache = pin_cache
696698
self.pin_ui = pin_ui
697699
self.data = data
698-
self.credential_backup=credential_backup
700+
self.credential_backup = credential_backup
699701
self.credential_restore.connect(lambda: self.finished.emit())
700702

701703
def run(self) -> None:
@@ -708,37 +710,33 @@ def run(self) -> None:
708710
@Slot(list)
709711
def check_credential_backup(self, existing_credentials: list[Credential]) -> None:
710712
try:
711-
serialized_credential_list=json.loads(self.credential_backup)
713+
serialized_credential_list = json.loads(self.credential_backup)
712714
except json.JSONDecodeError as e:
713-
self.trigger_error('Error: Invalid backup file')
714-
credential_list=[]
715+
self.trigger_exception(e)
716+
credential_list = []
715717
for serialized_credential in serialized_credential_list:
716718
try:
717719
credential = Credential.deserialize_credential(serialized_credential)
718720
credential_list.append(credential)
719721
except SecretsAppException as e:
720722
self.trigger_exception(e)
721723

722-
pin_required=False
724+
pin_required = False
723725
ids = {credential.id for credential in existing_credentials}
724-
temp_cred_list=[]
726+
temp_cred_list = []
725727
for credential in credential_list:
726728
if credential.id in ids:
727729
logger.warning("Warn: Credential with same ID already exists")
728730
else:
729731
temp_cred_list.append(credential)
730732

731733
if credential.protected:
732-
pin_required=True
734+
pin_required = True
733735

734-
self.credential_list=temp_cred_list
736+
self.credential_list = temp_cred_list
735737
if pin_required:
736738
verify_pin_job = VerifyPinJob(
737-
self.common_ui,
738-
self.pin_cache,
739-
self.pin_ui,
740-
self.data,
741-
set_pin=pin_required,
739+
self.common_ui, self.pin_cache, self.pin_ui, self.data, set_pin=pin_required
742740
)
743741
verify_pin_job.pin_verified.connect(self.add_credential)
744742
self.spawn(verify_pin_job)
@@ -776,6 +774,7 @@ def add_credential(self, successful: bool = True) -> None:
776774

777775
self.credential_restore.emit()
778776

777+
779778
class SecretsWorker(Worker):
780779
# TODO: remove DeviceData from signatures
781780

@@ -852,7 +851,7 @@ def backup_credential(self, data: DeviceData, pin_protected: bool) -> None:
852851
self.run(job)
853852

854853
@Slot(DeviceData, str)
855-
def restore_credential(self, data:DeviceData, backup_conent: str) -> None:
854+
def restore_credential(self, data: DeviceData, backup_conent: str) -> None:
856855
job = RestoreCredentialJob(self.common_ui, self.pin_cache, self.pin_ui, data, backup_conent)
857856
job.credential_restore.connect(self.credential_restore)
858857
self.run(job)

0 commit comments

Comments
 (0)