Skip to content

Commit

Permalink
- new API method to get payload from cbor; (eu-digital-green-certific…
Browse files Browse the repository at this point in the history
  • Loading branch information
MykhailoNester authored Sep 6, 2021
1 parent ebd5b90 commit aa213a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ interface CborService {
input: ByteArray,
verificationResult: VerificationResult
): GreenCertificateData?

fun getPayload(input: ByteArray): ByteArray?
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ import java.time.ZoneOffset
/**
* Decodes input as a CBOR structure
*/
class DefaultCborService(private val greenCertificateMapper: GreenCertificateMapper = DefaultGreenCertificateMapper()) : CborService {
class DefaultCborService(private val greenCertificateMapper: GreenCertificateMapper = DefaultGreenCertificateMapper()) :
CborService {

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

override fun decodeData(
input: ByteArray,
verificationResult: VerificationResult
input: ByteArray,
verificationResult: VerificationResult
): GreenCertificateData? {
verificationResult.cborDecoded = false
return try {
Expand All @@ -60,10 +61,26 @@ class DefaultCborService(private val greenCertificateMapper: GreenCertificateMap
val cborObject = hcert[CBORObject.FromObject(1)]

val greenCertificate: GreenCertificate = greenCertificateMapper.readValue(cborObject)
.also { verificationResult.cborDecoded = true }
GreenCertificateData(issuingCountry, cborObject.ToJSONString(), greenCertificate, issuedAt.atZone(ZoneOffset.UTC), expirationTime.atZone(ZoneOffset.UTC))
.also { verificationResult.cborDecoded = true }
GreenCertificateData(
issuingCountry,
cborObject.ToJSONString(),
greenCertificate,
issuedAt.atZone(ZoneOffset.UTC),
expirationTime.atZone(ZoneOffset.UTC)
)
} catch (e: Throwable) {
null
}
}

override fun getPayload(input: ByteArray): ByteArray? {
return try {
val map = CBORObject.DecodeFromBytes(input)
val hcert = map[CwtHeaderKeys.HCERT.asCBOR()]
hcert[CBORObject.FromObject(1)].EncodeToBytes()
} catch (ex: Exception) {
null
}
}
}

0 comments on commit aa213a5

Please sign in to comment.