Skip to content

Commit

Permalink
Merge pull request #917 from samarthgr/DIV-7858
Browse files Browse the repository at this point in the history
Div 7858 | adding support to download QRCode with Smart Health Card as png
  • Loading branch information
dileepbapat authored Jan 18, 2022
2 parents 4a3fb81 + 9d0172c commit b50e301
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion backend/certificate_api/configs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const KAFKA_BOOTSTRAP_SERVER = process.env.KAFKA_BOOTSTRAP_SERVERS || 'localhost
const DISEASE_CODE = process.env.DISEASE_CODE || 'COVID-19';
const PUBLIC_HEALTH_AUTHORITY = process.env.FHIR_PUBLIC_HEALTH_AUTHORITY || 'Govt Of India';
const EU_CERTIFICATE_EXPIRY = parseInt(process.env.EU_CERTIFICATE_EXPIRY) || 12;
const CERTIFICATE_ISSUER = process.env.CERTIFICATE_ISSUER || "https://divoc.dev/";
const CERTIFICATE_ISSUER = process.env.CERTIFICATE_ISSUER || "https://divoc.dev";
module.exports = {
REGISTRY_URL,
JWT_PUBLIC_KEY,
Expand Down
14 changes: 7 additions & 7 deletions backend/certificate_api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/certificate_api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@pathcheck/dcc-sdk": "^0.0.22",
"@pathcheck/shc-sdk": "^0.0.6",
"axios": "^0.21.1",
"certificate-fhir-convertor": "^1.3.4",
"certificate-fhir-convertor": "^1.3.5",
"handlebars": "^4.7.7",
"i18n-iso-countries": "6.8.0",
"jsonwebtoken": "^8.5.1",
Expand Down
22 changes: 15 additions & 7 deletions backend/certificate_api/src/routes/certificate_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,13 @@ async function certificateAsEUPayload(req, res) {

async function certificateAsSHCPayload(req, res) {
let refId;
let type;
try {
var queryData = url.parse(req.url, true).query;
try {
await verifyKeycloakToken(req.headers.authorization);
refId = queryData.refId;
type = queryData.type;
} catch (e) {
console.error(e);
res.statusCode = 403;
Expand All @@ -558,19 +560,25 @@ async function certificateAsSHCPayload(req, res) {
let certificate = JSON.parse(certificateRaw.certificate);

// convert certificate to SHC Json
const dccPayload = fhirCertificate.certificateToSmartHealthJson(certificate, {});
const shcPayload = fhirCertificate.certificateToSmartHealthJson(certificate, {});

// get keyPair from pem for 1st time
if (shcKeyPair.length === 0) {
const keyFile = keyUtils.PEMtoDER(shcPrivateKeyPem)
shcKeyPair = await keyUtils.DERtoJWK(keyFile, []);
}

const qrUri = await shc.signAndPack(await shc.makeJWT(dccPayload, config.EU_CERTIFICATE_EXPIRY, config.CERTIFICATE_ISSUER), shcKeyPair[0]);
const dataURL = await QRCode.toDataURL(qrUri, {scale: 2});
let doseToVaccinationDetailsMap = getVaccineDetailsOfPreviousDoses(certificateResp);
const certificateData = prepareDataForVaccineCertificateTemplate(certificateRaw, dataURL, doseToVaccinationDetailsMap);
const pdfBuffer = await createPDF(vaccineCertificateTemplateFilePath, certificateData);
const qrUri = await shc.signAndPack(await shc.makeJWT(shcPayload, config.EU_CERTIFICATE_EXPIRY, config.CERTIFICATE_ISSUER, new Date()), shcKeyPair[0]);

let buffer ;
if (type && type.toLowerCase() === "qrcode") {
buffer = await QRCode.toBuffer(qrUri, {scale: 2})
} else {
const dataURL = await QRCode.toDataURL(qrUri, {scale: 2});
let doseToVaccinationDetailsMap = getVaccineDetailsOfPreviousDoses(certificateResp);
const certificateData = prepareDataForVaccineCertificateTemplate(certificateRaw, dataURL, doseToVaccinationDetailsMap);
buffer = await createPDF(vaccineCertificateTemplateFilePath, certificateData);
}

res.statusCode = 200;
sendEvents({
Expand All @@ -579,7 +587,7 @@ async function certificateAsSHCPayload(req, res) {
type: "shc-cert-success",
extra: "Certificate found"
});
return pdfBuffer
return buffer

} else {
res.statusCode = 404;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "collection",
"entry": [
{
"fullUrl": "urn:uuid:{{patientId}}",
"fullUrl": "resource:0",
"resource": {
"resourceType": "Patient",
"name": [
Expand All @@ -24,7 +24,7 @@
}
},
{
"fullUrl": "urn:uuid:{{immunizationId}}",
"fullUrl": "resource:1",
"resource": {
"resourceType": "Immunization",
"status": "completed",
Expand All @@ -37,7 +37,7 @@
]
},
"patient": {
"reference": "urn:uuid:{{patientId}}"
"reference": "resource:0"
},
"occurrenceDateTime": "{{vaccinationDate}}",
"performer": [
Expand Down
2 changes: 1 addition & 1 deletion certificate-fhir-convertor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "certificate-fhir-convertor",
"version": "1.3.4",
"version": "1.3.5",
"description": "Vaccination context for verifiable certificate",
"main": "fhir-convertor.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions certificate-fhir-convertor/tests/fhir-convertor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ test('should convert W3C certificate json to Smart Health Card json', async () =
let shcCert = await certificateToSmartHealthJson(cert2, meta);
console.log(JSON.stringify(shcCert));

expect(shcCert.credentialSubject.fhirBundle.entry[0].fullUrl).toContain('urn:uuid:');
expect(shcCert.credentialSubject.fhirBundle.entry[0].fullUrl).toContain('resource:0');
expect(shcCert.credentialSubject.fhirBundle.entry[0].resource.resourceType).toBe('Patient');
expect(shcCert.credentialSubject.fhirBundle.entry[0].resource.name[0].text).toBe(cert2.credentialSubject.name);

expect(shcCert.credentialSubject.fhirBundle.entry[1].fullUrl).toContain('urn:uuid:');
expect(shcCert.credentialSubject.fhirBundle.entry[1].fullUrl).toContain('resource:1');
expect(shcCert.credentialSubject.fhirBundle.entry[1].resource.resourceType).toBe('Immunization');
expect(shcCert.credentialSubject.fhirBundle.entry[1].resource.occurrenceDateTime).toBe(cert2.evidence[0].date);
expect(shcCert.credentialSubject.fhirBundle.entry[1].resource.lotNumber).toBe(cert2.evidence[0].batch);
Expand Down

0 comments on commit b50e301

Please sign in to comment.