Skip to content

Commit aa213a5

Browse files
- new API method to get payload from cbor; (#62)
1 parent ebd5b90 commit aa213a5

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

decoder/src/main/java/dgca/verifier/app/decoder/cbor/CborService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ interface CborService {
5151
input: ByteArray,
5252
verificationResult: VerificationResult
5353
): GreenCertificateData?
54+
55+
fun getPayload(input: ByteArray): ByteArray?
5456
}

decoder/src/main/java/dgca/verifier/app/decoder/cbor/DefaultCborService.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ import java.time.ZoneOffset
3232
/**
3333
* Decodes input as a CBOR structure
3434
*/
35-
class DefaultCborService(private val greenCertificateMapper: GreenCertificateMapper = DefaultGreenCertificateMapper()) : CborService {
35+
class DefaultCborService(private val greenCertificateMapper: GreenCertificateMapper = DefaultGreenCertificateMapper()) :
36+
CborService {
3637

3738
override fun decode(
38-
input: ByteArray,
39-
verificationResult: VerificationResult
39+
input: ByteArray,
40+
verificationResult: VerificationResult
4041
): GreenCertificate? = decodeData(input, verificationResult)?.greenCertificate
4142

4243
override fun decodeData(
43-
input: ByteArray,
44-
verificationResult: VerificationResult
44+
input: ByteArray,
45+
verificationResult: VerificationResult
4546
): GreenCertificateData? {
4647
verificationResult.cborDecoded = false
4748
return try {
@@ -60,10 +61,26 @@ class DefaultCborService(private val greenCertificateMapper: GreenCertificateMap
6061
val cborObject = hcert[CBORObject.FromObject(1)]
6162

6263
val greenCertificate: GreenCertificate = greenCertificateMapper.readValue(cborObject)
63-
.also { verificationResult.cborDecoded = true }
64-
GreenCertificateData(issuingCountry, cborObject.ToJSONString(), greenCertificate, issuedAt.atZone(ZoneOffset.UTC), expirationTime.atZone(ZoneOffset.UTC))
64+
.also { verificationResult.cborDecoded = true }
65+
GreenCertificateData(
66+
issuingCountry,
67+
cborObject.ToJSONString(),
68+
greenCertificate,
69+
issuedAt.atZone(ZoneOffset.UTC),
70+
expirationTime.atZone(ZoneOffset.UTC)
71+
)
6572
} catch (e: Throwable) {
6673
null
6774
}
6875
}
76+
77+
override fun getPayload(input: ByteArray): ByteArray? {
78+
return try {
79+
val map = CBORObject.DecodeFromBytes(input)
80+
val hcert = map[CwtHeaderKeys.HCERT.asCBOR()]
81+
hcert[CBORObject.FromObject(1)].EncodeToBytes()
82+
} catch (ex: Exception) {
83+
null
84+
}
85+
}
6986
}

0 commit comments

Comments
 (0)