KHealthCertificate

divocparser.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
3  * SPDX-License-Identifier: LGPL-2.0-or-later
4  */
5 
6 #include "divocparser_p.h"
7 #include "jwsverifier_p.h"
8 #include "kvaccinationcertificate.h"
9 
10 #include <QByteArray>
11 #include <QDebug>
12 #include <QJsonArray>
13 #include <QJsonDocument>
14 #include <QJsonObject>
15 #include <QVariant>
16 
17 QVariant DivocParser::parse(const QByteArray &data)
18 {
20  const auto doc = QJsonDocument::fromJson(data, &error);
21  if (error.error != QJsonParseError::NoError) {
22  return {};
23  }
24 
25  // TODO check this is actually a VerifiableCredential structure
26  // TODO check the types on the subobjects we use
27 
29  const auto subject = doc.object().value(QLatin1String("credentialSubject")).toObject();
30  cert.setName(subject.value(QLatin1String("name")).toString());
31  const auto evidences = doc.object().value(QLatin1String("evidence")).toArray();
32  if (evidences.isEmpty()) {
33  return {};
34  }
35 
36  const auto evidence = evidences.at(0).toObject();
37  cert.setDate(QDateTime::fromString(evidence.value(QLatin1String("date")).toString(), Qt::ISODate).date());
38  cert.setVaccine(evidence.value(QLatin1String("vaccine")).toString());
39  cert.setManufacturer(evidence.value(QLatin1String("manufacturer")).toString());
40  cert.setDose(evidence.value(QLatin1String("dose")).toInt());
41  cert.setTotalDoses(evidence.value(QLatin1String("totalDoses")).toInt());
42 
43  const auto facility = evidence.value(QLatin1String("facility")).toObject();
44  const auto address = facility.value(QLatin1String("address")).toObject();
45  cert.setCountry(address.value(QLatin1String("addressCountry")).toString());
46 
47  cert.setCertificateId(evidence.value(QLatin1String("certificateId")).toString());
48  cert.setCertificateIssuer(doc.object().value(QLatin1String("issuer")).toString());
49  cert.setCertificateIssueDate(QDateTime::fromString(doc.object().value(QLatin1String("issuanceDate")).toString(), Qt::ISODate));
50 
51  JwsVerifier verifier(doc.object());
52  cert.setSignatureState(verifier.verify() ? KHealthCertificate::ValidSignature : KHealthCertificate::InvalidSignature);
53 
54  return cert;
55 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
PostalAddress address(const QVariant &location)
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QDateTime fromString(const QString &string, Qt::DateFormat format)
A vaccination certificate.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:21:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.