diff --git a/cryptnox_cli/command/change_pin.py b/cryptnox_cli/command/change_pin.py index 0cd2a98..d48c8dd 100644 --- a/cryptnox_cli/command/change_pin.py +++ b/cryptnox_cli/command/change_pin.py @@ -36,7 +36,7 @@ def _execute(self, card: cryptnox_sdk_py.Card) -> int: print("Card is in easy mode. Setting same PIN code") pin_code = security.EASY_MODE_PIN else: - pin_code = security.get_pin_code(card, f"Set new PIN code ({card.pin_rule}): ") + pin_code = security.confirm_pin_code(card, f"Set new PIN code ({card.pin_rule}): ") card.change_pin(pin_code) diff --git a/cryptnox_cli/command/change_puk.py b/cryptnox_cli/command/change_puk.py index 13e464c..e3f736b 100644 --- a/cryptnox_cli/command/change_puk.py +++ b/cryptnox_cli/command/change_puk.py @@ -37,7 +37,7 @@ def _execute(self, card: cryptnox_sdk_py.Card) -> int: break else: puk_code = security.get_puk_code(card) - new_puk_code = security.get_puk_code(card, f"Set new PUK code ({card.puk_rule}): ") + new_puk_code = security.confirm_puk_code(card, f"Set new PUK code ({card.puk_rule}): ") try: card.change_puk(puk_code, new_puk_code) diff --git a/cryptnox_cli/command/helper/security.py b/cryptnox_cli/command/helper/security.py index 36072d6..f8ec539 100644 --- a/cryptnox_cli/command/helper/security.py +++ b/cryptnox_cli/command/helper/security.py @@ -283,6 +283,34 @@ def get_puk_code(card: cryptnox_sdk_py.Card, text: str = "", allowed_values: Lis return _get_code(card.valid_puk, text, allowed_values) +def confirm_pin_code(card: cryptnox_sdk_py.Card, text: str, confirm_text: str = "") -> str: + """Prompt for new PIN twice and return it only if both entries match.""" + confirm_text = confirm_text or f"Confirm new PIN code ({card.pin_rule}): " + while True: + try: + value = get_pin_code(card, text) + confirm_value = get_pin_code(card, confirm_text) + except KeyboardInterrupt: + raise ExitException("Aborted.") + if value == confirm_value: + return value + print("The values do not match. Please try again.") + + +def confirm_puk_code(card: cryptnox_sdk_py.Card, text: str, confirm_text: str = "") -> str: + """Prompt for new PUK twice and return it only if both entries match.""" + confirm_text = confirm_text or f"Confirm new PUK code ({card.puk_rule}): " + while True: + try: + value = get_puk_code(card, text) + confirm_value = get_puk_code(card, confirm_text) + except KeyboardInterrupt: + raise ExitException("Aborted.") + if value == confirm_value: + return value + print("The values do not match. Please try again.") + + def _get_code( validation_method: typing.Callable, text: str = "", diff --git a/cryptnox_cli/command/unlock_pin.py b/cryptnox_cli/command/unlock_pin.py index b33986e..326cc85 100644 --- a/cryptnox_cli/command/unlock_pin.py +++ b/cryptnox_cli/command/unlock_pin.py @@ -33,7 +33,7 @@ def unblock_pin(card, puk): if is_easy_mode: pin_code = security.EASY_MODE_PIN else: - pin_code = security.get_pin_code(card, f"Set card PIN code ({card.pin_rule}): ") + pin_code = security.confirm_pin_code(card, f"Set card PIN code ({card.pin_rule}): ") card.unblock_pin(puk, pin_code)