diff options
Diffstat (limited to 'nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt')
-rw-r--r-- | nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt | 94 |
1 files changed, 34 insertions, 60 deletions
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt index e690dab8..916014e7 100644 --- a/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt +++ b/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt @@ -92,43 +92,6 @@ private fun askUserToAcceptKeys(bankKeys: BankPublicKeysFile): Boolean { } /** - * Parses the HPB response and stores the bank keys as "NOT accepted" to disk. - * - * @param cfg used to get the location of the bank keys file. - * @param bankKeys bank response to the HPB message. - */ -private fun handleHpbResponse( - cfg: EbicsSetupConfig, - bankKeys: EbicsKeyManagementResponseContent -) { - val hpbBytes = bankKeys.orderData // silences compiler. - if (hpbBytes == null) { - throw Exception("HPB content not found in a EBICS response with successful return codes.") - } - val hpbObj = try { - parseEbicsHpbOrder(hpbBytes.inputStream()) - } catch (e: Exception) { - throw Exception("HPB response content seems invalid", e) - } - val encPub = try { - CryptoUtil.loadRsaPublicKey(hpbObj.encryptionPubKey.encoded) - } catch (e: Exception) { - throw Exception("Could not import bank encryption key from HPB response", e) - } - val authPub = try { - CryptoUtil.loadRsaPublicKey(hpbObj.authenticationPubKey.encoded) - } catch (e: Exception) { - throw Exception("Could not import bank authentication key from HPB response", e) - } - val json = BankPublicKeysFile( - bank_authentication_public_key = authPub, - bank_encryption_public_key = encPub, - accepted = false - ) - persistBankKeys(json, cfg.bankPublicKeysFilename) -} - -/** * Collects all the steps from generating the message, to * sending it to the bank, and finally updating the state * on disk according to the response. @@ -147,34 +110,45 @@ suspend fun doKeysRequestAndUpdateState( orderType: KeysOrderType ) { logger.info("Doing key request ${orderType.name}") + val impl = Ebics3KeyMng(cfg, privs) val req = when(orderType) { - KeysOrderType.INI -> generateIniMessage(cfg, privs) - KeysOrderType.HIA -> generateHiaMessage(cfg, privs) - KeysOrderType.HPB -> generateHpbMessage(cfg, privs) + KeysOrderType.INI -> impl.INI() + KeysOrderType.HIA -> impl.HIA() + KeysOrderType.HPB -> impl.HPB() } - val xml = try { - client.postToBank(cfg.hostBaseUrl, req) - } catch (e: Exception) { - throw Exception("Could not POST the ${orderType.name} message to the bank at '${cfg.hostBaseUrl}'", e) - } - val ebics = parseKeysMgmtResponse(privs.encryption_private_key, xml) - if (ebics.technicalReturnCode != EbicsReturnCode.EBICS_OK) { - throw Exception("EBICS ${orderType.name} failed with code: ${ebics.technicalReturnCode}") - } - if (ebics.bankReturnCode != EbicsReturnCode.EBICS_OK) { - throw Exception("EBICS ${orderType.name} reached the bank, but could not be fulfilled, error code: ${ebics.bankReturnCode}") - } - + val xml = client.postToBank(cfg.hostBaseUrl, req) + val resp = Ebics3KeyMng.parseResponse(xml, privs.encryption_private_key) + // TODO better error messages for expected errros + + val orderData = resp.okOrFail("${orderType.name}") when (orderType) { KeysOrderType.INI -> privs.submitted_ini = true KeysOrderType.HIA -> privs.submitted_hia = true - KeysOrderType.HPB -> return handleHpbResponse(cfg, ebics) + KeysOrderType.HPB -> { + val orderData = requireNotNull(orderData) { + "HPB: missing order data" + } + val (authPub, encPub) = Ebics3KeyMng.parseHpbOrder(orderData) + val bankKeys = BankPublicKeysFile( + bank_authentication_public_key = authPub, + bank_encryption_public_key = encPub, + accepted = false + ) + try { + persistBankKeys(bankKeys, cfg.bankPublicKeysFilename) + } catch (e: Exception) { + throw Exception("Could not update the ${orderType.name} state on disk", e) + } + } } - try { - persistClientKeys(privs, cfg.clientPrivateKeysFilename) - } catch (e: Exception) { - throw Exception("Could not update the ${orderType.name} state on disk", e) + if (orderType != KeysOrderType.HPB) { + try { + persistClientKeys(privs, cfg.clientPrivateKeysFilename) + } catch (e: Exception) { + throw Exception("Could not update the ${orderType.name} state on disk", e) + } } + } /** @@ -266,12 +240,12 @@ class EbicsSetup: CliktCommand("Set up the EBICS subscriber") { else bankKeys.accepted = askUserToAcceptKeys(bankKeys) if (!bankKeys.accepted) { - throw Exception("Cannot successfully finish the setup without accepting the bank keys.") + throw Exception("Cannot successfully finish the setup without accepting the bank keys") } try { persistBankKeys(bankKeys, cfg.bankPublicKeysFilename) } catch (e: Exception) { - throw Exception("Could not set bank keys as accepted on disk.", e) + throw Exception("Could not set bank keys as accepted on disk", e) } } |