From cc5f0d715ce2c742005b2e0c3488cfa87a076202 Mon Sep 17 00:00:00 2001 From: gagam3hl Date: Fri, 27 Nov 2026 23:27:57 +0000 Subject: [PATCH] Don't stay at one-step TAN '999' when bank allows a two-step method too (#212) fetch_tan_mechanisms() sets '999' as a placeholder before the sync dialog. When the bank answers 3920 with both a two-step function and '999' (Commerzbank photoTAN: ['900', '999']), _process_response() saw the placeholder as a valid choice and never selected the two-step method. minimal_interactive_cli_bootstrap() then treated '999' as already chosen, and is_challenge_structured() failed with KeyError: '999'. Treat '999' like None in both places. Banks that only allow '999' still fall back to it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013em56AbgS1EJjr7jA7XaCe --- fints/client.py | 4 +++- fints/utils.py | 2 +- tests/test_client.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/fints/client.py b/fints/client.py index 213972a..1b14ff1 100644 --- a/fints/client.py +++ b/fints/client.py @@ -1579,7 +1579,9 @@ def send_tan(self, challenge: NeedTANResponse, tan: str): def _process_response(self, dialog, segment, response): if response.code == '3920' and not self.bank_identifier == ING_BANK_IDENTIFIER: self.allowed_security_functions = list(response.parameters) - if self.selected_security_function is None or not self.selected_security_function in self.allowed_security_functions: + # '999' counts as "not chosen yet": fetch_tan_mechanisms() sets it as a placeholder, and some banks + # (e.g. Commerzbank photoTAN) list '999' among the allowed functions next to a two-step one (#212). + if self.selected_security_function in (None, '999') or self.selected_security_function not in self.allowed_security_functions: # Select the first available twostep security_function that we support for security_function, parameter in self.get_tan_mechanisms().items(): if security_function == '999': diff --git a/fints/utils.py b/fints/utils.py index f02edb6..8caf7c7 100644 --- a/fints/utils.py +++ b/fints/utils.py @@ -291,7 +291,7 @@ def minimal_interactive_cli_bootstrap(client): """ # Fetch available TAN mechanisms by the bank, if we don't know it already. If the client was created with cached data, # the function is already set. - if not client.get_current_tan_mechanism(): + if client.get_current_tan_mechanism() in (None, '999'): client.fetch_tan_mechanisms() mechanisms = list(client.get_tan_mechanisms().items()) if len(mechanisms) > 1: diff --git a/tests/test_client.py b/tests/test_client.py index f602e4f..080b24a 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -227,3 +227,44 @@ def test_get_transactions(fints_client): assert len(transactions) == 3 assert transactions[0].data['amount'].amount == Decimal('182.34') + + +class _ClosedDialog: + open = False + + +def _response_3920(*security_functions): + from fints.formals import Response + return Response(code='3920', reference_element=None, + text='Zugelassene Ein- und Zwei-Schritt-Verfahren für den Benutzer.', + parameters=list(security_functions)) + + +def test_3920_replaces_onestep_placeholder(fints_client): + # Commerzbank photoTAN (#212): fetch_tan_mechanisms() sets '999' as a placeholder, + # then the bank allows a two-step method *and* '999'. The client must not stay at '999'. + fints_client.fetch_tan_mechanisms() + fints_client.set_tan_mechanism('999') + + fints_client._process_response(_ClosedDialog(), None, _response_3920('942', '999')) + + assert fints_client.get_current_tan_mechanism() == '942' + assert fints_client.is_challenge_structured() in (True, False) + + +def test_3920_keeps_onestep_if_only_onestep_allowed(fints_client): + fints_client.fetch_tan_mechanisms() + fints_client.set_tan_mechanism('999') + + fints_client._process_response(_ClosedDialog(), None, _response_3920('999')) + + assert fints_client.get_current_tan_mechanism() == '999' + + +def test_3920_keeps_explicitly_chosen_twostep(fints_client): + fints_client.fetch_tan_mechanisms() + fints_client.set_tan_mechanism('962') + + fints_client._process_response(_ClosedDialog(), None, _response_3920('942', '962', '999')) + + assert fints_client.get_current_tan_mechanism() == '962'