Skip to content

Commit

Permalink
Add --refresh-session CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Dec 21, 2024
1 parent 7b86652 commit 627a56c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
22 changes: 13 additions & 9 deletions dkb_robo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions dkb_robo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,6 +38,7 @@
help='use ChipTAN for login (either "qr" or "manual")',
type=str,
envvar="DKB_CHIP_TAN",
hidden=True,
)
@click.option(
"--username",
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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"]
)
5 changes: 3 additions & 2 deletions dkb_robo/dkb_robo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ 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
self.tan_insert = tan_insert
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 """
Expand All @@ -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()
Expand Down

0 comments on commit 627a56c

Please sign in to comment.