From 1b2b960c1b0c1819ee63afa3ce0a80cc132c2838 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 1 Nov 2021 19:53:30 +0100 Subject: [PATCH] Extracted TanMethodSelector --- .../banking/fints/FinTsClientForCustomer.kt | 4 +- .../dankito/banking/fints/FinTsJobExecutor.kt | 24 +++--------- .../banking/fints/util/TanMethodSelector.kt | 37 +++++++++++++++++++ 3 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 fints4k/src/commonMain/kotlin/net/dankito/banking/fints/util/TanMethodSelector.kt diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClientForCustomer.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClientForCustomer.kt index d235cc23..238630c9 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClientForCustomer.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClientForCustomer.kt @@ -11,6 +11,7 @@ import net.dankito.banking.fints.transactions.IAccountTransactionsParser import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser import net.dankito.banking.fints.util.IBase64Service import net.dankito.banking.fints.util.PureKotlinBase64Service +import net.dankito.banking.fints.util.TanMethodSelector import net.dankito.banking.fints.webclient.IWebClient import net.dankito.banking.fints.webclient.KtorWebClient @@ -22,6 +23,7 @@ open class FinTsClientForCustomer( messageBuilder: MessageBuilder = MessageBuilder(), mt940Parser: IAccountTransactionsParser = Mt940AccountTransactionsParser(), modelMapper: ModelMapper = ModelMapper(messageBuilder), + protected open val tanMethodSelector: TanMethodSelector = TanMethodSelector(), product: ProductData = ProductData("15E53C26816138699C7B6A3E8", "1.0.0") // TODO: get version dynamically) ) { @@ -30,7 +32,7 @@ open class FinTsClientForCustomer( : this(bank, callback, RequestExecutor(MessageBuilder(), webClient, base64Service)) - protected val client = FinTsClient(FinTsJobExecutor(callback, requestExecutor, messageBuilder, mt940Parser, modelMapper, product)) + protected val client = FinTsClient(FinTsJobExecutor(callback, requestExecutor, messageBuilder, mt940Parser, modelMapper, tanMethodSelector, product)) open val messageLogWithoutSensitiveData: List diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsJobExecutor.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsJobExecutor.kt index 22d3cb2d..589fadb9 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsJobExecutor.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsJobExecutor.kt @@ -20,6 +20,7 @@ import net.dankito.banking.fints.tan.FlickerCodeDecoder import net.dankito.banking.fints.tan.TanImageDecoder import net.dankito.banking.fints.transactions.IAccountTransactionsParser import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser +import net.dankito.banking.fints.util.TanMethodSelector import net.dankito.utils.multiplatform.log.LoggerFactory import net.dankito.utils.multiplatform.Date import net.dankito.utils.multiplatform.ObjectReference @@ -36,6 +37,7 @@ open class FinTsJobExecutor( protected open val messageBuilder: MessageBuilder = MessageBuilder(), protected open val mt940Parser: IAccountTransactionsParser = Mt940AccountTransactionsParser(), protected open val modelMapper: ModelMapper = ModelMapper(messageBuilder), + protected open val tanMethodSelector: TanMethodSelector = TanMethodSelector(), protected open val product: ProductData = ProductData("15E53C26816138699C7B6A3E8", "1.0.0") // TODO: get version dynamically ) { @@ -680,14 +682,15 @@ open class FinTsJobExecutor( done(true) } else { - findPreferredTanMethod(bank, preferredTanMethods)?.let { + tanMethodSelector.findPreferredTanMethod(bank.tanMethodsAvailableForUser, preferredTanMethods)?.let { bank.selectedTanMethod = it done(true) return } // we know user's supported tan methods, now ask user which one to select - callback.askUserForTanMethod(bank.tanMethodsAvailableForUser, selectSuggestedTanMethod(bank)) { selectedTanMethod -> + val suggestedTanMethod = tanMethodSelector.getSuggestedTanMethod(bank.tanMethodsAvailableForUser) + callback.askUserForTanMethod(bank.tanMethodsAvailableForUser, suggestedTanMethod) { selectedTanMethod -> if (selectedTanMethod != null) { bank.selectedTanMethod = selectedTanMethod done(true) @@ -699,23 +702,6 @@ open class FinTsJobExecutor( } } - private fun findPreferredTanMethod(bank: BankData, preferredTanMethods: List?): TanMethod? { - preferredTanMethods?.forEach { preferredTanMethodType -> - bank.tanMethodsAvailableForUser.firstOrNull { it.type == preferredTanMethodType }?.let { - return it - } - } - - return null - } - - protected open fun selectSuggestedTanMethod(bank: BankData): TanMethod? { - return bank.tanMethodsAvailableForUser.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan && it.type != TanMethodType.ChipTanManuell } - ?: bank.tanMethodsAvailableForUser.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan } - ?: bank.tanMethodsAvailableForUser.firstOrNull { it.type != TanMethodType.ChipTanUsb } - ?: bank.tanMethodsAvailableForUser.firstOrNull() - } - protected open fun updateBankData(bank: BankData, response: BankResponse) { modelMapper.updateBankData(bank, response) diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/util/TanMethodSelector.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/util/TanMethodSelector.kt new file mode 100644 index 00000000..fef1ce35 --- /dev/null +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/util/TanMethodSelector.kt @@ -0,0 +1,37 @@ +package net.dankito.banking.fints.util + +import net.dankito.banking.fints.model.TanMethod +import net.dankito.banking.fints.model.TanMethodType + + +open class TanMethodSelector { + + companion object { + val NonVisual = listOf(TanMethodType.AppTan, TanMethodType.SmsTan, TanMethodType.ChipTanManuell, TanMethodType.EnterTan) + } + + + open fun getSuggestedTanMethod(tanMethods: List): TanMethod? { + return tanMethods.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan && it.type != TanMethodType.ChipTanManuell } + ?: tanMethods.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan } + ?: tanMethods.firstOrNull { it.type != TanMethodType.ChipTanUsb } + ?: tanMethods.firstOrNull() + } + + open fun findPreferredTanMethod(tanMethods: List, preferredTanMethods: List?): TanMethod? { + preferredTanMethods?.forEach { preferredTanMethodType -> + tanMethods.firstOrNull { it.type == preferredTanMethodType }?.let { + return it + } + } + + return null + } + + open fun selectNonVisual(tanMethods: List): TanMethod? { + return findPreferredTanMethod(tanMethods, NonVisual) + ?: tanMethods.firstOrNull { it.displayName.contains("manuell", true) } + ?: tanMethods.firstOrNull() + } + +} \ No newline at end of file