libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit c31db1a6cb2adc62e0bd7a564c3b90e8ec3ae3f7
parent 906fad0cc183c43c764256cea11544e25ba08098
Author: Antoine A <>
Date:   Fri,  1 Dec 2023 10:56:30 +0000

Move all constants in a single file and remove redundant logging

Diffstat:
Mbank/src/main/kotlin/tech/libeufin/bank/Config.kt | 1-
Abank/src/main/kotlin/tech/libeufin/bank/Constants.kt | 40++++++++++++++++++++++++++++++++++++++++
Mbank/src/main/kotlin/tech/libeufin/bank/CoreBankApi.kt | 9+++------
Mbank/src/main/kotlin/tech/libeufin/bank/Main.kt | 11+++--------
Mbank/src/main/kotlin/tech/libeufin/bank/TalerCommon.kt | 5++++-
Mbank/src/main/kotlin/tech/libeufin/bank/helpers.kt | 1-
Mbank/src/test/kotlin/CoreBankApiTest.kt | 4++--
7 files changed, 52 insertions(+), 19 deletions(-)

diff --git a/bank/src/main/kotlin/tech/libeufin/bank/Config.kt b/bank/src/main/kotlin/tech/libeufin/bank/Config.kt @@ -28,7 +28,6 @@ import org.slf4j.LoggerFactory import tech.libeufin.util.DatabaseConfig private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.bank.Config") -val BANK_CONFIG_SOURCE = ConfigSource("libeufin", "libeufin-bank", "libeufin-bank") /** * Application the parsed configuration. diff --git a/bank/src/main/kotlin/tech/libeufin/bank/Constants.kt b/bank/src/main/kotlin/tech/libeufin/bank/Constants.kt @@ -0,0 +1,39 @@ +/* + * This file is part of LibEuFin. + * Copyright (C) 2023 Stanisci and Dold. + + * LibEuFin is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation; either version 3, or + * (at your option) any later version. + + * LibEuFin is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General + * Public License for more details. + + * You should have received a copy of the GNU Affero General Public + * License along with LibEuFin; see the file COPYING. If not, see + * <http://www.gnu.org/licenses/> + */ +package tech.libeufin.bank + +import ConfigSource +import java.time.Duration + +// Config +val BANK_CONFIG_SOURCE = ConfigSource("libeufin", "libeufin-bank", "libeufin-bank") + +// TAN +const val TAN_RETRY_COUNTER: Int = 3; +val TAN_VALIDITY_PERIOD: Duration = Duration.ofHours(1) +val TAN_RETRANSMISSION_PERIOD: Duration = Duration.ofMinutes(1) + +// Token +val TOKEN_DEFAULT_DURATION: java.time.Duration = Duration.ofDays(1L) + +// Account +val RESERVED_ACCOUNTS = setOf("admin", "bank") + +// Security +const val MAX_BODY_LENGTH: Long = 4 * 1024 // 4kB +\ No newline at end of file diff --git a/bank/src/main/kotlin/tech/libeufin/bank/CoreBankApi.kt b/bank/src/main/kotlin/tech/libeufin/bank/CoreBankApi.kt @@ -141,7 +141,7 @@ private fun Routing.coreBankAccountsApi(db: Database, ctx: BankConfig) { post("/accounts") { val req = call.receive<RegisterAccountRequest>() // Prohibit reserved usernames: - if (reservedAccounts.contains(req.username)) + if (RESERVED_ACCOUNTS.contains(req.username)) throw conflict( "Username '${req.username}' is reserved.", TalerErrorCode.BANK_RESERVED_USERNAME_CONFLICT @@ -187,8 +187,9 @@ private fun Routing.coreBankAccountsApi(db: Database, ctx: BankConfig) { requireAdmin = !ctx.allowAccountDeletion ) { delete("/accounts/{USERNAME}") { + // TODO prevent delection if exchange account if conversion is enabled // Not deleting reserved names. - if (reservedAccounts.contains(username)) + if (RESERVED_ACCOUNTS.contains(username)) throw conflict( "Cannot delete reserved accounts", TalerErrorCode.BANK_RESERVED_USERNAME_CONFLICT @@ -479,10 +480,6 @@ private fun Routing.coreBankWithdrawalApi(db: Database, ctx: BankConfig) { } private fun Routing.coreBankCashoutApi(db: Database, ctx: BankConfig) = conditional(ctx.allowConversion) { - val TAN_RETRY_COUNTER: Int = 3; - val TAN_VALIDITY_PERIOD: Duration = Duration.ofHours(1) - val TAN_RETRANSMISSION_PERIOD: Duration = Duration.ofMinutes(1) - auth(db, TokenScope.readwrite) { post("/accounts/{USERNAME}/cashouts") { val req = call.receive<CashoutRequest>() diff --git a/bank/src/main/kotlin/tech/libeufin/bank/Main.kt b/bank/src/main/kotlin/tech/libeufin/bank/Main.kt @@ -55,10 +55,7 @@ import org.postgresql.util.PSQLState import tech.libeufin.bank.AccountDAO.* import tech.libeufin.util.* -// GLOBALS private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.bank.Main") -val TOKEN_DEFAULT_DURATION: java.time.Duration = Duration.ofDays(1L) -private val MAX_BODY_LENGTH: Long = 4 * 1024 // 4kB /** * This plugin check for body lenght limit and inflates the requests that have "Content-Encoding: deflate" @@ -167,13 +164,9 @@ fun Application.corebankWebApp(db: Database, ctx: BankConfig) { * to get the most detailed message, we must consider BOTH sides: * the 'cause' AND its root cause! */ - logger.error(cause.message) var rootCause: Throwable? = cause.cause while (rootCause?.cause != null) rootCause = rootCause.cause - /* Here getting _some_ error message, by giving precedence - * to the root cause, as otherwise JSON details would be lost. */ - logger.error(rootCause?.message) // Telling apart invalid JSON vs missing parameter vs invalid parameter. val talerErrorCode = when (cause) { is MissingRequestParameterException -> @@ -188,6 +181,8 @@ fun Application.corebankWebApp(db: Database, ctx: BankConfig) { badRequest( cause.message, talerErrorCode, + /* Here getting _some_ error message, by giving precedence + * to the root cause, as otherwise JSON details would be lost. */ rootCause?.message ) ) @@ -344,7 +339,7 @@ class CreateAccount : CliktCommand("Create an account", name = "create-account") val dbCfg = cfg.loadDbConfig() val db = Database(dbCfg.dbConnStr, ctx.regionalCurrency, ctx.fiatCurrency) runBlocking { - if (reservedAccounts.contains(json.username)) { + if (RESERVED_ACCOUNTS.contains(json.username)) { throw Exception("Username '${json.username}' is reserved") } diff --git a/bank/src/main/kotlin/tech/libeufin/bank/TalerCommon.kt b/bank/src/main/kotlin/tech/libeufin/bank/TalerCommon.kt @@ -40,7 +40,6 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.bank.TalerCommon") -const val MAX_SAFE_INTEGER = 9007199254740991L; // 2^53 - 1 /** 32-byte Crockford's Base32 encoded data */ @Serializable(with = Base32Crockford32B.Serializer::class) @@ -390,6 +389,10 @@ data class RelativeTime( override val descriptor: SerialDescriptor = JsonElement.serializer().descriptor } + + companion object { + private const val MAX_SAFE_INTEGER = 9007199254740991L; // 2^53 - 1 + } } diff --git a/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt b/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt @@ -40,7 +40,6 @@ import tech.libeufin.bank.AccountDAO.* import tech.libeufin.util.* private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.bank.helpers") -val reservedAccounts = setOf("admin", "bank") // exchange ? fun ApplicationCall.expectUriComponent(componentName: String) = maybeUriComponent(componentName) ?: throw badRequest( diff --git a/bank/src/test/kotlin/CoreBankApiTest.kt b/bank/src/test/kotlin/CoreBankApiTest.kt @@ -193,7 +193,7 @@ class CoreBankAccountsApiTest { }.assertOk() // Reserved account - reservedAccounts.forEach { + RESERVED_ACCOUNTS.forEach { client.post("/accounts") { json { "username" to it @@ -276,7 +276,7 @@ class CoreBankAccountsApiTest { }.assertNotFound(TalerErrorCode.BANK_UNKNOWN_ACCOUNT) // Reserved account - reservedAccounts.forEach { + RESERVED_ACCOUNTS.forEach { client.delete("/accounts/$it") { pwAuth("admin") }.assertConflict(TalerErrorCode.BANK_RESERVED_USERNAME_CONFLICT)