diff --git a/decoder/src/main/java/dgca/verifier/app/decoder/cbor/CborService.kt b/decoder/src/main/java/dgca/verifier/app/decoder/cbor/CborService.kt index 1221936..076fa97 100644 --- a/decoder/src/main/java/dgca/verifier/app/decoder/cbor/CborService.kt +++ b/decoder/src/main/java/dgca/verifier/app/decoder/cbor/CborService.kt @@ -24,6 +24,14 @@ 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 @@ -31,4 +39,9 @@ import dgca.verifier.app.decoder.model.VerificationResult interface CborService { fun decode(input: ByteArray, verificationResult: VerificationResult): GreenCertificate? + + fun decodeData( + input: ByteArray, + verificationResult: VerificationResult + ): GreenCertificateData? } \ No newline at end of file diff --git a/decoder/src/main/java/dgca/verifier/app/decoder/cbor/DefaultCborService.kt b/decoder/src/main/java/dgca/verifier/app/decoder/cbor/DefaultCborService.kt index 5ab73da..0ec4c64 100644 --- a/decoder/src/main/java/dgca/verifier/app/decoder/cbor/DefaultCborService.kt +++ b/decoder/src/main/java/dgca/verifier/app/decoder/cbor/DefaultCborService.kt @@ -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()) @@ -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 } } } \ No newline at end of file diff --git a/decoder/src/main/java/dgca/verifier/app/decoder/model/GreenCertificate.kt b/decoder/src/main/java/dgca/verifier/app/decoder/model/GreenCertificate.kt index ad9abed..a6b0425 100644 --- a/decoder/src/main/java/dgca/verifier/app/decoder/model/GreenCertificate.kt +++ b/decoder/src/main/java/dgca/verifier/app/decoder/model/GreenCertificate.kt @@ -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 @@ -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) } \ No newline at end of file diff --git a/decoder/src/main/java/dgca/verifier/app/decoder/model/VerificationResult.kt b/decoder/src/main/java/dgca/verifier/app/decoder/model/VerificationResult.kt index a941466..de2ef47 100644 --- a/decoder/src/main/java/dgca/verifier/app/decoder/model/VerificationResult.kt +++ b/decoder/src/main/java/dgca/verifier/app/decoder/model/VerificationResult.kt @@ -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 } /**