From f7abd0b228f8e47d0160da80dcffada8dbd02e98 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Sat, 28 Jan 2017 14:05:30 +0100 Subject: [PATCH 1/4] Added parsing of warnings and errors. --- eetsend/main.cpp | 1 + libEet/eetresult.h | 3 +++ libEet/eetsender.cpp | 49 +++++++++++++++++++++----------------------- libEet/eetsender.h | 12 ++++++----- libEet/libEet.pro | 2 +- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/eetsend/main.cpp b/eetsend/main.cpp index bb178da..9c2408f 100644 --- a/eetsend/main.cpp +++ b/eetsend/main.cpp @@ -22,6 +22,7 @@ int main(int argc, char *argv[]) request.setRezim(EetRequest::STANDARD); EetSender sender; + sender.setPlayground(true); sender.setupSigner("/home/pepa/Dokumenty/dev/eet/121212.p12", "eet"); sender.sendRequest(&request); diff --git a/libEet/eetresult.h b/libEet/eetresult.h index b22cc97..b9ca65f 100644 --- a/libEet/eetresult.h +++ b/libEet/eetresult.h @@ -35,7 +35,10 @@ public: { RESPONSE_OK = 0, DATA_ERROR, + USER_CERT_ERROR, SERVER_ERROR, + SERVICE_CERT_ERROR, + INVALID_SIGNATURE, SSL_ERROR, TIMEDOUT }; diff --git a/libEet/eetsender.cpp b/libEet/eetsender.cpp index 66e4cf6..a53c7ac 100644 --- a/libEet/eetsender.cpp +++ b/libEet/eetsender.cpp @@ -5,12 +5,8 @@ #include "eetsigner.h" #include -#include -#include -#include #include #include -#include const QString EetSender::ms_nsDef = "declare namespace eet = \"http://fs.mfcr.cz/eet/schema/v3\";\n" "declare namespace ds = \"http://www.w3.org/2000/09/xmldsig#\";\n" @@ -25,18 +21,25 @@ EetSender::EetSender(QObject *parent) : QObject(parent) m_resut = nullptr; m_checkSignature = true; + m_resut = new EetResult(this); + m_manager = new QNetworkAccessManager(this); connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); - connect(m_manager, &QNetworkAccessManager::sslErrors, [this](QNetworkReply *rep, QList errs){ - emit serviceCertError(); + connect(m_manager, &QNetworkAccessManager::sslErrors, [this](QNetworkReply *rep, QList){ + m_resut->setStatus(EetResult::SSL_ERROR); + emit sendFinished(m_resut); + rep->deleteLater(); }); + + m_serviceUrl = PRODUCTION_URL; } void EetSender::sendRequest(EetRequest *request) { if (m_signer == nullptr) { - emit certError(); + m_resut->setStatus(EetResult::USER_CERT_ERROR); + emit sendFinished(m_resut); return; } @@ -60,12 +63,8 @@ void EetSender::sendRequest(EetRequest *request) QString strRequest = tempRequest.fillTemplate(val); - QNetworkRequest req(QUrl("https://pg.eet.cz/eet/services/EETServiceSOAP/v3")); + QNetworkRequest req(QUrl(m_serviceUrl.toStdString().c_str())); m_manager->post(req, strRequest.toUtf8()); - - QFile file("/home/pepa/Dokumenty/dev/eet/req.xml"); - file.open(QIODevice::WriteOnly); - file.write(strRequest.toUtf8()); } void EetSender::setupSigner(const QString &certPath, const QString &passwd) @@ -89,6 +88,11 @@ bool EetSender::checkSignature() const return m_checkSignature; } +void EetSender::setPlayground(bool pg) +{ + m_serviceUrl = pg ? PLAYGROUND_URL : PRODUCTION_URL; +} + EetResult *EetSender::resut() const { return m_resut; @@ -129,7 +133,7 @@ bool EetSender::verifySignature(const QByteArray &repData) if (res != QCA::ConvertGood) { - emit serviceCertError(); + m_resut->setStatus(EetResult::SERVICE_CERT_ERROR); return false; } @@ -137,7 +141,7 @@ bool EetSender::verifySignature(const QByteArray &repData) if (!pubKey.canVerify()) { - emit serviceCertError(); + m_resut->setStatus(EetResult::SERVICE_CERT_ERROR); return false; } @@ -145,7 +149,7 @@ bool EetSender::verifySignature(const QByteArray &repData) if (!signValid) { - emit signInvalid(); + m_resut->setStatus(EetResult::INVALID_SIGNATURE); return false; } @@ -156,19 +160,17 @@ void EetSender::replyFinished(QNetworkReply *reply) { if (reply->error() != QNetworkReply::NoError) { - emit sendError(); + m_resut->setStatus(EetResult::SERVER_ERROR); + emit sendFinished(m_resut); reply->deleteLater(); return; } QByteArray repData = reply->readAll(); - /*QFile file("/home/pepa/Dokumenty/dev/eet/reply.xml"); - file.open(QIODevice::ReadOnly); - repData = file.readAll();*/ - if (m_checkSignature && !verifySignature(repData)) { + emit sendFinished(m_resut); return; } @@ -181,11 +183,6 @@ void EetSender::replyFinished(QNetworkReply *reply) q.evaluateTo(&result); result = result.trimmed(); - if (m_resut == nullptr) - { - m_resut = new EetResult(this); - } - m_resut->setUuid(QUuid(result)); queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@dat_prij/data(.)"; @@ -262,7 +259,7 @@ void EetSender::replyFinished(QNetworkReply *reply) } m_resut->setErrors(errors); - emit responseRecieved(m_resut); + emit sendFinished(m_resut); reply->deleteLater(); } diff --git a/libEet/eetsender.h b/libEet/eetsender.h index 81481ec..3cd5b7b 100644 --- a/libEet/eetsender.h +++ b/libEet/eetsender.h @@ -8,6 +8,9 @@ #include "eettemplate.h" #include "eetresult.h" +#define PRODUCTION_URL "https://prod.eet.cz/eet/services/EETServiceSOAP/v3" +#define PLAYGROUND_URL "https://pg.eet.cz/eet/services/EETServiceSOAP/v3" + class QNetworkAccessManager; class QNetworkReply; @@ -23,6 +26,8 @@ public: void setCheckSignature(bool checkSignature); bool checkSignature() const; + void setPlayground(bool pg); + EetResult *resut() const; private: @@ -31,6 +36,7 @@ private: QNetworkAccessManager *m_manager; bool m_checkSignature; EetResult *m_resut; + QString m_serviceUrl; bool verifySignature(const QByteArray &repData); @@ -38,11 +44,7 @@ private slots: void replyFinished(QNetworkReply *reply); signals: - void certError(); - void sendError(); - void responseRecieved(EetRequest *result); - void signInvalid(); - void serviceCertError(); + void sendFinished(EetResult *result); public slots: }; diff --git a/libEet/libEet.pro b/libEet/libEet.pro index 2546763..8f0999b 100644 --- a/libEet/libEet.pro +++ b/libEet/libEet.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += network xml xmlpatterns +QT += network xmlpatterns QT -= gui From ae7eb571159dc90eff7da449bcf9c0a0141f3b3d Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Thu, 9 Feb 2017 21:35:17 +0100 Subject: [PATCH 2/4] Added PKP and BKP codes to EetResult. Added doxygen comments to EetSender class. --- libEet/eetresult.cpp | 20 ++++++++++++++++++++ libEet/eetresult.h | 8 ++++++++ libEet/eetsender.cpp | 2 ++ libEet/eetsender.h | 28 ++++++++++++++++++++++++++++ libEet/eettemplate.cpp | 14 ++++++++++++++ libEet/eettemplate.h | 4 ++++ 6 files changed, 76 insertions(+) diff --git a/libEet/eetresult.cpp b/libEet/eetresult.cpp index fa987df..700b5cf 100644 --- a/libEet/eetresult.cpp +++ b/libEet/eetresult.cpp @@ -65,6 +65,26 @@ void EetResult::setReciveDate(const QDateTime &reciveDate) m_reciveDate = reciveDate; } +QString EetResult::pkp() const +{ + return m_pkp; +} + +void EetResult::setPkp(const QString &pkp) +{ + m_pkp = pkp; +} + +QString EetResult::bkp() const +{ + return m_bkp; +} + +void EetResult::setBkp(const QString &bkp) +{ + m_bkp = bkp; +} + EetMessage::EetMessage(QObject *parent) :QObject(parent) { diff --git a/libEet/eetresult.h b/libEet/eetresult.h index b9ca65f..12ba172 100644 --- a/libEet/eetresult.h +++ b/libEet/eetresult.h @@ -63,11 +63,19 @@ public: QDateTime reciveDate() const; void setReciveDate(const QDateTime &reciveDate); + QString pkp() const; + void setPkp(const QString &pkp); + + QString bkp() const; + void setBkp(const QString &bkp); + private: ResponseStatus m_status; QUuid m_uuid; QDateTime m_reciveDate; QString m_fik; + QString m_pkp; + QString m_bkp; EetMessageList m_warnings; EetMessageList m_errors; }; diff --git a/libEet/eetsender.cpp b/libEet/eetsender.cpp index a53c7ac..9132a98 100644 --- a/libEet/eetsender.cpp +++ b/libEet/eetsender.cpp @@ -47,6 +47,7 @@ void EetSender::sendRequest(EetRequest *request) EetTemplate tempBody(BODY_TEMPLATE); tempBody.setSigner(m_signer); + tempBody.setResult(m_resut); QString strBody = tempBody.fillTemplate(request); QByteArray digest = m_signer->sha256HashData(strBody.toUtf8()); @@ -259,6 +260,7 @@ void EetSender::replyFinished(QNetworkReply *reply) } m_resut->setErrors(errors); + m_resut->setReciveDate(QDateTime::currentDateTime()); emit sendFinished(m_resut); reply->deleteLater(); diff --git a/libEet/eetsender.h b/libEet/eetsender.h index 3cd5b7b..708afac 100644 --- a/libEet/eetsender.h +++ b/libEet/eetsender.h @@ -14,20 +14,48 @@ class QNetworkAccessManager; class QNetworkReply; +/** + * @brief The EetSender class Performs send actions. + */ class EETCPPSHARED_EXPORT EetSender : public QObject { Q_OBJECT public: explicit EetSender(QObject *parent = 0); + /** + * @brief Sends request to EET web service + * @param request EET data + */ void sendRequest(EetRequest *request); + /** + * @brief Setup certificate for signing EET request data. + * @param certPath Path to certificate p12 file. File must contain private key. + * @param passwd Password for accesing p12 file. + */ void setupSigner(const QString &certPath, const QString &passwd); + /** + * @brief Sets verifing signature of response data. + * @param checkSignature true if you wish to verify. + */ void setCheckSignature(bool checkSignature); + /** + * @brief Returns signature verifing state. + * @return true if verify. + */ bool checkSignature() const; + /** + * @brief Sets comunication with playground EET portal. + * @param pg true if you wish to communicate with playground portal. + */ void setPlayground(bool pg); + /** + * @brief Returns comunication result. + * @return result + */ EetResult *resut() const; private: diff --git a/libEet/eettemplate.cpp b/libEet/eettemplate.cpp index 2734d90..7ef6761 100644 --- a/libEet/eettemplate.cpp +++ b/libEet/eettemplate.cpp @@ -1,3 +1,4 @@ +#include "eetresult.h" #include "eettemplate.h" #include @@ -5,6 +6,7 @@ EetTemplate::EetTemplate(QObject *parent) : QObject(parent) { + m_result = nullptr; } EetTemplate::EetTemplate(const QString &tmp, QObject *parent) : QObject(parent) @@ -14,6 +16,7 @@ EetTemplate::EetTemplate(const QString &tmp, QObject *parent) : QObject(parent) QTextStream stream(&tmpFile); m_template = stream.readAll(); + m_result = nullptr; } QString EetTemplate::fillTemplate(EetRequest *request) @@ -39,6 +42,12 @@ QString EetTemplate::fillTemplate(EetRequest *request) splitedBkp += base16bkp[i]; } + if (m_result != nullptr) + { + m_result->setPkp(base64Sign); + m_result->setBkp(splitedBkp.toUpper()); + } + QMap varMap; varMap["pkp"] = base64Sign; varMap["bkp"] = splitedBkp.toUpper(); @@ -131,3 +140,8 @@ QString EetTemplate::getPkpString(EetRequest *request) request->property("celk_trzba").toString()); return ret; } + +void EetTemplate::setResult(EetResult *result) +{ + m_result = result; +} diff --git a/libEet/eettemplate.h b/libEet/eettemplate.h index b2e5256..cb8c620 100644 --- a/libEet/eettemplate.h +++ b/libEet/eettemplate.h @@ -12,6 +12,8 @@ #define REQUEST_TEMPLATE ":/res/template_request.txt" #define SIGNATURE_TEMPLATE ":/res/template_signature.txt" +class EetResult; + class EetTemplate : public QObject { Q_OBJECT @@ -22,6 +24,7 @@ public: QString fillTemplate(EetRequest *request); QString fillTemplate(QMap map); void setSigner(EetSigner *signer) { m_signer = signer; } + void setResult(EetResult *result); signals: @@ -32,6 +35,7 @@ private: QString fillTemplateInternal(EetRequest *request, QMap *map); QString getPkpString(EetRequest *request); EetSigner *m_signer; + EetResult *m_result; }; #endif // EETTEMPLATE_H From bd4fb7789df85d8b14fd63ef3e0291943d1dbc4d Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 21 Feb 2017 21:03:49 +0100 Subject: [PATCH 3/4] Improved error handling. --- libEet/eetresult.h | 3 +- libEet/eetsender.cpp | 124 ++++++++++++++++++++++++++++--------------- libEet/eetsender.h | 8 ++- 3 files changed, 91 insertions(+), 44 deletions(-) diff --git a/libEet/eetresult.h b/libEet/eetresult.h index 12ba172..0899fa1 100644 --- a/libEet/eetresult.h +++ b/libEet/eetresult.h @@ -40,7 +40,8 @@ public: SERVICE_CERT_ERROR, INVALID_SIGNATURE, SSL_ERROR, - TIMEDOUT + TIMEDOUT, + OFFLINE }; explicit EetResult(QObject *parent = 0); diff --git a/libEet/eetsender.cpp b/libEet/eetsender.cpp index 9132a98..4384a25 100644 --- a/libEet/eetsender.cpp +++ b/libEet/eetsender.cpp @@ -7,6 +7,7 @@ #include #include #include +#include const QString EetSender::ms_nsDef = "declare namespace eet = \"http://fs.mfcr.cz/eet/schema/v3\";\n" "declare namespace ds = \"http://www.w3.org/2000/09/xmldsig#\";\n" @@ -15,6 +16,8 @@ const QString EetSender::ms_nsDef = "declare namespace eet = \"http://fs.mfcr.cz "declare namespace senc = \"http://schemas.xmlsoap.org/soap/encoding/\";\n" "declare namespace senv = \"http://schemas.xmlsoap.org/soap/envelope/\";\n"; +bool EetSender::m_online = true; + EetSender::EetSender(QObject *parent) : QObject(parent) { m_signer = nullptr; @@ -27,11 +30,14 @@ EetSender::EetSender(QObject *parent) : QObject(parent) connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); connect(m_manager, &QNetworkAccessManager::sslErrors, [this](QNetworkReply *rep, QList){ m_resut->setStatus(EetResult::SSL_ERROR); - emit sendFinished(m_resut); + emit sendFinished(); rep->deleteLater(); }); m_serviceUrl = PRODUCTION_URL; + m_timer = new QTimer(this); + m_timer->setInterval(1000); + m_timer->setSingleShot(true); } void EetSender::sendRequest(EetRequest *request) @@ -39,7 +45,7 @@ void EetSender::sendRequest(EetRequest *request) if (m_signer == nullptr) { m_resut->setStatus(EetResult::USER_CERT_ERROR); - emit sendFinished(m_resut); + emit sendFinished(); return; } @@ -65,7 +71,24 @@ void EetSender::sendRequest(EetRequest *request) QString strRequest = tempRequest.fillTemplate(val); QNetworkRequest req(QUrl(m_serviceUrl.toStdString().c_str())); - m_manager->post(req, strRequest.toUtf8()); + + qDebug() << strRequest; + + if (m_online) + { + QNetworkReply *rep = m_manager->post(req, strRequest.toUtf8()); + m_timer->start(); + connect(m_timer, &QTimer::timeout, [this, rep](){ + rep->abort(); + m_resut->setStatus(EetResult::TIMEDOUT); + emit sendFinished(); + }); + } + else + { + m_resut->setStatus(EetResult::OFFLINE); + emit sendFinished(); + } } void EetSender::setupSigner(const QString &certPath, const QString &passwd) @@ -91,7 +114,7 @@ bool EetSender::checkSignature() const void EetSender::setPlayground(bool pg) { - m_serviceUrl = pg ? PLAYGROUND_URL : PRODUCTION_URL; + m_serviceUrl = (pg ? PLAYGROUND_URL : PRODUCTION_URL); } EetResult *EetSender::resut() const @@ -99,6 +122,11 @@ EetResult *EetSender::resut() const return m_resut; } +void EetSender::setTimeout(int timeout) +{ + m_timer->setInterval(timeout); +} + bool EetSender::verifySignature(const QByteArray &repData) { QString queryString("//senv:Envelope/senv:Header/wsse:Security/ds:Signature/ds:SignedInfo"); @@ -159,56 +187,34 @@ bool EetSender::verifySignature(const QByteArray &repData) void EetSender::replyFinished(QNetworkReply *reply) { + m_timer->stop(); if (reply->error() != QNetworkReply::NoError) { m_resut->setStatus(EetResult::SERVER_ERROR); - emit sendFinished(m_resut); + emit sendFinished(); reply->deleteLater(); return; } QByteArray repData = reply->readAll(); - if (m_checkSignature && !verifySignature(repData)) - { - emit sendFinished(m_resut); - return; - } + qDebug() << QString(repData); QXmlQuery q; q.setFocus(QString(repData)); - QString result; - QString queryString("//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@uuid_zpravy/data(.)"); - q.setQuery(ms_nsDef + queryString); - q.evaluateTo(&result); - result = result.trimmed(); - - m_resut->setUuid(QUuid(result)); - - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@dat_prij/data(.)"; - q.setQuery(ms_nsDef + queryString); - q.evaluateTo(&result); - result = result.trimmed(); - m_resut->setReciveDate(QDateTime::fromString(result)); - - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Potvrzeni/@fik/data(.)"; - q.setQuery(ms_nsDef + queryString); - q.evaluateTo(&result); - result = result.trimmed(); - m_resut->setFik(result); - QXmlResultItems items; - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Varovani"; + QString result; + QString queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Chyba"; q.setQuery(ms_nsDef + queryString); q.evaluateTo(&items); - EetMessageList warnings; + EetMessageList errors; QXmlItem item = items.next(); while (!item.isNull()) { EetMessage *mesg = new EetMessage(m_resut); - queryString = "./@kod_varov/data(.)"; + queryString = "./@kod/data(.)"; q.setQuery(ms_nsDef + queryString); q.setFocus(item); q.evaluateTo(&result); @@ -223,23 +229,48 @@ void EetSender::replyFinished(QNetworkReply *reply) result = result.trimmed(); mesg->setMessage(result); - warnings.append(mesg); + errors.append(mesg); item = items.next(); } - m_resut->setWarnings(warnings); + m_resut->setErrors(errors); + + if (m_checkSignature && errors.isEmpty() && !verifySignature(repData)) + { + emit sendFinished(); + return; + } + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@uuid_zpravy/data(.)"; + q.setQuery(ms_nsDef + queryString); + q.evaluateTo(&result); + result = result.trimmed(); - queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Chyba"; + m_resut->setUuid(QUuid(result)); + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Hlavicka/@dat_prij/data(.)"; + q.setQuery(ms_nsDef + queryString); + q.evaluateTo(&result); + result = result.trimmed(); + m_resut->setReciveDate(QDateTime::fromString(result)); + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Potvrzeni/@fik/data(.)"; + q.setQuery(ms_nsDef + queryString); + q.evaluateTo(&result); + result = result.trimmed(); + m_resut->setFik(result); + + queryString = "//senv:Envelope/senv:Body/eet:Odpoved/eet:Varovani"; q.setQuery(ms_nsDef + queryString); q.evaluateTo(&items); - EetMessageList errors; + EetMessageList warnings; item = items.next(); while (!item.isNull()) { EetMessage *mesg = new EetMessage(m_resut); - queryString = "./@kod/data(.)"; + queryString = "./@kod_varov/data(.)"; q.setQuery(ms_nsDef + queryString); q.setFocus(item); q.evaluateTo(&result); @@ -254,14 +285,23 @@ void EetSender::replyFinished(QNetworkReply *reply) result = result.trimmed(); mesg->setMessage(result); - errors.append(mesg); + warnings.append(mesg); item = items.next(); } - m_resut->setErrors(errors); - m_resut->setReciveDate(QDateTime::currentDateTime()); - emit sendFinished(m_resut); + m_resut->setWarnings(warnings); + + if (errors.isEmpty()) + { + m_resut->setReciveDate(QDateTime::currentDateTime()); + } + else + { + m_resut->setStatus(EetResult::DATA_ERROR); + } + + emit sendFinished(); reply->deleteLater(); } diff --git a/libEet/eetsender.h b/libEet/eetsender.h index 708afac..c520509 100644 --- a/libEet/eetsender.h +++ b/libEet/eetsender.h @@ -2,6 +2,7 @@ #define EETSENDER_H #include +#include #include "eetcpp_global.h" #include "eetrequest.h" @@ -58,10 +59,15 @@ public: */ EetResult *resut() const; + void setTimeout(int timeout); + + static bool m_online; + private: static const QString ms_nsDef; EetSigner *m_signer; QNetworkAccessManager *m_manager; + QTimer *m_timer; bool m_checkSignature; EetResult *m_resut; QString m_serviceUrl; @@ -72,7 +78,7 @@ private slots: void replyFinished(QNetworkReply *reply); signals: - void sendFinished(EetResult *result); + void sendFinished(); public slots: }; From 601033294dae8f14ee16bfce36d9e50cfe8e2852 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 21 Feb 2017 22:13:33 +0100 Subject: [PATCH 4/4] Fixed errors parsing. --- libEet/eetsender.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libEet/eetsender.cpp b/libEet/eetsender.cpp index 4384a25..207c94f 100644 --- a/libEet/eetsender.cpp +++ b/libEet/eetsender.cpp @@ -87,6 +87,7 @@ void EetSender::sendRequest(EetRequest *request) else { m_resut->setStatus(EetResult::OFFLINE); + m_timer->stop(); emit sendFinished(); } } @@ -222,7 +223,7 @@ void EetSender::replyFinished(QNetworkReply *reply) mesg->setCode(result.toInt()); - queryString = "./text(.)"; + queryString = "./string()"; q.setQuery(ms_nsDef + queryString); q.setFocus(item); q.evaluateTo(&result); @@ -278,7 +279,7 @@ void EetSender::replyFinished(QNetworkReply *reply) mesg->setCode(result.toInt()); - queryString = "./text(.)"; + queryString = "./string()"; q.setQuery(ms_nsDef + queryString); q.setFocus(item); q.evaluateTo(&result);