Skip to content

Commit

Permalink
Extract new interfaces, classes for chains into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
nodh committed Apr 1, 2022
1 parent 13ad22f commit 7aa4427
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 83 deletions.
31 changes: 0 additions & 31 deletions src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/Chain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -47,7 +53,6 @@ object DefaultChain {
if (atRepository == null)
return euChain


val atContextService = DefaultContextIdentifierService("AT1:")
val atChain = Chain(
DefaultHigherOrderValidationService(),
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
)
}
}
}
}
37 changes: 37 additions & 0 deletions src/commonMain/kotlin/ehn/techiop/hcert/kotlin/chain/IChain.kt
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 7aa4427

Please sign in to comment.