From 627a56cc1aae0892240760bda5ff48c6c90a7f6c Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sat, 21 Dec 2024 22:00:25 +0100 Subject: [PATCH] Add --refresh-session CLI option --- dkb_robo/api.py | 22 +++++++++++++--------- dkb_robo/cli.py | 14 ++++++++++++-- dkb_robo/dkb_robo.py | 5 +++-- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/dkb_robo/api.py b/dkb_robo/api.py index 57c4636..63394c3 100644 --- a/dkb_robo/api.py +++ b/dkb_robo/api.py @@ -38,14 +38,16 @@ class Wrapper(object): client = None token_dic = None account_dic = {} + refresh_session: bool = False session_refresher: BankingSessionRefresher old_session_refresher: OldBankingSessionRefresher - def __init__(self, dkb_user: str = None, dkb_password: str = None, chip_tan: bool = False, proxies: Dict[str, str] = None, logger: logging.Logger = False, mfa_device: int = None): + def __init__(self, dkb_user: str = None, dkb_password: str = None, chip_tan: bool = False, proxies: Dict[str, str] = None, logger: logging.Logger = False, mfa_device: int = None, refresh_session: bool = False): self.dkb_user = dkb_user self.dkb_password = dkb_password self.proxies = proxies self.logger = logger + self.refresh_session = refresh_session if chip_tan: self.logger.info('Using to chip_tan to login') if chip_tan in ('qr', 'chip_tan_qr'): @@ -1456,10 +1458,11 @@ def login(self) -> Tuple[Dict, None]: raise DKBRoboError('Login failed: 2nd factor authentication did not complete') # start session refresher - self.session_refresher = BankingSessionRefresher( - client=self.client, logger=self.logger - ) - self.session_refresher.start() + if self.refresh_session: + self.session_refresher = BankingSessionRefresher( + client=self.client, logger=self.logger + ) + self.session_refresher.start() # get account overview self.account_dic = self._get_overview() @@ -1468,10 +1471,11 @@ def login(self) -> Tuple[Dict, None]: self._do_sso_redirect() # start a session refresher for the legacy login - self.old_session_refresher = OldBankingSessionRefresher( - client=self.client, logger=self.logger - ) - self.old_session_refresher.start() + if self.refresh_session: + self.old_session_refresher = OldBankingSessionRefresher( + client=self.client, logger=self.logger + ) + self.old_session_refresher.start() self.logger.debug('api.Wrapper.login() ended\n') return self.account_dic, None diff --git a/dkb_robo/cli.py b/dkb_robo/cli.py index 545bd67..815df54 100644 --- a/dkb_robo/cli.py +++ b/dkb_robo/cli.py @@ -2,6 +2,7 @@ """ dkb_robo cli """ from datetime import date from pprint import pprint +from typing import Literal, Optional import sys import csv import json @@ -37,6 +38,7 @@ help='use ChipTAN for login (either "qr" or "manual")', type=str, envvar="DKB_CHIP_TAN", + hidden=True, ) @click.option( "--username", @@ -62,8 +64,15 @@ help="output format to use", envvar="DKB_FORMAT", ) +@click.option( + "--refresh-session", + is_flag=True, + default=False, + help="Refresh the session to prevent timeouts", + envvar="DKB_REFRESH_SESSION", +) @click.pass_context -def main(ctx, debug, use_tan, chip_tan, username, password, format): # pragma: no cover +def main(ctx: click.Context, debug: bool, use_tan: bool, chip_tan, username: str, password: Optional[str], format: Literal["pprint", "table", "csv", "json"], refresh_session: bool): # pragma: no cover """ main fuunction """ if use_tan: @@ -76,6 +85,7 @@ def main(ctx, debug, use_tan, chip_tan, username, password, format): # pragma: ctx.obj["USERNAME"] = username ctx.obj["PASSWORD"] = password ctx.obj["FORMAT"] = _load_format(format) + ctx.obj["REFRESH_SESSION"] = refresh_session @main.command() @@ -250,5 +260,5 @@ def formatter(data): # pragma: no cover def _login(ctx): return dkb_robo.DKBRobo( - dkb_user=ctx.obj["USERNAME"], dkb_password=ctx.obj["PASSWORD"], chip_tan=ctx.obj["CHIP_TAN"], debug=ctx.obj["DEBUG"] + dkb_user=ctx.obj["USERNAME"], dkb_password=ctx.obj["PASSWORD"], chip_tan=ctx.obj["CHIP_TAN"], debug=ctx.obj["DEBUG"], refresh_session=ctx.obj["REFRESH_SESSION"] ) diff --git a/dkb_robo/dkb_robo.py b/dkb_robo/dkb_robo.py index e41842c..5d57103 100644 --- a/dkb_robo/dkb_robo.py +++ b/dkb_robo/dkb_robo.py @@ -26,7 +26,7 @@ class DKBRobo(object): logger = None wrapper = None - def __init__(self, dkb_user=None, dkb_password=None, tan_insert=False, legacy_login=False, debug=False, mfa_device=None, chip_tan=False): + def __init__(self, dkb_user=None, dkb_password=None, tan_insert=False, legacy_login=False, debug=False, mfa_device=None, chip_tan=False, refresh_session=False): self.dkb_user = dkb_user self.dkb_password = dkb_password self.chip_tan = chip_tan @@ -34,6 +34,7 @@ def __init__(self, dkb_user=None, dkb_password=None, tan_insert=False, legacy_lo self.legacy_login = legacy_login self.logger = logger_setup(debug) self.mfa_device = mfa_device + self.refresh_session = refresh_session def __enter__(self): """ Makes DKBRobo a Context Manager """ @@ -48,7 +49,7 @@ def __enter__(self): if self.mfa_device == 'm': self.mfa_device = 1 - self.wrapper = Wrapper(dkb_user=self.dkb_user, dkb_password=self.dkb_password, proxies=self.proxies, chip_tan=self.chip_tan, mfa_device=self.mfa_device, logger=self.logger) + self.wrapper = Wrapper(dkb_user=self.dkb_user, dkb_password=self.dkb_password, proxies=self.proxies, chip_tan=self.chip_tan, mfa_device=self.mfa_device, logger=self.logger, refresh_session=self.refresh_session) # login and get the account overview (self.account_dic, self.last_login) = self.wrapper.login()