commit d13b09a8a905387eb2013eb42f3fa68a2664f13c
parent 01e4e216098a7c650c45b28c6adf5a0f27f3a1ac
Author: ms <ms@taler.net>
Date: Tue, 19 Oct 2021 09:54:44 +0200
Create customer via Access API
Diffstat:
5 files changed, 56 insertions(+), 4 deletions(-)
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
@@ -117,6 +117,15 @@ object DemobankCustomersTable : LongIdTable() {
val passwordHash = text("passwordHash")
}
+class DemobankCustomerEntity(id: EntityID<Long>) : LongEntity(id) {
+ companion object : LongEntityClass<DemobankCustomerEntity>(DemobankCustomersTable)
+ var isPublic by DemobankCustomersTable.isPublic
+ var demobankConfig by DemobankCustomersTable.demobankConfig
+ var balance by DemobankCustomersTable.balance
+ var username by DemobankCustomersTable.username
+ var passwordHash by DemobankCustomersTable.passwordHash
+}
+
/**
* This table stores RSA public keys of subscribers.
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Helpers.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Helpers.kt
@@ -23,6 +23,7 @@ import io.ktor.http.HttpStatusCode
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction
+import tech.libeufin.util.internalServerError
/**
* Helps to communicate Camt values without having
@@ -87,6 +88,16 @@ fun getBankAccountFromSubscriber(subscriber: EbicsSubscriberEntity): BankAccount
}
}
+fun ensureDemobank(name: String): DemobankConfigEntity {
+ return transaction {
+ val res = DemobankConfigEntity.find {
+ DemobankConfigsTable.name eq name
+ }.firstOrNull()
+ if (res == null) throw internalServerError("Demobank '$name' never created")
+ res
+ }
+}
+
fun getSandboxConfig(name: String?): DemobankConfigEntity? {
return transaction {
if (name == null) {
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
@@ -72,6 +72,11 @@ data class BankAccountRequest(
val currency: String
)
+data class CustomerRegistration(
+ val username: String,
+ val password: String
+)
+
data class CamtParams(
// name/label of the bank account to query.
val bankaccount: String,
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -1075,8 +1075,6 @@ val sandboxApp: Application.() -> Unit = {
get("/accounts/{account_name}") {
// Authenticated. Accesses basic information (balance)
// about an account. (see docs)
-
- // FIXME: Since we now use IBANs everywhere, maybe the account should also be assigned an IBAN
}
get("/accounts/{account_name}/history") {
@@ -1095,8 +1093,34 @@ val sandboxApp: Application.() -> Unit = {
// Get transaction history of a public account
}
- post("/register") {
-
+ // Keeping the prefix "testing" to allow integration tests using this endpoint.
+ post("/testing/register") {
+ // Check demobank was created.
+ val demobank = ensureDemobank(call.getUriComponent("demobankid"))
+ val req = call.receive<CustomerRegistration>()
+ val checkExist = transaction {
+ DemobankCustomerEntity.find {
+ DemobankCustomersTable.username eq req.username
+ }
+ }.firstOrNull()
+ if (checkExist != null) {
+ throw SandboxError(
+ HttpStatusCode.Conflict,
+ "Username ${req.username} not available."
+ )
+ }
+ // Create new customer.
+ requireValidResourceName(req.username)
+ transaction {
+ // FIXME: Since we now use IBANs everywhere, maybe the account should also be assigned an IBAN
+ DemobankCustomerEntity.new {
+ username = req.username
+ passwordHash = CryptoUtil.hashpw(req.password)
+ demobankConfig = demobank.id
+ }
+ }
+ call.respondText("Registration successful")
+ return@post
}
}
diff --git a/sandbox/src/main/resources/logback.xml b/sandbox/src/main/resources/logback.xml
@@ -9,6 +9,9 @@
<logger name="tech.libeufin.sandbox" level="DEBUG" additivity="false">
<appender-ref ref="STDERR" />
</logger>
+ <logger name="tech.libeufin.util" level="DEBUG" additivity="false">
+ <appender-ref ref="STDERR" />
+ </logger>
<logger name="io.netty" level="WARN" />
<logger name="ktor" level="WARN" />