summaryrefslogtreecommitdiff
path: root/bank/src/main/kotlin/tech/libeufin/bank/Main.kt
blob: 7d32939933a81a8d337916755fb92fce85f0165e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2019 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/>
 */
// Main HTTP handlers and related data definitions.

package tech.libeufin.bank

import TalerConfig
import TalerConfigError
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.core.context
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.output.CliktHelpFormatter
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.versionOption
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.requestvalidation.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.callloging.*
import kotlinx.serialization.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.statuspages.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.utils.io.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.encodeStructure
import kotlinx.serialization.json.*
import net.taler.common.errorcodes.TalerErrorCode
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.event.Level
import tech.libeufin.util.*
import java.time.Duration
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.zip.InflaterInputStream
import kotlin.system.exitProcess

// GLOBALS
private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.bank.Main")
const val GENERIC_UNDEFINED = -1 // Filler for ECs that don't exist yet.
val TOKEN_DEFAULT_DURATION: java.time.Duration = Duration.ofDays(1L)


/**
 * Application context with the parsed configuration.
 */
data class BankApplicationContext(
    /**
     * Main, internal currency of the bank.
     */
    val currency: String,
    /**
     * Restrict account registration to the administrator.
     */
    val restrictRegistration: Boolean,
    /**
     * Restrict account deletion to the administrator.
     */
    val restrictAccountDeletion: Boolean,
    /**
     * Cashout currency, if cashouts are supported.
     */
    val cashoutCurrency: String?,
    /**
     * Default limit for the debt that a customer can have.
     * Can be adjusted per account after account creation.
     */
    val defaultCustomerDebtLimit: TalerAmount,
    /**
     * Debt limit of the admin account.
     */
    val defaultAdminDebtLimit: TalerAmount,
    /**
     * If true, transfer a registration bonus from the admin
     * account to the newly created account.
     */
    val registrationBonusEnabled: Boolean,
    /**
     * Only set if registration bonus is enabled.
     */
    val registrationBonus: TalerAmount?,
    /**
     * Exchange that the bank suggests to wallets for withdrawal.
     */
    val suggestedWithdrawalExchange: String?,
    /**
     * URL where the user should be redirected to complete the captcha.
     * It can contain the substring "{woid}" that is going to be replaced 
     * with the withdrawal operation id and should point where the bank
     * SPA is located.
     */
    val spaCaptchaURL: String?,
)


/**
 * This custom (de)serializer interprets the Timestamp JSON
 * type of the Taler common API.  In particular, it is responsible
 * for _serializing_ timestamps, as this datatype is so far
 * only used to respond to clients.
 */
object TalerProtocolTimestampSerializer : KSerializer<TalerProtocolTimestamp> {
    override fun serialize(encoder: Encoder, value: TalerProtocolTimestamp) {
        // Thanks: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#hand-written-composite-serializer
        encoder.encodeStructure(descriptor) {
            if (value.t_s == Instant.MAX) {
                encodeStringElement(descriptor, 0, "never")
                return@encodeStructure
            }
            encodeLongElement(descriptor, 0, value.t_s.epochSecond)
        }
    }

    override fun deserialize(decoder: Decoder): TalerProtocolTimestamp {
        val jsonInput = decoder as? JsonDecoder ?: throw internalServerError("TalerProtocolTimestamp had no JsonDecoder")
        val json = try {
            jsonInput.decodeJsonElement().jsonObject
        } catch (e: Exception) {
            throw badRequest(
                "Did not find a JSON object for TalerProtocolTimestamp: ${e.message}",
                TalerErrorCode.TALER_EC_GENERIC_JSON_INVALID
            )
        }
        val maybeTs = json["t_s"]?.jsonPrimitive ?: throw badRequest("Taler timestamp invalid: t_s field not found")
        if (maybeTs.isString) {
            if (maybeTs.content != "never") throw badRequest("Only 'never' allowed for t_s as string, but '${maybeTs.content}' was found")
            return TalerProtocolTimestamp(t_s = Instant.MAX)
        }
        val ts: Long = maybeTs.longOrNull
            ?: throw badRequest("Could not convert t_s '${maybeTs.content}' to a number")
        // Not allowing negative values, despite java.time allowance.
        if (ts < 0)
            throw badRequest("Negative timestamp not allowed.")
        val instant = try {
            Instant.ofEpochSecond(ts)
        } catch (e: Exception) {
            logger.error("Could not get Instant from t_s: $ts: ${e.message}")
            throw badRequest("Could not serialize this t_s: ${ts}")
        }
        return TalerProtocolTimestamp(instant)
    }

    override val descriptor: SerialDescriptor =
        buildClassSerialDescriptor("TalerProtocolTimestamp") {
            element<JsonElement>("t_s")
        }
}

/**
 * This custom (de)serializer interprets the RelativeTime JSON
 * type.  In particular, it is responsible for converting the
 * "forever" string into Long.MAX_VALUE.  Any other numeric value
 * is passed as is.
 */
object RelativeTimeSerializer : KSerializer<RelativeTime> {
    /**
     * Internal representation to JSON.
     */
    override fun serialize(encoder: Encoder, value: RelativeTime) {
        // Thanks: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#hand-written-composite-serializer
        encoder.encodeStructure(descriptor) {
            if (value.d_us == ChronoUnit.FOREVER.duration) {
                encodeStringElement(descriptor, 0, "forever")
                return@encodeStructure
            }
            val dUs = try {
                value.d_us.toNanos() / 1000L
            } catch (e: Exception) {
                logger.error(e.message)
                // Bank's fault, as each numeric value should be checked before entering the system.
                throw internalServerError("Could not convert java.time.Duration to JSON")
            }
            encodeLongElement(descriptor, 0, dUs)
        }
    }

    /**
     * JSON to internal representation.
     */
    override fun deserialize(decoder: Decoder): RelativeTime {
        val jsonInput = decoder as? JsonDecoder ?: throw internalServerError("RelativeTime had no JsonDecoder")
        val json = try {
            jsonInput.decodeJsonElement().jsonObject
        } catch (e: Exception) {
            throw badRequest(
                "Did not find a RelativeTime JSON object: ${e.message}",
                TalerErrorCode.TALER_EC_GENERIC_JSON_INVALID
            )
        }
        val maybeDUs = json["d_us"]?.jsonPrimitive ?: throw badRequest("Relative time invalid: d_us field not found")
        if (maybeDUs.isString) {
            if (maybeDUs.content != "forever") throw badRequest("Only 'forever' allowed for d_us as string, but '${maybeDUs.content}' was found")
            return RelativeTime(d_us = ChronoUnit.FOREVER.duration)
        }
        val dUs: Long = maybeDUs.longOrNull
            ?: throw badRequest("Could not convert d_us: '${maybeDUs.content}' to a number")
        if (dUs < 0)
            throw badRequest("Negative duration specified.")
        val duration = try {
            Duration.of(dUs, ChronoUnit.MICROS)
        } catch (e: Exception) {
            logger.error("Could not get Duration out of d_us content: ${dUs}. ${e.message}")
            throw badRequest("Could not get Duration out of d_us content: ${dUs}")
        }
        return RelativeTime(d_us = duration)
    }

    override val descriptor: SerialDescriptor =
        buildClassSerialDescriptor("RelativeTime") {
            // JsonElement helps to obtain "union" type Long|String
            element<JsonElement>("d_us")
        }
}

object TalerAmountSerializer : KSerializer<TalerAmount> {

    override val descriptor: SerialDescriptor =
        PrimitiveSerialDescriptor("TalerAmount", PrimitiveKind.STRING)

    override fun serialize(encoder: Encoder, value: TalerAmount) {
        encoder.encodeString(value.toString())
    }

    override fun deserialize(decoder: Decoder): TalerAmount {
        val maybeAmount = try {
            decoder.decodeString()
        } catch (e: Exception) {
            throw badRequest(
                "Did not find any Taler amount as string: ${e.message}",
                TalerErrorCode.TALER_EC_GENERIC_JSON_INVALID
            )
        }
        return parseTalerAmount(maybeAmount)
    }
}

/**
 * This plugin inflates the requests that have "Content-Encoding: deflate"
 */
val corebankDecompressionPlugin = createApplicationPlugin("RequestingBodyDecompression") {
    onCallReceive { call ->
        transformBody { data ->
            if (call.request.headers[HttpHeaders.ContentEncoding] == "deflate") {
                logger.debug("Inflating request..")
                val brc = try {
                    withContext(Dispatchers.IO) {
                        val inflated = InflaterInputStream(data.toInputStream())

                        @Suppress("BlockingMethodInNonBlockingContext")
                        val bytes = inflated.readAllBytes()
                        ByteReadChannel(bytes)
                    }
                } catch (e: Exception) {
                    logger.error("Deflated request failed to inflate: ${e.message}")
                    throw badRequest(
                        hint = "Could not inflate request",
                        talerErrorCode = TalerErrorCode.TALER_EC_GENERIC_COMPRESSION_INVALID
                    )
                }
                brc
            } else data
        }
    }
}

/**
 * Set up web server handlers for the Taler corebank API.
 */
fun Application.corebankWebApp(db: Database, ctx: BankApplicationContext) {
    install(CallLogging) {
        this.level = Level.DEBUG
        this.logger = tech.libeufin.bank.logger
        this.format { call ->
            "${call.response.status()}, ${call.request.httpMethod.value} ${call.request.path()}"
        }
    }
    install(CORS) {
        anyHost()
        allowHeader(HttpHeaders.Authorization)
        allowHeader(HttpHeaders.ContentType)
        allowMethod(HttpMethod.Options)
        allowMethod(HttpMethod.Patch)
        allowMethod(HttpMethod.Delete)
        allowCredentials = true
    }
    install(corebankDecompressionPlugin)
    install(IgnoreTrailingSlash)
    install(ContentNegotiation) {
        json(Json {
            @OptIn(ExperimentalSerializationApi::class)
            explicitNulls = false
            encodeDefaults = true
            prettyPrint = true
            ignoreUnknownKeys = true
        })
    }
    install(RequestValidation)
    install(StatusPages) {
        /**
         * This branch triggers when the Ktor layers detect one
         * invalid request.  It _might_ be thrown by the bank's
         * actual logic, but that should be avoided because this
         * (Ktor native) type doesn't easily map to the Taler error
         * format.
         */
        exception<BadRequestException> { call, cause ->
            /**
             * NOTE: extracting the root cause helps with JSON error messages,
             * because they mention the particular way they are invalid, but OTOH
             * it loses (by getting null) other error messages, like for example
             * the one from MissingRequestParameterException.  Therefore, in order
             * 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 ->
                    TalerErrorCode.TALER_EC_GENERIC_PARAMETER_MISSING

                is ParameterConversionException ->
                    TalerErrorCode.TALER_EC_GENERIC_PARAMETER_MALFORMED

                else -> TalerErrorCode.TALER_EC_GENERIC_JSON_INVALID
            }
            call.respond(
                status = HttpStatusCode.BadRequest,
                message = TalerError(
                    code = talerErrorCode.code,
                    hint = cause.message,
                    detail = rootCause?.message
                )
            )
        }
        /**
         * This branch triggers when a bank handler throws it, and namely
         * after one logical failure of the request(-handling).  This branch
         * should be preferred to catch errors, as it allows to include the
         * Taler specific error detail.
         */
        exception<LibeufinBankException> { call, cause ->
            logger.error(cause.talerError.hint)
            // Stacktrace if bank's fault
            if (cause.httpStatus.toString().startsWith('5'))
                cause.printStackTrace()
            call.respond(
                status = cause.httpStatus,
                message = cause.talerError
            )
        }
        // Catch-all branch to mean that the bank wasn't able to manage one error.
        exception<Exception> { call, cause ->
            cause.printStackTrace()
            logger.error(cause.message)
            call.respond(
                status = HttpStatusCode.InternalServerError,
                message = TalerError(
                    code = TalerErrorCode.TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE.code,
                    hint = cause.message
                )
            )
        }
    }
    routing {
        get("/config") {
            call.respond(Config())
            return@get
        }
        this.accountsMgmtHandlers(db, ctx)
        this.talerIntegrationHandlers(db, ctx)
        this.talerWireGatewayHandlers(db, ctx)
    }
}

class LibeufinBankCommand : CliktCommand() {
    init {
        versionOption(getVersion())
        subcommands(ServeBank(), BankDbInit(), ChangePw())
    }

    override fun run() = Unit
}

fun durationFromPretty(s: String): Long {
    var durationUs: Long = 0;
    var currentNum = "";
    var parsingNum = true
    for (c in s) {
        if (c >= '0' && c <= '9') {
            if (!parsingNum) {
                throw Error("invalid duration, unexpected number")
            }
            currentNum += c
            continue
        }
        if (c == ' ') {
            if (currentNum != "") {
                parsingNum = false
            }
            continue
        }
        if (currentNum == "") {
            throw Error("invalid duration, missing number")
        }
        val n = currentNum.toInt(10)
        durationUs += when (c) {
            's' -> { n * 1000000 }
            'm' -> { n * 1000000 * 60 }
            'h' -> { n * 1000000 * 60 * 60 }
            'd' -> { n * 1000000 * 60 * 60 * 24 }
            else -> { throw Error("invalid duration, unsupported unit '$c'") }
        }
        parsingNum = true
        currentNum = ""
    }
    return durationUs
}

fun TalerConfig.requireValueAmount(section: String, option: String, currency: String): TalerAmount {
    val amountStr = lookupValueString(section, option)
    if (amountStr == null) {
        throw TalerConfigError("expected amount for section $section, option $option, but config value is empty")
    }
    val amount = parseTalerAmount2(amountStr, FracDigits.EIGHT)
    if (amount == null) {
        throw TalerConfigError("expected amount for section $section, option $option, but amount is malformed")
    }
    if (amount.currency != currency) {
        throw TalerConfigError(
            "expected amount for section $section, option $option, but currency is wrong (got ${amount.currency} expected $currency"
        )
    }
    return amount
}

/**
 * Read the configuration of the bank from a config file.
 * Throws an exception if the configuration is malformed.
 */
fun readBankApplicationContextFromConfig(cfg: TalerConfig): BankApplicationContext {
    val currency = cfg.requireValueString("libeufin-bank", "currency")
    return BankApplicationContext(
        currency = currency,
        restrictRegistration = cfg.lookupValueBooleanDefault("libeufin-bank", "restrict_registration", false),
        cashoutCurrency = cfg.lookupValueString("libeufin-bank", "cashout_currency"),
        defaultCustomerDebtLimit = cfg.requireValueAmount("libeufin-bank", "default_customer_debt_limit", currency),
        registrationBonusEnabled = cfg.lookupValueBooleanDefault("libeufin-bank", "registration_bonus_enabled", false),
        registrationBonus = cfg.requireValueAmount("libeufin-bank", "registration_bonus", currency),
        suggestedWithdrawalExchange = cfg.lookupValueString("libeufin-bank", "suggested_withdrawal_exchange"),
        defaultAdminDebtLimit = cfg.requireValueAmount("libeufin-bank", "default_admin_debt_limit", currency),
        spaCaptchaURL = cfg.lookupValueString("libeufin-bank", "spa_captcha_url"),
        restrictAccountDeletion = cfg.lookupValueBooleanDefault("libeufin-bank", "restrict_account_deletion", true)
    )
}


class BankDbInit : CliktCommand("Initialize the libeufin-bank database", name = "dbinit") {
    private val configFile by option(
        "--config", "-c",
        help = "set the configuration file"
    )

    private val requestReset by option(
        "--reset", "-r",
        help = "reset database (DANGEROUS: All existing data is lost)"
    ).flag()

    init {
        context {
            helpFormatter = CliktHelpFormatter(showDefaultValues = true)
        }
    }

    override fun run() {
        val config = TalerConfig.load(this.configFile)
        val dbConnStr = config.requireValueString("libeufin-bankdb-postgres", "config")
        val sqlDir = config.requireValuePath("libeufin-bankdb-postgres", "sql_dir")
        if (requestReset) {
            resetDatabaseTables(dbConnStr, sqlDir)
        }
        initializeDatabaseTables(dbConnStr, sqlDir)
    }
}

class ServeBank : CliktCommand("Run libeufin-bank HTTP server", name = "serve") {
    private val configFile by option(
        "--config", "-c",
        help = "set the configuration file"
    )
    init {
        context {
            helpFormatter = CliktHelpFormatter(showDefaultValues = true)
        }
    }

    override fun run() {
        val config = TalerConfig.load(this.configFile)
        val ctx = readBankApplicationContextFromConfig(config)
        val dbConnStr = config.requireValueString("libeufin-bankdb-postgres", "config")
        logger.info("using database '$dbConnStr'")
        val serveMethod = config.requireValueString("libeufin-bank", "serve")
        if (serveMethod.lowercase() != "tcp") {
            logger.info("Can only serve libeufin-bank via TCP")
            exitProcess(1)
        }
        val servePortLong = config.requireValueNumber("libeufin-bank", "port")
        val servePort = servePortLong.toInt()
        val db = Database(dbConnStr, ctx.currency)
        if (!maybeCreateAdminAccount(db, ctx)) // logs provided by the helper
            exitProcess(1)
        embeddedServer(Netty, port = servePort, configure = {
            // Disable threads for now, the DB isn't thread safe yet.
            connectionGroupSize = 1
            workerGroupSize = 1
            callGroupSize = 1
        }) {
            corebankWebApp(db, ctx)
        }.start(wait = true)
    }
}

class ChangePw : CliktCommand("Change account password", name = "passwd") {
    private val configFile by option(
        "--config", "-c",
        help = "set the configuration file"
    )
    private val account by argument("account")
    private val password by argument("password")
    init {
        context {
            helpFormatter = CliktHelpFormatter(showDefaultValues = true)
        }
    }

    override fun run() {
        val config = TalerConfig.load(this.configFile)
        val ctx = readBankApplicationContextFromConfig(config)
        val dbConnStr = config.requireValueString("libeufin-bankdb-postgres", "config")
        config.requireValueNumber("libeufin-bank", "port")
        val db = Database(dbConnStr, ctx.currency)
        if (!maybeCreateAdminAccount(db, ctx)) // logs provided by the helper
            exitProcess(1)

        if (!db.customerChangePassword(account, CryptoUtil.hashpw(password))) {
            println("password change failed")
            exitProcess(1)
        } else {
            println("password change succeeded")
        }
    }
}

fun main(args: Array<String>) {
    LibeufinBankCommand().main(args)
}