mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 21:40:39 +02:00
Class for communication with server
This commit is contained in:
parent
0f75f32ddd
commit
870f5487ba
124
src/colnod/requestmanager.cpp
Normal file
124
src/colnod/requestmanager.cpp
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
#include "requestmanager.h"
|
||||||
|
|
||||||
|
RequestManager::RequestManager()
|
||||||
|
{
|
||||||
|
manager = new QNetworkAccessManager();
|
||||||
|
token = "";
|
||||||
|
loginStatus = false;
|
||||||
|
|
||||||
|
QFile configFile(QDir::current().filePath("config.json"));
|
||||||
|
|
||||||
|
if (!configFile.open(QIODevice::ReadOnly)) {
|
||||||
|
qCritical() << "Couldn't open config file config.json" << QDir::current().filePath("config.json");
|
||||||
|
QCoreApplication::exit(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->config = QJsonDocument::fromJson(configFile.readAll());
|
||||||
|
QObject::connect(manager, &QNetworkAccessManager::sslErrors, this, &RequestManager::onSSLError);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RequestManager::login(QString host, QString username, QString password)
|
||||||
|
{
|
||||||
|
QNetworkRequest request;
|
||||||
|
QString hash = encryptedPassword(password);
|
||||||
|
authHeader = getLoginHeader(username, hash);
|
||||||
|
QJsonObject reqBody = {
|
||||||
|
{"password", hash},
|
||||||
|
{"username", username}
|
||||||
|
};
|
||||||
|
this->host = host;
|
||||||
|
|
||||||
|
request.setUrl(host + "/AaaManager/authSession");
|
||||||
|
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
||||||
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8");
|
||||||
|
QJsonDocument jsonDocument;
|
||||||
|
jsonDocument.setObject(reqBody);
|
||||||
|
QNetworkReply* reply = manager->post(request, jsonDocument.toJson());
|
||||||
|
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||||
|
if (reply->error())
|
||||||
|
{
|
||||||
|
qDebug() << reply->errorString();
|
||||||
|
loginStatus = false;
|
||||||
|
qDebug() << reply->readAll();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
jsonResponse = reply->readAll();
|
||||||
|
prepareAuthHeader();
|
||||||
|
emit successLogin();
|
||||||
|
reply->deleteLater();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void RequestManager::getRequest(QString endpoint)
|
||||||
|
{
|
||||||
|
QNetworkRequest request;
|
||||||
|
request.setUrl(host + endpoint);
|
||||||
|
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
||||||
|
QNetworkReply* reply = manager->post(request, QByteArray());
|
||||||
|
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||||
|
if (reply->error())
|
||||||
|
{
|
||||||
|
qDebug() << reply->errorString();
|
||||||
|
qDebug() << reply->readAll();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//print reply info
|
||||||
|
auto items = reply->rawHeaderPairs();
|
||||||
|
for (auto item : items)
|
||||||
|
{
|
||||||
|
qDebug() << item;
|
||||||
|
}
|
||||||
|
auto reqHeaderName = reply->request().rawHeaderList();
|
||||||
|
for (QString header : reqHeaderName)
|
||||||
|
{
|
||||||
|
qDebug() << reply->request().rawHeader(header.toUtf8());
|
||||||
|
}
|
||||||
|
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
||||||
|
|
||||||
|
jsonResponse = reply->readAll();
|
||||||
|
emit responseReady();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
QString RequestManager::getFromJson(QString json, QString member)
|
||||||
|
{
|
||||||
|
QJsonParseError err;
|
||||||
|
QJsonDocument document = QJsonDocument::fromJson(json.toUtf8(),&err);
|
||||||
|
if (err.error != 0)
|
||||||
|
{
|
||||||
|
qDebug() << err.errorString();
|
||||||
|
}
|
||||||
|
return document[member].toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString RequestManager::encryptedPassword(QString password)
|
||||||
|
{
|
||||||
|
return QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha512).toHex().toUpper();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString RequestManager::getLoginHeader(QString username, QString hash)
|
||||||
|
{
|
||||||
|
QString authValue = username + ":" + hash;
|
||||||
|
return "Basic " + authValue.toLocal8Bit().toBase64();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RequestManager::prepareAuthHeader()
|
||||||
|
{
|
||||||
|
token = getFromJson(jsonResponse, "id");
|
||||||
|
QString authValue = token + ":";
|
||||||
|
authHeader = "Basic" + authValue.toLocal8Bit().toBase64();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RequestManager::onSSLError(QNetworkReply *reply, const QList<QSslError> &errors)
|
||||||
|
{
|
||||||
|
qDebug() << "Network SSL errors";
|
||||||
|
for (const auto &error : errors)
|
||||||
|
{
|
||||||
|
if (error.error() == QSslError::SelfSignedCertificate)
|
||||||
|
reply->ignoreSslErrors(errors);
|
||||||
|
qDebug() << error.errorString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
62
src/colnod/requestmanager.h
Normal file
62
src/colnod/requestmanager.h
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#ifndef REQUESTMANAGER_H
|
||||||
|
#define REQUESTMANAGER_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QNetworkReply>
|
||||||
|
#include <QNetworkAccessManager>
|
||||||
|
#include <QNetworkRequest>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <qcoreapplication.h>
|
||||||
|
#include <qnetworkreply.h>
|
||||||
|
#include <QByteArray>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonParseError>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QString>
|
||||||
|
#include <QMultiMap>
|
||||||
|
#include <QPointer>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QSslError>
|
||||||
|
|
||||||
|
class RequestManager : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
RequestManager();
|
||||||
|
void login(QString endpoint, QString username, QString password);
|
||||||
|
void getRequest(QString endpoint);
|
||||||
|
void fetchSubjects(QString endpoint);
|
||||||
|
const QJsonDocument &getConfig() const {return config;}
|
||||||
|
const QString &getResponse() const {return jsonResponse;}
|
||||||
|
QString getToken(){return token;}
|
||||||
|
private:
|
||||||
|
|
||||||
|
QString getFromJson(QString json, QString member);
|
||||||
|
QString encryptedPassword(QString password);
|
||||||
|
QString getLoginHeader(QString username, QString hash);
|
||||||
|
void prepareAuthHeader();
|
||||||
|
private slots:
|
||||||
|
void onSSLError(QNetworkReply *reply, const QList<QSslError> &errors);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void successLogin();
|
||||||
|
void responseReady();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
QNetworkAccessManager* manager;
|
||||||
|
|
||||||
|
QJsonDocument config;
|
||||||
|
QString authHeader;
|
||||||
|
QString host;
|
||||||
|
QString token;
|
||||||
|
QString jsonResponse;
|
||||||
|
bool loginStatus;
|
||||||
|
QString tenant;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // REQUESTMANAGER_H
|
||||||
Loading…
x
Reference in New Issue
Block a user