Skip to content

Commit

Permalink
Feature/certlogic engine dependency (eu-digital-green-certificates#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandrsarapulovgl authored Jun 23, 2021
1 parent daf0340 commit c6637c8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ package dgca.verifier.app.decoder.cbor

import dgca.verifier.app.decoder.model.GreenCertificate
import dgca.verifier.app.decoder.model.VerificationResult
import java.time.ZonedDateTime

data class GreenCertificateData(
val hcertJson: String,
val greenCertificate: GreenCertificate,
val issuedAt: ZonedDateTime,
val expirationTime: ZonedDateTime
)

/**
* Decodes input as a CBOR structure
*/
interface CborService {

fun decode(input: ByteArray, verificationResult: VerificationResult): GreenCertificate?

fun decodeData(
input: ByteArray,
verificationResult: VerificationResult
): GreenCertificateData?
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,24 @@ import dgca.verifier.app.decoder.cwt.CwtHeaderKeys
import dgca.verifier.app.decoder.model.GreenCertificate
import dgca.verifier.app.decoder.model.VerificationResult
import java.time.Instant
import java.time.ZoneOffset

/**
* Decodes input as a CBOR structure
*/
class DefaultCborService : CborService {

override fun decode(input: ByteArray, verificationResult: VerificationResult): GreenCertificate? {
override fun decode(
input: ByteArray,
verificationResult: VerificationResult
): GreenCertificate? = decodeData(input, verificationResult)?.greenCertificate

override fun decodeData(
input: ByteArray,
verificationResult: VerificationResult
): GreenCertificateData? {
verificationResult.cborDecoded = false
try {
return try {
val map = CBORObject.DecodeFromBytes(input)

val issuedAt = Instant.ofEpochSecond(map[CwtHeaderKeys.ISSUED_AT.asCBOR()].AsInt64())
Expand All @@ -46,14 +55,16 @@ class DefaultCborService : CborService {
verificationResult.isNotExpired = expirationTime.isAfter(Instant.now())

val hcert = map[CwtHeaderKeys.HCERT.asCBOR()]
val hcertv1 = hcert[CBORObject.FromObject(1)].EncodeToBytes()

return CBORMapper()
val cborObject = hcert[CBORObject.FromObject(1)]
val hcertv1 = cborObject.EncodeToBytes()

val greenCertificate: GreenCertificate = CBORMapper()
.readValue(hcertv1, GreenCertificate::class.java)
.also { verificationResult.cborDecoded = true }

GreenCertificateData(cborObject.toString(), greenCertificate, issuedAt.atZone(ZoneOffset.UTC), expirationTime.atZone(ZoneOffset.UTC))
} catch (e: Throwable) {
return null
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package dgca.verifier.app.decoder.model

import com.fasterxml.jackson.annotation.JsonProperty
import java.io.Serializable
import java.util.*

/**
* CBOR structure of the certificate
Expand Down Expand Up @@ -62,4 +63,15 @@ data class GreenCertificate(
""
}
}

fun getIssuingCountry(): String = try {
when {
vaccinations?.isNotEmpty() == true -> vaccinations.last().countryOfVaccination
tests?.isNotEmpty() == true -> tests.last().countryOfVaccination
recoveryStatements?.isNotEmpty() == true -> recoveryStatements.last().countryOfVaccination
else -> ""
}
} catch (ex: Exception) {
""
}.toLowerCase(Locale.ROOT)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ data class VerificationResult(
var isSchemaValid: Boolean = false,
var isIssuedTimeCorrect: Boolean = false,
var isNotExpired: Boolean = false,
var rulesValidationFailed: Boolean = false,
var testVerification: TestVerificationResult? = null
) {

fun isValid(): Boolean {
val isTestValid = testVerification?.isTestValid() ?: true
return base45Decoded && zlibDecoded && coseVerified && cborDecoded && isSchemaValid && isTestValid &&
isIssuedTimeCorrect && isNotExpired
isIssuedTimeCorrect && isNotExpired && !rulesValidationFailed
}

/**
Expand Down

0 comments on commit c6637c8

Please sign in to comment.