[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libeufin] 01/02: fix payto-parser
From: |
gnunet |
Subject: |
[libeufin] 01/02: fix payto-parser |
Date: |
Tue, 15 Dec 2020 01:38:09 +0100 |
This is an automated email from the git hooks/post-receive script.
ms pushed a commit to branch master
in repository libeufin.
commit 6045f4885b5020fc052b1407965f343e2868ecfc
Author: MS <ms@taler.net>
AuthorDate: Tue Dec 15 01:23:46 2020 +0100
fix payto-parser
---
util/src/main/kotlin/Payto.kt | 19 ++++++++++---------
util/src/test/kotlin/PaytoTest.kt | 29 ++++++++++++++++++++++-------
2 files changed, 32 insertions(+), 16 deletions(-)
diff --git a/util/src/main/kotlin/Payto.kt b/util/src/main/kotlin/Payto.kt
index a1cccee..a35aa71 100644
--- a/util/src/main/kotlin/Payto.kt
+++ b/util/src/main/kotlin/Payto.kt
@@ -1,7 +1,5 @@
package tech.libeufin.util
-import io.ktor.http.HttpStatusCode
-import tech.libeufin.util.EbicsProtocolError
import java.net.URI
/**
@@ -10,28 +8,31 @@ import java.net.URI
data class Payto(
val name: String,
val iban: String,
- val bic: String = "NOTGIVEN"
+ val bic: String
)
+class InvalidPaytoError(msg: String) : Exception(msg)
fun parsePayto(paytoLine: String): Payto {
+ if (!"^payto://".toRegex().containsMatchIn(paytoLine)) throw
InvalidPaytoError("Invalid payto line: $paytoLine")
val javaParsedUri = try {
URI(paytoLine)
} catch (e: java.lang.Exception) {
- throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' is
not a valid URI")
+ throw InvalidPaytoError("'${paytoLine}' is not a valid URI")
}
if (javaParsedUri.scheme != "payto") {
- throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' is
not payto")
+ throw InvalidPaytoError("'${paytoLine}' is not payto")
}
- val iban = javaParsedUri.path.split("/").last()
val queryStringAsList = javaParsedUri.query.split("&")
// admit only ONE parameter: receiver-name.
if (queryStringAsList.size != 1) {
- throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}'
has unsupported query string")
+ throw InvalidPaytoError("'${paytoLine}' has unsupported query string")
}
val splitParameter = queryStringAsList.first().split("=")
if (splitParameter.first() != "receiver-name" && splitParameter.first() !=
"sender-name") {
- throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}'
has unsupported query string")
+ throw InvalidPaytoError("'${paytoLine}' has unsupported query string")
}
val receiverName = splitParameter.last()
- return Payto(iban = iban, name = receiverName)
+ val split_path = javaParsedUri.path.split("/").filter { it.isNotEmpty() }
+ if (split_path.size != 2) throw InvalidPaytoError("BIC and IBAN are both
mandatory ($split_path)")
+ return Payto(iban = split_path[1], bic = split_path[0], name =
receiverName)
}
\ No newline at end of file
diff --git a/util/src/test/kotlin/PaytoTest.kt
b/util/src/test/kotlin/PaytoTest.kt
index 761c445..5c1f50a 100644
--- a/util/src/test/kotlin/PaytoTest.kt
+++ b/util/src/test/kotlin/PaytoTest.kt
@@ -1,26 +1,41 @@
import org.junit.Test
import tech.libeufin.util.EbicsProtocolError
+import tech.libeufin.util.InvalidPaytoError
import tech.libeufin.util.parsePayto
class PaytoTest {
@Test
- fun parsePaytoTest() {
- val noBic = parsePayto("payto://iban/IBAN123?receiver-name=The%20Name")
- assert(noBic.iban == "IBAN123" && noBic.name == "The Name")
- val withBic =
parsePayto("payto://iban/BIC123/IBAN123?receiver-name=The%20Name")
- assert(withBic.iban == "IBAN123" && withBic.name == "The Name")
+ fun wrongCases() {
try {
parsePayto("http://iban/BIC123/IBAN123?receiver-name=The%20Name")
- } catch (e: EbicsProtocolError) {
+ } catch (e: InvalidPaytoError) {
+ println(e)
println("wrong scheme was caught")
}
try {
parsePayto(
"payto://iban/BIC123/IBAN123?receiver-name=The%20Name&address=house"
)
- } catch (e: EbicsProtocolError) {
+ } catch (e: InvalidPaytoError) {
+ println(e)
println("more than one parameter isn't allowed")
}
+ try {
+ parsePayto(
+
"payto:iban/BIC123/IBAN123?receiver-name=The%20Name&address=house"
+ )
+ } catch (e: InvalidPaytoError) {
+ println(e)
+ println("'://' missing, invalid Payto")
+ }
+ }
+
+ @Test
+ fun parsePaytoTest() {
+ val withBic =
parsePayto("payto://iban/BIC123/IBAN123?receiver-name=The%20Name")
+ assert(withBic.iban == "IBAN123")
+ assert(withBic.bic == "BIC123")
+ assert(withBic.name == "The Name")
}
}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.