KHealthCertificate

divocparser.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
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 <QJsonDocument>
12#include <QJsonArray>
13#include <QVariant>
14
15void DivocParser::init()
16{
17 Q_INIT_RESOURCE(divoc_data);
18}
19
20QVariant DivocParser::parse(const QByteArray &data)
21{
23 const auto doc = QJsonDocument::fromJson(data, &error);
24 if (error.error != QJsonParseError::NoError) {
25 return {};
26 }
27
28 // TODO check this is actually a VerifiableCredential structure
29 // TODO check the types on the subobjects we use
30
32 const auto subject = doc.object().value(QLatin1String("credentialSubject")).toObject();
33 cert.setName(subject.value(QLatin1String("name")).toString());
34 const auto evidences = doc.object().value(QLatin1String("evidence")).toArray();
35 if (evidences.isEmpty()) {
36 return {};
37 }
38
39 const auto evidence = evidences.at(0).toObject();
40 cert.setDate(QDateTime::fromString(evidence.value(QLatin1String("date")).toString(), Qt::ISODate).date());
41 cert.setVaccine(evidence.value(QLatin1String("vaccine")).toString());
42 cert.setManufacturer(evidence.value(QLatin1String("manufacturer")).toString());
43 cert.setDose(evidence.value(QLatin1String("dose")).toInt());
44 cert.setTotalDoses(evidence.value(QLatin1String("totalDoses")).toInt());
45
46 const auto facility = evidence.value(QLatin1String("facility")).toObject();
47 const auto address = facility.value(QLatin1String("address")).toObject();
48 cert.setCountry(address.value(QLatin1String("addressCountry")).toString());
49
50 cert.setCertificateId(evidence.value(QLatin1String("certificateId")).toString());
51 cert.setCertificateIssuer(doc.object().value(QLatin1String("issuer")).toString());
52 cert.setCertificateIssueDate(QDateTime::fromString(doc.object().value(QLatin1String("issuanceDate")).toString(), Qt::ISODate));
53
54 JwsVerifier verifier(doc.object());
55 cert.setSignatureState(verifier.verify() ? KHealthCertificate::ValidSignature : KHealthCertificate::InvalidSignature);
56
57 return cert;
58}
@ ValidSignature
signature is valid
@ InvalidSignature
signature is invalid
A vaccination certificate.
char * toString(const EngineQuery &query)
PostalAddress address(const QVariant &location)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:47:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.