Skip to content

Commit 274cec5

Browse files
Remove local_print, local_critical
User interactions should be handled by the application, not by the SDK. local_print is just removed. Instead of calling local_critical, we return a bool so that the application can trigger the appropriate calls.
1 parent 2e8df19 commit 274cec5

File tree

2 files changed

+11
-39
lines changed

2 files changed

+11
-39
lines changed

src/nitrokey/helpers.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
1-
import sys
21
import time
3-
from typing import Any
4-
5-
6-
def local_print(*messages: Any, **kwargs: Any) -> None:
7-
for item in messages:
8-
print(item, **kwargs)
9-
10-
11-
def local_critical(
12-
*messages: Any, support_hint: bool = True, ret_code: int = 1, **kwargs: Any
13-
) -> None:
14-
messages = ("Critical error:",) + tuple(messages)
15-
local_print(*messages, **kwargs)
16-
sys.exit(ret_code)
172

183

194
class Try:

src/nitrokey/trussed/admin_app.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import enum
2-
import sys
32
from dataclasses import dataclass
43
from enum import Enum, IntFlag
54
from typing import Optional
65

76
from fido2 import cbor
87
from fido2.ctap import CtapError
98

10-
from nitrokey.helpers import local_critical, local_print
11-
129
from .device import App, NitrokeyTrussedDevice
1310
from .exceptions import TimeoutException
1411
from .utils import Uuid, Version
@@ -121,7 +118,7 @@ def check(cls, i: int, msg: str) -> None:
121118
error = "The application does not support factory reset through nitropy"
122119
elif status == FactoryResetStatus.APP_FAILED_PARSE:
123120
error = "The application name must be utf-8"
124-
local_critical(f"{msg}: {error}", support_hint=False)
121+
raise Exception(f"{msg}: {error}")
125122

126123

127124
@enum.unique
@@ -269,37 +266,27 @@ def set_config(self, key: str, value: str) -> None:
269266
assert reply
270267
ConfigStatus.check(reply[0], "Failed to set config value")
271268

272-
def factory_reset(self) -> None:
269+
def factory_reset(self) -> bool:
273270
try:
274-
local_print(
275-
"Please touch the device to confirm the operation", file=sys.stderr
276-
)
277271
reply = self._call(AdminCommand.FACTORY_RESET, response_len=1)
278-
if reply is None:
279-
local_critical(
280-
"Factory reset is not supported by the firmware version on the device",
281-
support_hint=False,
282-
)
283-
return
284272
except OSError as e:
285273
if e.errno == 5:
286274
self.device.logger.debug("ignoring OSError after reboot", exc_info=e)
287-
return
275+
return True
288276
else:
289277
raise e
290-
FactoryResetStatus.check(reply[0], "Failed to factory reset the device")
278+
if reply:
279+
FactoryResetStatus.check(reply[0], "Failed to factory reset the device")
280+
return reply is not None
291281

292-
def factory_reset_app(self, application: str) -> None:
293-
local_print("Please touch the device to confirm the operation", file=sys.stderr)
282+
def factory_reset_app(self, application: str) -> bool:
294283
reply = self._call(
295284
AdminCommand.FACTORY_RESET_APP,
296285
data=application.encode("ascii"),
297286
response_len=1,
298287
)
299-
if reply is None:
300-
local_critical(
301-
"Application Factory reset is not supported by the firmware version on the device",
302-
support_hint=False,
288+
if reply:
289+
FactoryResetStatus.check(
290+
reply[0], "Failed to factory reset the application"
303291
)
304-
return
305-
FactoryResetStatus.check(reply[0], "Failed to factory reset the device")
292+
return reply is not None

0 commit comments

Comments
 (0)