From 7aa442717cadee8929a010b7322bd6adb64689a7 Mon Sep 17 00:00:00 2001 From: Christian Kollmann Date: Fri, 1 Apr 2022 15:39:00 +0200 Subject: [PATCH] Extract new interfaces, classes for chains into separate files --- .../ehn/techiop/hcert/kotlin/chain/Chain.kt | 31 ---------------- .../hcert/kotlin/chain/DefaultChain.kt | 35 +++++------------- .../hcert/kotlin/chain/DelegatingChain.kt | 36 ++++++++++++++++++ .../ehn/techiop/hcert/kotlin/chain/IChain.kt | 37 +++++++++++++++++++ .../hcert/kotlin/chain/debug/DebugChain.kt | 33 ++++------------- 5 files changed, 89 insertions(+), 83 deletions(-) create mode 100644 src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DelegatingChain.kt create mode 100644 src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/IChain.kt diff --git a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/Chain.kt b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/Chain.kt index 65944d7e..c1f7e5a3 100644 --- a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/Chain.kt +++ b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/Chain.kt @@ -5,37 +5,6 @@ import ehn.techiop.hcert.kotlin.data.GreenCertificate import ehn.techiop.hcert.kotlin.log.globalLogLevel import ehn.techiop.hcert.kotlin.trust.CwtHelper import io.github.aakira.napier.Napier -import kotlin.js.JsName - -interface IChain { - /** - * Process the [input], apply encoding in this order: - * - [CborService] - * - [CwtService] - * - [CoseService] - * - [CompressorService] - * - [Base45Service] - * - [ContextIdentifierService] - * - * The result ([ChainResult]) will contain all intermediate steps, as well as the final result in [ChainResult.step5Prefixed]. - */ - @JsName("encode") - fun encode(input: GreenCertificate): ChainResult - - /** - * Process the [input], apply decoding in this order: - * - [ContextIdentifierService] - * - [Base45Service] - * - [CompressorService] - * - [CoseService] - * - [CwtService] - * - [CborService] - * - [SchemaValidationService] - * The result ([ChainDecodeResult]) will contain the parsed data, as well as intermediate results. - */ - @JsName("decode") - fun decode(input: String): DecodeResult -} /** * Main entry point for the creation/encoding and verification/decoding of HCERT data into QR codes diff --git a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DefaultChain.kt b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DefaultChain.kt index fb5df7c0..f9e7874c 100644 --- a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DefaultChain.kt +++ b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DefaultChain.kt @@ -1,7 +1,13 @@ package ehn.techiop.hcert.kotlin.chain -import ehn.techiop.hcert.kotlin.chain.impl.* -import ehn.techiop.hcert.kotlin.data.GreenCertificate +import ehn.techiop.hcert.kotlin.chain.impl.DefaultBase45Service +import ehn.techiop.hcert.kotlin.chain.impl.DefaultCborService +import ehn.techiop.hcert.kotlin.chain.impl.DefaultCompressorService +import ehn.techiop.hcert.kotlin.chain.impl.DefaultContextIdentifierService +import ehn.techiop.hcert.kotlin.chain.impl.DefaultCoseService +import ehn.techiop.hcert.kotlin.chain.impl.DefaultCwtService +import ehn.techiop.hcert.kotlin.chain.impl.DefaultHigherOrderValidationService +import ehn.techiop.hcert.kotlin.chain.impl.DefaultSchemaValidationService import kotlinx.datetime.Clock import kotlin.js.JsName import kotlin.jvm.JvmOverloads @@ -47,7 +53,6 @@ object DefaultChain { if (atRepository == null) return euChain - val atContextService = DefaultContextIdentifierService("AT1:") val atChain = Chain( DefaultHigherOrderValidationService(), @@ -60,28 +65,6 @@ object DefaultChain { atContextService ) - return object : IChain { - override fun encode(input: GreenCertificate): ChainResult { - return euChain.encode(input) - } - - override fun decode(input: String): DecodeResult { - val check = VerificationResult() - return try { - euContextService.decode(input, check) - euChain.decode(input) - } catch (_: VerificationException) { - try { - atContextService.decode(input, check) - atChain.decode(input) - } catch (e: VerificationException) { - DecodeResult( - VerificationResult().apply { error = e.error;e.details?.let { errorDetails.putAll(it) } }, - ChainDecodeResult(listOf(e.error), null, null, null, null, null, null) - ) - } - } - } - } + return DelegatingChain(euChain, euContextService, atChain, atContextService) } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DelegatingChain.kt b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DelegatingChain.kt new file mode 100644 index 00000000..f6580e21 --- /dev/null +++ b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/DelegatingChain.kt @@ -0,0 +1,36 @@ +package ehn.techiop.hcert.kotlin.chain + +import ehn.techiop.hcert.kotlin.data.GreenCertificate + +/** + * Can decode incoming HCERT data depending on Context, e.g. either "HC1:" or "AT1:" + */ +class DelegatingChain( + private val euChain: Chain, + private val euContextService: ContextIdentifierService, + private val atChain: Chain, + private val atContextService: ContextIdentifierService +) : IChain { + + override fun encode(input: GreenCertificate): ChainResult { + return euChain.encode(input) + } + + override fun decode(input: String): DecodeResult { + val check = VerificationResult() + return try { + euContextService.decode(input, check) + euChain.decode(input) + } catch (_: VerificationException) { + try { + atContextService.decode(input, check) + atChain.decode(input) + } catch (e: VerificationException) { + DecodeResult( + VerificationResult().apply { error = e.error;e.details?.let { errorDetails.putAll(it) } }, + ChainDecodeResult(listOf(e.error), null, null, null, null, null, null) + ) + } + } + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/IChain.kt b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/IChain.kt new file mode 100644 index 00000000..e1c9064b --- /dev/null +++ b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/IChain.kt @@ -0,0 +1,37 @@ +package ehn.techiop.hcert.kotlin.chain + +import ehn.techiop.hcert.kotlin.data.GreenCertificate +import kotlin.js.JsName + +/** + * Interface for the encoding / decoding chain + */ +interface IChain { + /** + * Process the [input], apply encoding in this order: + * - [CborService] + * - [CwtService] + * - [CoseService] + * - [CompressorService] + * - [Base45Service] + * - [ContextIdentifierService] + * + * The result ([ChainResult]) will contain all intermediate steps, as well as the final result in [ChainResult.step5Prefixed]. + */ + @JsName("encode") + fun encode(input: GreenCertificate): ChainResult + + /** + * Process the [input], apply decoding in this order: + * - [ContextIdentifierService] + * - [Base45Service] + * - [CompressorService] + * - [CoseService] + * - [CwtService] + * - [CborService] + * - [SchemaValidationService] + * The result ([ChainDecodeResult]) will contain the parsed data, as well as intermediate results. + */ + @JsName("decode") + fun decode(input: String): DecodeResult +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/debug/DebugChain.kt b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/debug/DebugChain.kt index 0c7e198e..f52a738c 100644 --- a/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/debug/DebugChain.kt +++ b/src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/debug/DebugChain.kt @@ -1,10 +1,12 @@ package ehn.techiop.hcert.kotlin.chain.debug -import ehn.techiop.hcert.kotlin.chain.* +import ehn.techiop.hcert.kotlin.chain.CertificateRepository +import ehn.techiop.hcert.kotlin.chain.Chain +import ehn.techiop.hcert.kotlin.chain.DelegatingChain +import ehn.techiop.hcert.kotlin.chain.IChain import ehn.techiop.hcert.kotlin.chain.impl.DefaultBase45Service import ehn.techiop.hcert.kotlin.chain.impl.DefaultCborService import ehn.techiop.hcert.kotlin.chain.impl.DefaultCompressorService -import ehn.techiop.hcert.kotlin.data.GreenCertificate import kotlinx.datetime.Clock import kotlin.js.JsName import kotlin.jvm.JvmOverloads @@ -50,28 +52,7 @@ object DebugChain { atContextService ) - return object : IChain { - override fun encode(input: GreenCertificate): ChainResult { - return euChain.encode(input) - } - - override fun decode(input: String): DecodeResult { - val check = VerificationResult() - return try { - euContextService.decode(input, check) - euChain.decode(input) - } catch (_: VerificationException) { - try { - atContextService.decode(input, check) - atChain.decode(input) - } catch (e: VerificationException) { - DecodeResult( - VerificationResult().apply { error = e.error;e.details?.let { errorDetails.putAll(it) } }, - ChainDecodeResult(listOf(e.error), null, null, null, null, null, null) - ) - } - } - } - } + return DelegatingChain(euChain, euContextService, atChain, atContextService) } -} \ No newline at end of file +} +