From fce10927c9c83ae48e1b2f40da1e3c0b4474b789 Mon Sep 17 00:00:00 2001 From: MS Date: Mon, 15 Feb 2021 18:42:23 +0100 Subject: Fix payto parser, remove add-incoming API. --- nexus/src/main/kotlin/tech/libeufin/nexus/Taler.kt | 73 ++-------------------- util/src/main/kotlin/Payto.kt | 34 +++++----- util/src/test/kotlin/PaytoTest.kt | 6 ++ 3 files changed, 29 insertions(+), 84 deletions(-) diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Taler.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Taler.kt index e5e13e4d..16519c46 100644 --- a/nexus/src/main/kotlin/tech/libeufin/nexus/Taler.kt +++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Taler.kt @@ -249,7 +249,9 @@ private suspend fun talerTransfer(call: ApplicationCall) { Pain001Data( creditorIban = creditorData.iban, creditorBic = creditorData.bic, - creditorName = creditorData.name, + creditorName = creditorData.name ?: throw NexusError( + HttpStatusCode.BadRequest, "Payto did not mention account owner" + ), subject = transferRequest.wtid, sum = amountObj.amount, currency = amountObj.currency @@ -289,69 +291,6 @@ fun roundTimestamp(t: GnunetTimestamp): GnunetTimestamp { return GnunetTimestamp(t.t_ms - (t.t_ms % 1000)) } -/** - * Serve a /taler/admin/add-incoming - */ -private suspend fun talerAddIncoming(call: ApplicationCall, httpClient: HttpClient) { - val facadeID = expectNonNull(call.parameters["fcid"]) - call.request.requirePermission(PermissionQuery("facade", facadeID, "facade.talerWireGateway.addIncoming")) - val addIncomingData = call.receive() - val debtor = parsePayto(addIncomingData.debit_account) - val res = transaction { - val facadeState = getTalerFacadeState(facadeID) - val facadeBankAccount = getTalerFacadeBankAccount(facadeID) - return@transaction object { - val facadeLastSeen = facadeState.highestSeenMessageSerialId - val facadeIban = facadeBankAccount.iban - val facadeBic = facadeBankAccount.bankCode - val facadeHolderName = facadeBankAccount.accountHolder - } - } - - /** forward the payment information to the sandbox. */ - val response = httpClient.post( - urlString = "http://localhost:5000/admin/payments", - block = { - /** FIXME: ideally Jackson should define such request body. */ - val parsedAmount = parseAmount(addIncomingData.amount) - this.body = """{ - "creditorIban": "${res.facadeIban}", - "creditorBic": "${res.facadeBic}", - "creditorName": "${res.facadeHolderName}", - "debitorIban": "${debtor.iban}", - "debitorBic": "${debtor.bic}", - "debitorName": "${debtor.name}", - "amount": "${parsedAmount.amount}", - "currency": "${parsedAmount.currency}", - "direction": "CRDT", - "subject": "${addIncomingData.reserve_pub}", - "uid": "${getRandomString(8)}" - }""".trimIndent() - contentType(ContentType.Application.Json) - } - ) - if (response.status != HttpStatusCode.OK) { - throw NexusError( - HttpStatusCode.InternalServerError, - "Could not forward the 'add-incoming' payment to the bank (sandbox)" - ) - } - return call.respond( - TextContent( - customConverter( - TalerAddIncomingResponse( - timestamp = GnunetTimestamp( - System.currentTimeMillis() - ), - row_id = res.facadeLastSeen - ) - ), - ContentType.Application.Json - ) - ) -} - - private fun ingestOneIncomingTransaction(payment: NexusBankTransactionEntity, txDtls: TransactionDetails) { val subject = txDtls.unstructuredRemittanceInformation val debtorName = txDtls.debtor?.name @@ -637,7 +576,7 @@ fun talerFacadeRoutes(route: Route, httpClient: HttpClient) { route.get("/config") { val facadeId = ensureNonNull(call.parameters["fcid"]) - call.request.requirePermission(PermissionQuery("facade", facadeId, "facade.talerWireGateway.addIncoming")) + call.request.requirePermission(PermissionQuery("facade", facadeId, "facade.talerWireGateway.config")) call.respond(object { val version = "0.0.0" val name = "taler-wire-gateway" @@ -649,10 +588,6 @@ fun talerFacadeRoutes(route: Route, httpClient: HttpClient) { talerTransfer(call) return@post } - route.post("/admin/add-incoming") { - talerAddIncoming(call, httpClient) - return@post - } route.get("/history/outgoing") { historyOutgoing(call) return@get diff --git a/util/src/main/kotlin/Payto.kt b/util/src/main/kotlin/Payto.kt index 04fda1ac..ed52d1b1 100644 --- a/util/src/main/kotlin/Payto.kt +++ b/util/src/main/kotlin/Payto.kt @@ -6,7 +6,7 @@ import java.net.URI * Helper data structures. */ data class Payto( - val name: String, + val name: String?, val iban: String, val bic: String? ) @@ -22,22 +22,26 @@ fun parsePayto(paytoLine: String): Payto { if (javaParsedUri.scheme != "payto") { throw InvalidPaytoError("'${paytoLine}' is not payto") } - val queryStringAsList = javaParsedUri.query.split("&") - // admit only ONE parameter: receiver-name. - if (queryStringAsList.size != 1) { - throw InvalidPaytoError("'${paytoLine}' has unsupported query string") - } - val splitParameter = queryStringAsList.first().split("=") - if (splitParameter.first() != "receiver-name" && splitParameter.first() != "sender-name") { - throw InvalidPaytoError("'${paytoLine}' has unsupported query string") - } - val receiverName = splitParameter.last() + + val accountOwner = if (javaParsedUri.query != null) { + val queryStringAsList = javaParsedUri.query.split("&") + // admit only ONE parameter: receiver-name. + if (queryStringAsList.size != 1) { + throw InvalidPaytoError("'${paytoLine}' has unsupported query string") + } + val splitParameter = queryStringAsList.first().split("=") + if (splitParameter.first() != "receiver-name" && splitParameter.first() != "sender-name") { + throw InvalidPaytoError("'${paytoLine}' has unsupported query string") + } + splitParameter.last() + } else null + val splitPath = javaParsedUri.path.split("/").filter { it.isNotEmpty() } if (splitPath.size > 2) { throw InvalidPaytoError("too many path segments in iban payto URI") } - if (splitPath.size < 2) { - return Payto(iban = splitPath[0], name = receiverName, bic = null) - } - return Payto(iban = splitPath[1], bic = splitPath[0], name = receiverName) + val (iban, bic) = if (splitPath.size == 1) { + Pair(splitPath[0], null) + } else Pair(splitPath[1], splitPath[0]) + return Payto(iban = iban, bic = bic, name = accountOwner) } \ No newline at end of file diff --git a/util/src/test/kotlin/PaytoTest.kt b/util/src/test/kotlin/PaytoTest.kt index 5c1f50a9..635966d6 100644 --- a/util/src/test/kotlin/PaytoTest.kt +++ b/util/src/test/kotlin/PaytoTest.kt @@ -7,6 +7,12 @@ class PaytoTest { @Test fun wrongCases() { + try { + parsePayto("payto://iban/IBAN/BIC") + } catch (e: InvalidPaytoError) { + println(e) + println("must give IBAN _and_ BIC") + } try { parsePayto("http://iban/BIC123/IBAN123?receiver-name=The%20Name") } catch (e: InvalidPaytoError) { -- cgit v1.2.3