summaryrefslogtreecommitdiff
path: root/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsClient.kt
blob: e2fe7264c0e9e5b8d17bc3509aadc22bd45d3176 (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
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2020 Taler Systems S.A.
 *
 * 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/>
 */

/**
 * High-level interface for the EBICS protocol.
 */
package tech.libeufin.nexus.ebics

import io.ktor.client.HttpClient
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import tech.libeufin.nexus.NexusError
import tech.libeufin.util.*
import tech.libeufin.util.ebics_h005.Ebics3Request
import java.util.*

private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.util")

private suspend inline fun HttpClient.postToBank(url: String, body: String): String {
    if (!XMLUtil.validateFromString(body)) throw NexusError(
        HttpStatusCode.InternalServerError,
        "EBICS (outgoing) document is invalid"
    )
    val response: HttpResponse = try {
        this.post(urlString = url) {
                setBody(body)
        }
    } catch (e: ClientRequestException) {
        logger.error("Exception during request to $url: ${e.message}")
        val returnStatus = if (e.response.status.value == HttpStatusCode.RequestTimeout.value)
            HttpStatusCode.GatewayTimeout
        else HttpStatusCode.BadGateway
        throw NexusError(
            returnStatus,
            e.message
        )
    }
    catch (e: Exception) {
        logger.error("Exception during request to $url: ${e.message}")
        throw NexusError(
            HttpStatusCode.BadGateway,
            e.message ?: "Could not reach the bank"
        )
    }
    /**
     * EBICS should be expected only after a 200 OK response
     * (including problematic ones); throw exception in all the other cases,
     * by echoing what the bank said.
     */
    if (response.status.value != HttpStatusCode.OK.value)
        throw NexusError(
            HttpStatusCode.BadGateway,
            "bank says: ${response.bodyAsText()}"
        )
    return response.bodyAsText()
}

sealed class EbicsDownloadResult

class EbicsDownloadSuccessResult(
    val orderData: ByteArray,
    /**
     * This value points at the EBICS transaction that carried
     * the order data contained in this structure.  That makes
     * possible to log the EBICS transaction that carried one
     * invalid order data, for example.
     */
    val transactionID: String? = null
) : EbicsDownloadResult()

class EbicsDownloadEmptyResult(
    val orderData: ByteArray = ByteArray(0)
) : EbicsDownloadResult()


/**
 * A bank-technical error occurred.
 */
class EbicsDownloadBankErrorResult(
    val returnCode: EbicsReturnCode
) : EbicsDownloadResult()

/**
 * Do an EBICS download transaction.  This includes
 * the initialization phase, transaction phase and receipt phase.
 */
suspend fun doEbicsDownloadTransaction(
    client: HttpClient,
    subscriberDetails: EbicsClientSubscriberDetails,
    fetchSpec: EbicsFetchSpec
): EbicsDownloadResult {

    // Initialization phase
    val initDownloadRequestStr = if (fetchSpec.isEbics3) {
        if (fetchSpec.ebics3Service == null)
            throw internalServerError("Expected EBICS 3 fetch spec but null was found.")
        createEbicsRequestForDownloadInitialization(
            subscriberDetails,
            fetchSpec.ebics3Service,
            fetchSpec.orderParams
        )
    }
    else {
        if (fetchSpec.orderType == null)
            throw internalServerError("Expected EBICS 2.5 order type but null was found.")
        createEbicsRequestForDownloadInitialization(
            subscriberDetails,
            fetchSpec.orderType,
            fetchSpec.orderParams
        )
    }
    val payloadChunks = LinkedList<String>()
    val initResponseStr = client.postToBank(subscriberDetails.ebicsUrl, initDownloadRequestStr)
    val initResponse = parseAndValidateEbicsResponse(
        subscriberDetails,
        initResponseStr,
        withEbics3 = fetchSpec.isEbics3
    )
    val transactionID: String? = initResponse.transactionID
    // Checking for EBICS communication problems.
    when (initResponse.technicalReturnCode) {
        EbicsReturnCode.EBICS_OK -> {
            /**
             * The EBICS communication succeeded, but business problems
             * may be reported along the 'bank technical' code; this check
             * takes place later.
             */
        }
        else -> {
            // The bank gave a valid XML response but EBICS had problems.
            throw EbicsProtocolError(
                HttpStatusCode.UnprocessableEntity,
                "EBICS-technical error at init phase: " +
                        "${initResponse.technicalReturnCode} ${initResponse.reportText}," +
                        " for fetching level ${fetchSpec.originalLevel} and transaction ID: $transactionID.",
                initResponse.technicalReturnCode
            )
        }
    }
    // Checking the 'bank technical' code.
    when (initResponse.bankReturnCode) {
        EbicsReturnCode.EBICS_OK -> {
            // Success, nothing to do!
        }
        EbicsReturnCode.EBICS_NO_DOWNLOAD_DATA_AVAILABLE -> {
            // The 'pf' dialect might respond this value here (at init phase),
            // in contrast to what the default dialect does (waiting the transfer phase)
            return EbicsDownloadEmptyResult()
        }
        else -> {
            println("Bank raw response: $initResponseStr")
            logger.error(
                "Bank-technical error at init phase: ${initResponse.bankReturnCode}" +
                        ", for fetching level ${fetchSpec.originalLevel} and transaction ID $transactionID."
            )
            return EbicsDownloadBankErrorResult(initResponse.bankReturnCode)
        }
    }
    logger.debug("Bank acknowledges EBICS download initialization." +
            "  Transaction ID: $transactionID.")
    val encryptionInfo = initResponse.dataEncryptionInfo
        ?: throw NexusError(
            HttpStatusCode.BadGateway,
            "Initial response did not contain encryption info.  " +
                    "Fetching level ${fetchSpec.originalLevel} , transaction ID $transactionID"
        )

    val initOrderDataEncChunk = initResponse.orderDataEncChunk
        ?: throw NexusError(
            HttpStatusCode.BadGateway,
            "Initial response for download transaction does not " +
                    "contain data transfer.  Fetching level ${fetchSpec.originalLevel}, " +
                    "transaction ID $transactionID."
        )
    payloadChunks.add(initOrderDataEncChunk)

    val numSegments = initResponse.numSegments
        ?: throw NexusError(
            HttpStatusCode.FailedDependency,
            "Missing segment number in EBICS download init response." +
                    "  Fetching level ${fetchSpec.originalLevel}, transaction ID $transactionID"
        )
    // Transfer phase
    for (x in 2 .. numSegments) {
        val transferReqStr =
            createEbicsRequestForDownloadTransferPhase(
                subscriberDetails,
                transactionID,
                x,
                numSegments,
                fetchSpec.isEbics3
            )
        logger.debug("EBICS download transfer phase of ${transactionID}: sending segment $x")
        val transferResponseStr = client.postToBank(subscriberDetails.ebicsUrl, transferReqStr)
        val transferResponse = parseAndValidateEbicsResponse(subscriberDetails, transferResponseStr)
        when (transferResponse.technicalReturnCode) {
            EbicsReturnCode.EBICS_OK -> {
                // Success, nothing to do!
            }
            else -> {
                throw NexusError(
                    HttpStatusCode.FailedDependency,
                    "EBICS-technical error at transfer phase: " +
                            "${transferResponse.technicalReturnCode} ${transferResponse.reportText}." +
                            "  Fetching level ${fetchSpec.originalLevel}, transaction ID $transactionID"
                )
            }
        }
        when (transferResponse.bankReturnCode) {
            EbicsReturnCode.EBICS_OK -> {
                // Success, nothing to do!
            }
            else -> {
                logger.error("Bank-technical error at transfer phase: " +
                        "${transferResponse.bankReturnCode}." +
                        "  Fetching level ${fetchSpec.originalLevel}, transaction ID $transactionID")
                return EbicsDownloadBankErrorResult(transferResponse.bankReturnCode)
            }
        }
        val transferOrderDataEncChunk = transferResponse.orderDataEncChunk
            ?: throw NexusError(
                HttpStatusCode.BadGateway,
                "transfer response for download transaction " +
                        "does not contain data transfer.  Fetching level ${fetchSpec.originalLevel}, transaction ID $transactionID"
            )
        payloadChunks.add(transferOrderDataEncChunk)
        logger.debug("Download transfer phase of ${transactionID}: bank acknowledges $x")
    }

    val respPayload = decryptAndDecompressResponse(subscriberDetails, encryptionInfo, payloadChunks)

    // Acknowledgement phase
    val ackRequest = createEbicsRequestForDownloadReceipt(
        subscriberDetails,
        transactionID,
        fetchSpec.isEbics3
    )
    val ackResponseStr = client.postToBank(
        subscriberDetails.ebicsUrl,
        ackRequest
    )
    val ackResponse = parseAndValidateEbicsResponse(
        subscriberDetails,
        ackResponseStr,
        withEbics3 = fetchSpec.isEbics3
    )
    when (ackResponse.technicalReturnCode) {
        EbicsReturnCode.EBICS_DOWNLOAD_POSTPROCESS_DONE -> {
        }
        else -> {
            throw NexusError(
                HttpStatusCode.InternalServerError,
                "Unexpected EBICS return code" +
                        " at acknowledgement phase: ${ackResponse.technicalReturnCode.name}." +
                        "  Fetching level ${fetchSpec.originalLevel}, transaction ID $transactionID"
            )
        }
    }
    logger.debug("Bank acknowledges EBICS download receipt.  Transaction ID: $transactionID.")
    return EbicsDownloadSuccessResult(respPayload, transactionID)
}

// Currently only 1-segment requests.
suspend fun doEbicsUploadTransaction(
    client: HttpClient,
    subscriberDetails: EbicsClientSubscriberDetails,
    uploadSpec: EbicsUploadSpec,
    payload: ByteArray
) {
    if (subscriberDetails.bankEncPub == null) {
        throw NexusError(HttpStatusCode.BadRequest,
            "bank encryption key unknown, request HPB first"
        )
    }
    val preparedUploadData = prepareUploadPayload(
        subscriberDetails,
        payload,
        isEbics3 = uploadSpec.isEbics3
    )
    val req: String = if (uploadSpec.isEbics3) {
        if (uploadSpec.ebics3Service == null)
            throw internalServerError("EBICS 3 service data was expected, but null was found.")
        createEbicsRequestForUploadInitialization(
            subscriberDetails,
            uploadSpec.ebics3Service,
            uploadSpec.orderParams,
            preparedUploadData
        )
    } else {
        if (uploadSpec.orderType == null)
            throw internalServerError("EBICS 2.5 order type was expected, but null was found.")
        createEbicsRequestForUploadInitialization(
            subscriberDetails,
            uploadSpec.orderType,
            uploadSpec.orderParams ?: EbicsStandardOrderParams(),
            preparedUploadData
        )
    }
    logger.debug("EBICS upload message to: ${subscriberDetails.ebicsUrl}")
    val responseStr = client.postToBank(subscriberDetails.ebicsUrl, req)

    val initResponse = parseAndValidateEbicsResponse(
        subscriberDetails,
        responseStr,
        withEbics3 = uploadSpec.isEbics3
    )
    // The bank indicated one error, hence Nexus sent invalid data.
    if (initResponse.technicalReturnCode != EbicsReturnCode.EBICS_OK) {
        throw NexusError(
            HttpStatusCode.InternalServerError,
            reason = "EBICS-technical error at init phase:" +
                    " ${initResponse.technicalReturnCode} ${initResponse.reportText}"
        )
    }
    // The bank did NOT indicate any error, but the response
    // lacks required information, blame the bank.
    val transactionID = initResponse.transactionID
    if (initResponse.bankReturnCode != EbicsReturnCode.EBICS_OK) {
        throw NexusError(
            HttpStatusCode.InternalServerError,
            reason = "Bank-technical error at init phase:" +
                    " ${initResponse.bankReturnCode}"
        )
    }
    logger.debug("Bank acknowledges EBICS upload initialization. " +
            " Transaction ID: $transactionID.")

    /* now send actual payload */
    val ebicsPayload = createEbicsRequestForUploadTransferPhase(
        subscriberDetails,
        transactionID,
        preparedUploadData,
        0,
        withEbics3 = uploadSpec.isEbics3
    )
    val txRespStr = client.postToBank(
        subscriberDetails.ebicsUrl,
        ebicsPayload
    )
    val txResp = parseAndValidateEbicsResponse(
        subscriberDetails,
        txRespStr,
        withEbics3 = uploadSpec.isEbics3
    )
    when (txResp.technicalReturnCode) {
        EbicsReturnCode.EBICS_OK -> {/* do nothing */}
        else -> {
            // EBICS failed, blame Nexus.
            throw EbicsProtocolError(
                httpStatusCode = HttpStatusCode.InternalServerError,
                reason = txResp.reportText,
                ebicsTechnicalCode = txResp.technicalReturnCode
            )
        }
    }
    when (txResp.bankReturnCode) {
        EbicsReturnCode.EBICS_OK -> {/* do nothing */}
        else -> {
            /**
             * Although EBICS went fine, the bank complained about
             * the communication content.
             */
            throw EbicsProtocolError(
                httpStatusCode = HttpStatusCode.UnprocessableEntity,
                reason = "bank-technical error: ${txResp.reportText}"
            )
        }
    }
    logger.debug("Bank acknowledges EBICS upload transfer.  Transaction ID: $transactionID")
}

suspend fun doEbicsHostVersionQuery(client: HttpClient, ebicsBaseUrl: String, ebicsHostId: String): EbicsHevDetails {
    val ebicsHevRequest = makeEbicsHEVRequestRaw(ebicsHostId)
    val resp = client.postToBank(ebicsBaseUrl, ebicsHevRequest)
    return parseEbicsHEVResponse(resp)
}

suspend fun doEbicsIniRequest(
    client: HttpClient,
    subscriberDetails: EbicsClientSubscriberDetails
): EbicsKeyManagementResponseContent {
    val request = makeEbicsIniRequest(subscriberDetails)
    val respStr = client.postToBank(
        subscriberDetails.ebicsUrl,
        request
    )
    return parseAndDecryptEbicsKeyManagementResponse(subscriberDetails, respStr)
}

suspend fun doEbicsHiaRequest(
    client: HttpClient,
    subscriberDetails: EbicsClientSubscriberDetails
): EbicsKeyManagementResponseContent {
    val request = makeEbicsHiaRequest(subscriberDetails)
    val respStr = client.postToBank(
        subscriberDetails.ebicsUrl,
        request
    )
    return parseAndDecryptEbicsKeyManagementResponse(subscriberDetails, respStr)
}


suspend fun doEbicsHpbRequest(
    client: HttpClient,
    subscriberDetails: EbicsClientSubscriberDetails
): HpbResponseData {
    val request = makeEbicsHpbRequest(subscriberDetails)
    val respStr = client.postToBank(
        subscriberDetails.ebicsUrl,
        request
    )
    val parsedResponse = parseAndDecryptEbicsKeyManagementResponse(subscriberDetails, respStr)
    val orderData = parsedResponse.orderData ?: throw EbicsProtocolError(
        HttpStatusCode.BadGateway,
        "Cannot find data in a HPB response"
    )
    return parseEbicsHpbOrder(orderData)
}